bjd54321
bjd54321

Reputation: 462

Azure functions - should functions be written inside static classes

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?

Upvotes: 35

Views: 22782

Answers (3)

krypru
krypru

Reputation: 1742

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

Upvotes: 32

Mike S
Mike S

Reputation: 3169

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.

Upvotes: 7

Amor
Amor

Reputation: 8491

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?

A static class can only contain static members, and it can't be instantiated. Changing the class to non-static will allow you to add non-static members and create an instance of this class.

Please check whether you need to add non-static members to, or create an instance of, this class. Due to the Single Responsibility Principle, which states that every class should have responsibility over a single part of the functionality provided by the software, I suggest you create a new class and put the non-static members there.

If that is the case, what problems might rise by using a non-static class to hold the Azure Function method?

I suppose the reason you want to use a non-static class is that you want to create some non-static members in it. Doing so will make your class complex and difficult to maintain.

My final answer is that the class can be changed to non-static. To keep the class simple, I suggest you keep the class static.

Upvotes: -6

Related Questions