3

Why Azure function class is static by default?

    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
        ILogger log)

Is it because, Azure function based out of serverless architecture, using static or single class instance should result in saving compute resources? or 'static' decision is made for any other technical reasons?

I know Azure function class can be non-static as well from 2019 onwards but here my question is different what is Microsoft's technical decision behind, declaring the Azure function class as static by default. Knowing this reason or justification would be helpful in my software designing as well. Curious.

Again re-iterating my question as some of the folks answering Azure function class can also be non-static. My question is NOT whether Azure function class can be non-static? but why it is declared as static by default?

191180rk
  • 735
  • 2
  • 12
  • 37
  • 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. If you want to use Dependency Injection via constructor,you cane use non-static. – Harshitha Veeramalla Jan 20 '22 at 15:45
  • Does this answer your question? [Can I create non static Azure function class in C#, what are the consequences?](https://stackoverflow.com/questions/59821026/can-i-create-non-static-azure-function-class-in-c-what-are-the-consequences) – Peter Bons Jan 20 '22 at 15:46
  • 1
    @PeterBons, I know Azure function class can be non-static from 2019 onwards but here my question is different what is Microsoft's technical decision behind by declaring the Azure function class as static by default. Knowing this reason or justification would be helpful in my software designing. – 191180rk Jan 20 '22 at 15:54
  • *Knowing this reason or justification would be helpful in my software designing.* not sure how, Azure Function is a cloud service that abstracts away the complexity needed to support scaling & hosting for example. Your architecture should be independant of the implementation of the cloud service. – Peter Bons Jan 20 '22 at 15:59
  • The samples don't do any complex job, so the default static method is fine for them. Based on your requirement you can use non-static method. – Harshitha Veeramalla Jan 20 '22 at 16:04
  • 1
    There isn't a technical reason. Azure Functions meant to be simple from a developers perspective. A simple static class with a simple static function did provide an easy starting point. Currently when using constuctor based DI you need a startup class, register the dependencies etc. It is al about complexity from a developers perspective. That is also described by team members [here](https://github.com/Azure/azure-functions-host/issues/911) – Peter Bons Jan 20 '22 at 17:24
  • @PeterBons I disagree. Static being sealed by default is one reasoning. But DI has its own overhead on startup costs. And can easily result in unnecessary complexity. Throwing multiple unrelated objects in the same bucket for no other reason than DI is bad architecture. You should use multiple factories in that case. Which static classes encourage. Since you have to query the factory to find the dependency. Rather than dynamic reflection finding and caching it for you (class attribute tying it to a factory would work too - non-static classes could be ok...but they aren't very good as of now) – TamusJRoyce May 15 '23 at 16:50

2 Answers2

1

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.

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.

Knowing this reason or justification would be helpful in my software designing as well.

My concern was that if I have a static class, then all its members must be static. And then such a static variable would be shared across different requests to the function (or functions). So I would be effectively sharing state, while according to this learn.microsoft.com/en-us/azure/azure-functions/…, functions should be written to be stateless.

0

You got it. Exactly right with your first assumption.

Don't change your classes from being static. They are sealed by default. And it improves startup performance. It is not just because of how azure functions were originally designed.

public static class Program {
  public static IHost? host;
  public static async Task Main(string[] args) {
    host = BuildFactory();
  } 
}

then you can call

Program.host.Services.GetService<IInterface>();

to get your singleton from the factory

you should only be putting in configurations and anything with lifetimes that need cleaned up and allocated dynamically. Sql connections, http pools, etc.

Do not put everything you want injected all over the place. Otherwise, a new factories should be created for every responsibility/purpose. And .NET isn't setup to easily allow for that.

TamusJRoyce
  • 817
  • 1
  • 12
  • 25