This Azure tutorial will discuss the simple steps to integrate dependency injection into Azure Functions with an example.
Azure Function Dependency Injection Example
Less discuss an example.
The scenario I’m implementing Dependency Injection is I will create TSinfoEmployeeDetails Azure Function App, where we will validate whether the Employee belongs to TSinfo based on the Email ID they provide. If the Email ID contains @tsinfo.com, then the email ID is valid, and the employee belongs to the TSinfo; otherwise, the Employee is not part of the TSinfo company.
Before starting the actual functionality, we should know what the prerequisites needed here to work with this functionality.
Prerequisites
Below are the prerequisites needed here to start with the development activity.
- Make sure you have an Azure Subscription or Azure Account. If you don’t have one, you can create an Azure Free account now.
- You must install Visual Studio 2019 with Azure Development workload on your development machine. If you have not yet installed it, you can install Visual Studio 2019 on your machine now.
- Basic Knowledge of C# language.
Assuming you have all the prerequisites needed here, let’s start with the functionality by creating a new Azure Function Project. We will discuss the simple steps to add Dependency Injection in Azure Functions with C#.
Creating an HTTP trigger Azure function
1. Open Visual Studio 2019 and click on the Create a new Project button, as shown below.

2. Now search for the Azure Functions template, choose the Azure Functions on the Create a new project window, and then click on the Next button to go to the next screen.

3. Enter your Project name, choose a location for your Azure function project On the Configure Your New Project window, and then click on the Create button.

4. We need to choose the trigger for our Azure Function; let’s choose the Http trigger as the trigger option, select the Storage Emulator For the Storage Account (AzureWebJobsStorage) option, and choose the Function as Authorization level. On the Create a new Azure Functions Application window. Now click on the Create button to create the Azure Function as shown below.

6. You can see below that the project was created successfully.

7. Now, let’s create our Interface. Add a class named IEmployeeService.cs and then click on the Add button.

Now add the below lines of code in the IEmployeeService.cs file
namespace TsinfoEmployeeDetails
{
public interface IEmployeeService
{
string EmployeeEmail (string employeeEmailID);
}
}

8. Now, add a class named as EmployeeService.cs

Now add the below lines of code in the EmployeeService.cs file
using System;
using System.Collections.Generic;
using System.Text;
namespace TsinfoEmployeeDetails
{
public class EmployeeService : IEmployeeService
{
public string EmployeeEmail(string employeeEmailID)
{
if (employeeEmailID != null && employeeEmailID.Contains("@tsinfo.com"))
return "Valid EmailID";
else
return "Invalid EmailID";
}
}
}

9. The next step is adding a Startup assembly class file to the Project.
Before adding that class file, we need to install the Below Nugget packages as the dependencies as the prerequisites
- Azure.Functions.Extensions (Latest Version)
- Microsoft.NET.Sdk.Functions (Latest Version)
- Extensions.Http (Latest version)
To add the Azure.Functions.Extensions, Right-click on the project –> Manage Nugget Packages –> search for the Azure.Functions.Extensions and Click on the search result, select the latest version –> Finally, click on the Install button to install the Azure.Functions.Extensions nugget package.

S