Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IActionResult with Azure Functions in .NET 5?

After migrating my Azure Functions project to .NET 5, it has started wrapping my responses in a weird wrapper class.

For instance, consider the following endpoint:

public record Response(string SomeValue);

[Function("Get")]
public async Task<IActionResult> Get(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "get-something")]
    HttpRequestData request)
{
    return new OkObjectResult(new Response("hello world"));
}

Before, it would return:

{
    "someValue": "hello world"
}

But now, it returns:

{
  "Value": {
    "SomeValue": "hello world"
  },
  "Formatters": [],
  "ContentTypes": [],
  "DeclaredType": null,
  "StatusCode": 200
}

I get that it must be because it just tries to serialize the object result, but I can't find any documentation on how this is supposed to work in .NET 5.

My main function currently looks like this:

public static async Task Main()
{
    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults(x => 
            x.UseDefaultWorkerMiddleware())
        .ConfigureAppConfiguration((_, builder) => builder
            .AddJsonFile("local.settings.json", true)
            .Build())
        .ConfigureServices(ConfigureServices)
        .Build();

    await host.RunAsync();
}

My project is located here, in case someone are interested: https://github.com/sponsorkit/sponsorkit.io

Currently, my .NET 5 work is on a branch called feature/signup-flow.

like image 397
Mathias Lykkegaard Lorenzen Avatar asked Apr 11 '21 13:04

Mathias Lykkegaard Lorenzen


People also ask

Do Azure Functions support .NET 5?

NET Core 3.1 and . NET 5 Azure Functions are. As of March 2021, Microsoft announced that Azure Functions are supported running on . NET 5.

Does Azure Functions support .NET 6?

NET to version 6. You can do that by updating to the new shiny Visual Studio 2022 with built-in SDK or downloading it from here: . NET 6.0 SDK. In Visual Studio 2022, Azure Functions Core Tools, necessary for developing Azure Functions, are available by adding the Azure Cloud Tool in the installation process of VS.

Can we call API from Azure function?

Depending upon your requirement you can create any of supported trigger to trigger your function app and your function app code will make the HTTP calls. You can refer to Call a Web API From a . NET Client (C#) document for more details.


1 Answers

Using IActionResult with Azure Functions in .NET 5?

You can't return IActionResult with Azure Functions in .NET 5. Or more generally, you can't return IActionResult with Azure Functions using the isolated process model. Quote from the docs:

For HTTP triggers, you must use HttpRequestData and HttpResponseData to access the request and response data. This is because you don't have access to the original HTTP request and response objects when running out-of-process.

Instead of IActionResult, you need to return HttpResponseData. Example code is here.

like image 92
Stephen Cleary Avatar answered Sep 20 '22 12:09

Stephen Cleary