Azure - functions must be written inside static classes

I am starting to test Azure Functions. I am using Visual Studio 2017 Preview version 15.3. When I right click on the Azure Functions project that I created and select Add> New Item ...> Azure Function, the default template generated by Visual Studio has public static class

with a method public static async Task

(function).

Does the class have to be static (I changed it to non-static and it seems to work)? Is this best practice for Azure? If so, what problems might arise when using a non-static class to store an Azure Function method?

+5


source to share


3 answers


Does the class have to be static (I changed it to non-static and it seems to work)? Is this the best method for Azure Functions?

A static class can only contain static members and cannot be created. Changing a class to non-static will allow you to add non-static members and create an instance of that class.

Please check if you need to add non-static members or instantiate this class. Because of the Single Responsibility Principle, which says that each class should be responsible for one piece of functionality provided by software, I suggest you create a new class and put non-static members in there.



If so, what problems might arise when using a non-static class to store an Azure Function method?

My guess is that the reason you want to use a non-static class is because you want to create multiple non-static members in it. This will make your class difficult and difficult to maintain.

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

+2


source


Considering that functions are called without a server, the static methods here have the correct semantics, i.e. You should assume that the process can terminate after each function call, and therefore should not accumulate state in instance methods between function calls.



However, we are investigating dependency injection .

+2


source


The static method is really needed.

The static class is optional and sane by default. I can't think of a good reason not to make it static, but if you have one, no one can stop you. Just make sure it doesn't embarrass your teammates.

+1


source







All Articles