Azure

How To combine Dependency Injection In Azure Capabilities

On this article, we’re going to learn to combine Dependency Injection into Azure capabilities. We may even create a easy Http set off Azure operate and CustomerService after which we are going to inject the service object into the operate utilizing DI.

 

In case you are new to Azure capabilities then first verify the beneath articles:

On this article, we are going to talk about the beneath matters,

  • What’s Dependency Injection sample?
  • Steps so as to add Dependency Injection in Azure Capabilities.

What’s Dependency Injection sample?

 

Dependency Injection is a software program design sample which is used to makes a category unbiased of its dependencies and helps to achive the Inversion of Management (IOC) and unfastened coupling between courses and their dependencies. 

 

In software program engineering, dependency injection is a method during which an object receives different objects that it relies on. These different objects are known as dependencies. Within the typical “utilizing” relationship the receiving object is known as a consumer and the handed (that’s, “injected”) object is known as a service.

 

To study extra about dependency injection you may check with this article. We are able to add dependency injection in Azure operate much like ASP.NET Core. If you wish to study extra about dependeny injection in ASP.NET core then you may check with this article

 

So on this article, we are going to talk about methods to add dependency injection into Azure capabilities.

 

Steps so as to add Dependency Injection in Azure Capabilities

 

Stipulations

Create a easy HTTP set off Azure operate

 

So open Visual Studio and Go to File -> New -> Challenge. Search “Azure Capabilities” within the search field and choose the Azure operate template and click on on Subsequent.

 

 

Give a reputation to the operate venture and click on on Create.

 

 

Choose the HTTP set off template and set the Authorization degree as Nameless and click on on Create.

 

 

That is it. We have now created our first Azure operate. Open the Function1.cs file to see generated operate.

 

 

Create a easy CustomerService which returns mock buyer information

 

So proper click on on answer -> Add -> New Challenge after which choose .NET Customary class library and click on on Subsequent. Then give title and eventually click on on Create.

 

 

Now create class known as Buyer with the beneath properties,

  1. public class Buyer  
  2. {  
  3.     public string Title { getset; }  
  4.     public int Id { getset; }  
  5.     public string Nation { getset; }  
  6. }   

Now create a interface known as ICustomerService.cs as beneath,

  1. utilizing System.Collections.Generic;  
  2.   
  3. namespace AzureFuncDependencyDemo.Buyer.Service  
  4. {  
  5.     public interface ICustomerService  
  6.     {  
  7.         Checklist<Buyer> GetCustomersData();  
  8.     }  
  9. }  

And at last create a category which implements ICustomerService interface’s “GetCustomersDataAsync” technique and returns an inventory of Prospects.

  1. utilizing System.Collections.Generic;  
  2.   
  3. namespace AzureFuncDependencyDemo.Buyer.Service  
  4. {  
  5.     public class CustomerService : ICustomerService  
  6.     {  
  7.         public Checklist<Buyer> GetCustomersData()  
  8.         {  
  9.             var customersData = new Checklist<Buyer>();  
  10.             customersData.Add(new Buyer()  
  11.             {  
  12.                 Id = 101,  
  13.                 Title = “Customer1”,  
  14.                 Nation = “India”  
  15.             });  
  16.   
  17.             customersData.Add(new Buyer()  
  18.             {  
  19.                 Id = 102,  
  20.                 Title = “Customer2”,  
  21.                 Nation = “USA”  
  22.             });  
  23.             return customersData;  
  24.         }  
  25.     }  
  26. }  

Inject CustomerService into Azure operate to get buyer information

 

First we have to set up the beneath nuget bundle into Azure operate venture,

 

 

 

To inject dependency we first have to create a brand new class known as as “Startup.cs” into the foundation of the venture. Add the beneath code into class which is used at the beginning of the operate app.  

  1. utilizing AzureFuncDependencyDemo;  
  2. utilizing AzureFuncDependencyDemo.Buyer.Service;  
  3. utilizing Microsoft.Azure.Capabilities.Extensions.DependencyInjection;  
  4. utilizing Microsoft.Extensions.DependencyInjection;  
  5.   
  6. [assembly: FunctionsStartup(typeof(Startup))]  
  7.   
  8. namespace AzureFuncDependencyDemo  
  9. {  
  10.     public class Startup : FunctionsStartup  
  11.     {  
  12.         public override void Configure(IFunctionsHostBuilder builder)  
  13.         {  
  14.             builder.Companies.AddTransient<ICustomerService, CustomerService>();  
  15.         }  
  16.     }  
  17. }  
  • Then FunctionStartup attribute is used to register the meeting which specifies the title it is advisable used throughout startup of operate.
  • Provoke  FunctionStartup and implement the Configure technique to register all of the dependencies.
  • Register ICustomerService dependency in the identical means we normally do in ASP.NET core apps.

