1
votes

I have a function in a folder called 'TestMe', like so:

enter image description here

I can build the project in VSCode and upload it fine however the contents of function.json seems to be ignored.

My function is pretty simple:

    [FunctionName("TestMe")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, string graphToken, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = new StreamReader(req.Body).ReadToEnd();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }

and the function.json:

{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "type": "token",
      "direction": "in",
      "name": "graphToken",
      "resource": "https://graph.microsoft.com",
      "identity": "userFromRequest"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

I was following the guide here where I thought I could get a graphToken but instead all I got was this error:

Microsoft.Azure.WebJobs.JobHost.Validate(IFunctionDefinition function, object key) in JobHost.cs Microsoft.Azure.WebJobs.JobHost+d__36.MoveNext() in JobHost.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) Microsoft.Azure.WebJobs.Script.ScriptHost+d__86.MoveNext() in ScriptHost.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) Microsoft.Azure.WebJobs.Script.WebHost.Middleware.FunctionInvocationMiddleware+d__4.MoveNext() in FunctionInvocationMiddleware.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) Microsoft.Azure.WebJobs.Script.WebHost.Middleware.FunctionInvocationMiddleware+d__3.MoveNext() in FunctionInvocationMiddleware.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) System.Runtime.CompilerServices.TaskAwaiter.GetResult() Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()

So I tried disabling the function and building/uploading the azure functions again from VSCode but I could still access the function making me believe the function.json is being ignored.

1

1 Answers

1
votes

It looks like you are using precompiled C# Function, which means you have a csproj file and your function is compiled to a DLL and then deployed to Azure.

In this case, function.json is auto-generated by Functions SDK and you shouldn't be doing any manual changes to that file. The trigger will be placed to function.json automatically, and all other bindings are inferred from function signature and attributes.

So, you should place Token attribute on graphToken parameter, as described here:

In C# class libraries, use the Token attribute, which is defined in NuGet package Microsoft.Azure.WebJobs.Extensions.AuthTokens.