I recently encountered an issue with my Azure Function written in JS that is triggered by the Service Bus and generates files to Blob Storage. When attempting to return an HTTP result, I received the following error message:
System.Private.CoreLib: Exception while executing function: Functions.categoryMessageConsumer. Microsoft.Azure.WebJobs.Script: Unable to cast object of type 'System.String' to type 'Microsoft.AspNetCore.Http.HttpRequest'.
It's perplexing why the result is being mapped to the HttpRequest object.
index.ts:
import { AzureFunction, Context, HttpRequest } from '@azure/functions';
...
const serviceBusTopicTrigger: AzureFunction = async function(context: Context, req: HttpRequest) {
let categoryMessage: CategoryMessage = Object.assign(new CategoryMessage(), req);
let messageValidationResult = await categoryMessage.validate();
if(!messageValidationResult.isValid) {
context.log.error(messageValidationResult.errors);
return {
status: 400,
body: "Unexpected error"
};
}
...
}
function.json output binding:
...
{
"type": "http",
"direction": "out",
"name": "$return"
}
...
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}