My basic wireframe for Azure Function v3
In this article, I will show you the wireframe I use for every Azure Function that I create and use. This demo will be an HTTP Trigger which means that it will receive an HTTP request, process it, and then return a result.
First, we will create a new project, in the "Search for templates" textbox, enter the following: Azure. Your first result should be "Azure Functions".
Upon pressing "Next" you will then have to name the project, for this instance, I will be calling it: V3.Practice. Press "Create" once you have finished naming it.
On the next screen, by default, the version of .Net Core is .Net Core 3 (Long Term Support). We can leave this as-is for now as .Net Core 3 is perfectly adequate for what we need. In the article today, I will be using an HTTP trigger with OpenAPI as OpenAPI is an extremely powerful tool, more can be found here. Click "Create" and the project will open once Visual Studio has created the project.
Now that we have the project open and ready to go, we see the following screen. The project is now ready to be run.
There are a few things we will need to do in order to set up the project correctly. Firstly, we will create a folder structure as seen directly below this text. This is achieved by adding folders to the project, not the solution.
The task we have is to delete Function1.cs, once you have deleted it, right-click on the folder called "PracticeFunction", hover "Add" and select "New Azure Function...". We will name it: "TestFunction.cs". Click "Add" once you have finished naming it and then select "HTTP trigger with OpenAPI" and click "Add".
Once you have created the new function, right-click on the folder called "PracticeFunction" and hover "Add" and click on "Class...".
This class is going to handle the dependency injection (DI) for the "TestFunction" function. I prefer to segregate it so that I can maintain a clean and neat function class while having the constructor and variables in their own partial class. We are going to call the new class: "TestFunction.DI.cs". This naming convention helps us see that this is the Dependency Injection class for the TestFunction. Click "Add" to add the class.
Once the class has been created, you will see the page display and an error on the name of your class. If you hover it, you will see the following:
This is expected, what we need to do now is make this class a public partial class and add a constructor so that it looks like the image below.
We will fix the red like by going to the "TestFunction.cs" file and making the changes as seen in the images below:
领英推荐
To
And now all errors have gone!
Before the next class is added, we will need to add a reference to a NuGet package to enable the next step to work. Right-click the project and click "Manage NuGet Packages...".
Now that the NuGet manager is open, navigate to "Browse" and search the following: Microsoft.Azure.Functions.Extensions. Install the package by Microsoft using latest stable and accept the MIT license. Navigate back to "Installed" and it should look like this.
Next, the startup class needs to be added, this will allow us to register any interfaces and services as singletons/scoped instances.
For more on the startup options, click here. Right-click on the project, hover "Add" and click "Add a new class..." and name it "Startup.cs".
Now the startup class requires a few modifications before it is going to be usable. The "Startup.cs" should look like this.
Create a new line above the namespace and paste the following code:
[assembly: FunctionsStartup(typeof(SP.Customer.IoC.Startup))]
Make the class "public" and make it inherit from "FunctionsStartup".
public class Startup? : FunctionsStartup
{
}
The class should look as follows.
Now, to get rid of the red line under the class name, the following code is to be placed inside the class.
public override void Configure(IFunctionsHostBuilder builder)
{
}
Inside the Configure method, the startup can be configured, from adding a database to adding logging. Custom services can be built and registered in this file as well. The "Startup.cs" should look like the following.
Thank you for following this tutorial on how I like to set up my Azure Function Projects, in the next tutorial I will cover adding an Interface to the Startup and how to consume that Interface via DI (Dependency Injection). Please leave feedback so I can know where to improve on these articles!
Until next time!
Declan
Senior Software Developer
3 年Probably the most coherent and simple explanation for the DI in Azure Functions. Can we use this base template on micro-service architecture? Can you give us a template of such implementation? I heard that we cahave serveless architecture with Azure Functions! Thank you for sharing this .