I have created and published a Http triggered Azure function from visual studio.The function is for connecting to the iot hub and getting the module twin properties.
I have configured the local.settings.json file with the module connection string and also added the same in the application settings in the portal. But when I run the function in the portal its giving me internal server error 500.
Azure function version I am running is v1.
json files i am using:
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxxxxxx",
"AzureWebJobsDashboard": "xxxxxx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ModuleConnectionString": "xxxxxxxxx"
}
}
host.json
{
"http": {
"routePrefix": "api",
"maxOutstandingRequests": 20,
"maxConcurrentRequests": 10,
"dynamicThrottlesEnabled": false
},
"version": "2.0"
}
function.json
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.19",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"methods": [
"get",
"post"
],
"authLevel": "function",
"name": "req"
}
],
"disabled": false,
"scriptFile": xxxx.dll",
"entryPoint": "xxxxxxxx.Run"
}
following is the code:
function1.cs
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var moduleConnectionString = Environment.GetEnvironmentVariable("ModuleConnectionString", EnvironmentVariableTarget.Process);
ModuleClient _moduleClient = ModuleClient.CreateFromConnectionString(moduleConnectionString, TransportType.Amqp);
var sample = new TwinSample(_moduleClient);
await sample.RunSampleAsync();
}
}
twin.cs
class TwinSample
{
private ModuleClient _moduleClient;
public TwinSample(ModuleClient moduleClient)
{
this._moduleClient = _moduleClient;
}
public async Task<string> RunSampleAsync()
{
Console.WriteLine("Retrieving twin...");
Twin twin = await _moduleClient.GetTwinAsync().ConfigureAwait(false);
Console.WriteLine("\tInitial twin value received:");
Console.WriteLine($"\t{twin.ToJson()}");
return twin.ToJson();
}
}
I have tried changing the runtime version from 1 to 2 and vice versa. Still its not working. Also under Application Setting I have set
WEBSITE_NODE_DEFAULT_VERSION
to 8.11.1.from 6.5.0.
Can any one help please me resolve this issue?
HTTP Function is supposed to actually return an HTTP result, try changing your Function to
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
ILogger log)
{
// your code goes here
return req.CreateResponse(HttpStatusCode.OK, "Done");
}