Now add the beneath code into operate to inject dependency and get the client information.

  1. utilizing System.Threading.Duties;  
  2. utilizing AzureFuncDependencyDemo.Buyer.Service;  
  3. utilizing Microsoft.AspNetCore.Http;  
  4. utilizing Microsoft.AspNetCore.Mvc;  
  5. utilizing Microsoft.Azure.WebJobs;  
  6. utilizing Microsoft.Azure.WebJobs.Extensions.Http;  
  7.   
  8. namespace AzureFuncDependencyDemo  
  9. {  
  10.   
  11.     public class Function1  
  12.     {  
  13.         personal readonly ICustomerService customerService;  
  14.   
  15.         public Function1(ICustomerService _customerService)  
  16.         {  
  17.             customerService = _customerService;  
  18.         }  
  19.   
  20.         [FunctionName(“Function1”)]  
  21.         public  async Process<IActionResult> Run(  
  22.             [HttpTrigger(AuthorizationLevel.Anonymous, “get”, Route = null)] HttpRequest req)  
  23.         {  
  24.             var customersData = await customerService.GetCustomersDataAsync();  
  25.   
  26.             return new OkObjectResult(customersData);  
  27.         }  
  28.     }  
  29. }  

Now to run the operate app we have to simply run the venture. It would then begin Azure operate cli to run the operate.

 

 

The operate is working on ‘http://localhost:7071/api/Function1’

 

 

That is it. We have now now configured dependency injection into Azure operate app. 

 

If we wish to use HttpClient to make a http request then we will inject the HttpClient dependency in the identical means we like including into NET Core. So let’s add the beneath nuget bundle,

 

 

After that open Startup.cs class and add beneath line to inject HttpClient dependency.

  1. utilizing AzureFuncDependencyDemo.Buyer.Service;  
  2. utilizing Microsoft.Azure.Capabilities.Extensions.DependencyInjection;  
  3. utilizing Microsoft.Extensions.DependencyInjection;  
  4.   
  5. [assembly: FunctionsStartup(typeof(AzureFuncDependencyDemo.Startup))]  
  6.   
  7. namespace AzureFuncDependencyDemo  
  8. {  
  9.     public class Startup : FunctionsStartup  
  10.     {  
  11.         public override void Configure(IFunctionsHostBuilder builder)  
  12.         {  
  13.             builder.Companies.AddHttpClient();  
  14.             builder.Companies.AddTransient<ICustomerService, CustomerService>();  
  15.         }  
  16.     }  
  17. }  
Now create one other azure operate and use the HttpClient to fetch person’s information utilizing ‘jsonplaceholder.typicode.com/customers‘ mock api. Proper click on on answer and click on on Add -> New Azure Operate.

 

 

Choose Azure operate and provides it title “GetUsers” and click on on Add button.

 

 

Choose operate set off kind as HttpTrigger and Authorization degree as Nameless. 

 

 

Now add the beneath code to fetch person’s information. 

  1. utilizing System.Web.Http;  
  2. utilizing System.Threading.Duties;  
  3. utilizing Microsoft.AspNetCore.Http;  
  4. utilizing Microsoft.AspNetCore.Mvc;  
  5. utilizing Microsoft.Azure.WebJobs;  
  6. utilizing Microsoft.Azure.WebJobs.Extensions.Http;  
  7. utilizing Microsoft.Extensions.Logging;  
  8.   
  9. namespace AzureFuncDependencyDemo  
  10. {  
  11.     public class GetUsers  
  12.     {  
  13.         personal readonly HttpClient _httpClient;  
  14.         public GetUsers(HttpClient  httpClient)  
  15.         {  
  16.             _httpClient = httpClient;  
  17.         }  
  18.   
  19.         [FunctionName(“GetUsers”)]  
  20.         public async Process<IActionResult> Run(  
  21.             [HttpTrigger(AuthorizationLevel.Anonymous, “get”, Route = null)] HttpRequest req,  
  22.             ILogger log)  
  23.         {  
  24.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  25.   
  26.             var responseMessage = await _httpClient.GetAsync(“http://jsonplaceholder.typicode.com/customers”);  
  27.             var usersData = responseMessage.Content material.ReadAsStringAsync().Outcome;  
  28.             return new OkObjectResult(usersData);  
  29.         }  
  30.     }  
  31. }   

Right here we’ve inject HttpClient dependency utilizing Constructor after which name GetAsync technique to fetch information from api. So run the appliance and see the output.

 

 

Conclusion

 

On this article, we’ve created a brand new Azure operate and easy mock CustomerService from Visual Studio. Additionally, I demonstrated methods to inject CustomerService dependency into Azure operate app and likewise inject it into operate. I actually hope that you simply take pleasure in this text, share it with pals, and please don’t hesitate to ship me your ideas or feedback.

 

Keep tuned for extra Azure Capabilities articles.

 

 

Joyful Coding!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button