0

In VS Code, I created a new Function app using the command palette (Azure Static Web Apps: Create HTTP Function). I used C# as the Function language.

The following reference was added to the csproj file.

<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />

The first function works fine

[FunctionName("GetDocumentSignatureStatus")]
public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.User, "get", Route = "departments/{dept:alpha}/documents/{doc}/versions/{version}/signatures/{userId}")] 
        HttpRequest req, ILogger log, 
        string dept, string doc, string version, string userId)
{
    //implementation 
}

I added a a second function within the same class with this signature.

[FunctionName("SignDocumentForUser")]
    public static async Task<IActionResult> Sign(
        [HttpTrigger(AuthorizationLevel.User, "post", Route = "departments/{dept:alpha}/documents/{doc}/versions/{version}/signatures")]
        HttpRequest req, 
        ILogger log, 
        string dept, string doc, string version,
        [FromBody]UserSignature signature)
{
    //implementation
}

The first function ("GetDocumentSignatureStatus") works as expected from my static web app. The second ("SignDocumentForUser") does not work and gives the following error.

[api] [2023-07-11T17:16:16.321Z] An unhandled host error has occurred. [api] [2023-07-11T17:16:16.321Z] Microsoft.Azure.WebJobs.Host: 'SignDocumentForUser' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?.

I run the app from the Terminal in VS Code using

edit:

swa start --host=localhost --port=3010 --app-devserver-url=http:/localhost:7071 --run "npm start"

I've tried using the command palette to create a second Http Function.

I've also tried following suggestions from here and here.

After trying Ikhtesam Afrin's suggestion, I was prompted to look into the build output where I found this.

[api] [2023-07-14T13:17:41.768Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'SignDocumentForUser'ure. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azod for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), bui [api] [2023-07-14T13:17:41.773Z] Error indexing method 'SignDocumentForUser'

If I remove the [FromBody] parameter, the endpoint works. I cannot get the function to bind to that body parameter.

Matthew
  • 3
  • 3

1 Answers1

0

As I didn't have your complete code, I used default code to create two functions within same class using Azure Static Web Apps: Create HTTP Function.

In my case, both the functions are working as expected and I am using below versions.

<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>

Code

using  System;
using  System.IO;
using  System.Threading.Tasks;
using  Microsoft.AspNetCore.Mvc;
using  Microsoft.Azure.WebJobs;
using  Microsoft.Azure.WebJobs.Extensions.Http;
using  Microsoft.AspNetCore.Http;
using  Microsoft.Extensions.Logging;
using  Newtonsoft.Json;
namespace  DemoFunctionApp
{
public  static  class  GetDocumentSignatureStatus
{
[FunctionName("GetDocumentSignatureStatus")]
public  static  async  Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.User, "get", Route  =  null)] HttpRequest  req,
ILogger  log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string  name  =  req.Query["name"];
string  requestBody  =  await  new  StreamReader(req.Body).ReadToEndAsync();
dynamic  data  =  JsonConvert.DeserializeObject(requestBody);
name  =  name  ??  data?.name;
string  responseMessage  =  string.IsNullOrEmpty(name)
?  "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
:  $"Hello, {name}. This HTTP triggered function executed successfully.";
return  new  OkObjectResult(responseMessage);
}
[FunctionName("SignDocumentForUser")]
public  static  async  Task<IActionResult> Sign(
[HttpTrigger(AuthorizationLevel.User, "post", Route  =  null)] HttpRequest  req,
ILogger  log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string  name  =  req.Query["name"];
string  requestBody  =  await  new  StreamReader(req.Body).ReadToEndAsync();
dynamic  data  =  JsonConvert.DeserializeObject(requestBody);
name  =  name  ??  data?.name;
string  responseMessage  =  string.IsNullOrEmpty(name)
?  "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
:  $"Hello, {name}. This HTTP triggered function executed successfully.";
return  new  OkObjectResult(responseMessage);
}
}
}

My extension bundle reference in the host.json as follows:

{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
}
}

Output

enter image description here

enter image description here

enter image description here

Try to create the functions again with latest versions. It seems like a recurring issue for some cases and this issue got resolved after upgrading to new version or creating the functions freshly as per github link

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6
  • Thanks for your input. I did this and it worked so I compared with what I had and the difference is that you don't have my UserSignature object in the function signature. If I revert to my original code and remove [FromBody]UserSignature my code works. I then looked closer at the build output and in the wall of text, I found additional info that I'm adding to my question. – Matthew Jul 14 '23 at 17:15
  • @SPT this did not solve my problem. I amended my original question to note that the problem seems to be stemming from the [FromBody] function parameter. I have not solved why that doesn't work. – Matthew Jul 27 '23 at 13:33