0
votes

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?

1
Anything in the logs of Function App?Mikhail Shilkov
@Mikhail In the Monitor Section, the log c# HTTP trigger function processed a request has log level of information. After that I am getting log level error. Date,Message and log level were the only information i could find in the invocation details.Harshith R
So what does the error message say?Mikhail Shilkov
@Mikhail This is logs from the console. 2018-09-06T09:24:17 Welcome, you are now connected to log-streaming service. 2018-09-06T09:24:31.605 [Information] Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=4832f4bb-325c-489b-92be-6d7427e9cb1b) 2018-09-06T09:24:31.605 [Information] C# HTTP trigger function processed a request. 2018-09-06T09:24:31.662 [Error] Executed 'Function1' (Failed, Id=4832f4bb-325c-489b-92be-6d7427e9cb1b)Harshith R
The Error message field is blankHarshith R

1 Answers

1
votes

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");
}