Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure functions - should functions be written inside static classes

Tags:

I'm starting to try out Azure functions. I'm using Visual Studio 2017 Preview version 15.3. When I right click on the Azure Functions project I created, and select Add>New Item...>Azure Function, the default template Visual Studio generates is of a public static class with a public static async Task method (the function).

Does the class need to be static (I changed it to non-static and it seems to work)? Is that a best practice for Azure functions? If that is the case, what problems might rise by using a non-static class to hold the Azure Function method?

like image 874
bjd54321 Avatar asked Jul 18 '17 13:07

bjd54321


People also ask

Why Azure functions are static by default?

but why it is declared as static by default? By default, whenever you create an Azure Function, it comes in a template with a static class and a static method. Microsoft enabled instance methods for Azure Functions, now we are able to have non-static classes and methods for our functions.

What is the difference between Azure functions and durable functions?

Durable Functions is an extension of Azure Functions. You can use Durable Functions for stateful orchestration of function execution. A durable function app is a solution that's made up of different Azure functions. Functions can play different roles in a durable function orchestration.

Are static functions better?

They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.


2 Answers

As always it depends. Among other answers which stay to keep function class static, I would like to present another case where the non-static class can be helpful. Please look at: Functions dependency injection. If you want use DI inside your functions there is no easy way to do it with static class because it's can not have instance constructors with parameters. So you are not able to write something like this:

public class HttpTrigger
{
    private readonly IMyService _service;
    private readonly HttpClient _client;

    public HttpTrigger(IMyService service, IHttpClientFactory httpClientFactory)
    {
        _service = service;
        _client = httpClientFactory.CreateClient();
    }

    [FunctionName("GetPosts")]
    public async Task<IActionResult> Get(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "posts")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        var res = await _client.GetAsync("https://microsoft.com");
        await _service.AddResponse(res);

        return new OkResult();
    }
}
like image 81
krypru Avatar answered Sep 26 '22 06:09

krypru


Given that the functions are invoked in a serverless fashion, static methods have the correct semantics here, i.e., you should assume the process can exit after every function invocation, and so you shouldn't be accumulating state on instance methods in between function invocations.

That said, we are investigating Dependency Injection.

like image 43
Mike S Avatar answered Sep 26 '22 06:09

Mike S