My basic wireframe for Azure Function v3
Photo by Oscar Nord on Unsplash

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".

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

Now that we have the project open and ready to go, we see the following screen. The project is now ready to be run.

No alt text provided for this image

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.

No alt text provided for this image

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".

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Once you have created the new function, right-click on the folder called "PracticeFunction" and hover "Add" and click on "Class...".

No alt text provided for this image


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.

No alt text provided for this image

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:

No alt text provided for this image

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.

No alt text provided for this image

We will fix the red like by going to the "TestFunction.cs" file and making the changes as seen in the images below:

No alt text provided for this image

To

No alt text provided for this image

And now all errors have gone!

No alt text provided for this image

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...".

No alt text provided for this image

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.

No alt text provided for this image

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".

No alt text provided for this image

Now the startup class requires a few modifications before it is going to be usable. The "Startup.cs" should look like this.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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

Cristinel Marcu

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 .

要查看或添加评论,请登录

Declan Taggart的更多文章

  • Bicep - Parameter Decorators

    Bicep - Parameter Decorators

    In my previous article, I covered Parameters and Variables and how to use them. This article is a continuation of my…

  • Bicep - Parameters and Variables

    Bicep - Parameters and Variables

    In my previous article, I covered the creation of a basic Bicep file with the commands that are required and a…

  • Create your first Bicep file

    Create your first Bicep file

    What is Bicep? Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure Resources. In a…

  • Basic unit testing with xUnit

    Basic unit testing with xUnit

    Unit testing has many benefits and will help you identify bugs and problem areas in your code. If we look at the…

  • Dependency injection in Azure Function v3

    Dependency injection in Azure Function v3

    In this article, I am going to show how DI (Dependency Injection) can easily be achieved within an Azure Function…

    2 条评论

社区洞察

其他会员也浏览了