Azure

Implementing Dependency Injection In Azure Features

This text talks about how we will use dependency injection in Azure Features. Dependency injection is a really well-known design sample that’s used to implement IoC as gives you a facility to segregate object creation logic from its utilization.

Conditions And Instruments Used

As a way to implement dependency injection in Azure Operate App, you want an lively Azure subscription, Visual Studio (I’m utilizing Visual Studio 2019), and dealing data of the C# language.

Create Operate App in Visual Studio

Step one is to create a brand new challenge in Visual Studio of kind Azure Features:

and choose Http Set off as proven under:

Add Lessons For Dependency Injection

Subsequent we have to add the service (courses) which we wish to inject. For simplicity, we are going to create an interface and a category implementing that interface. This interface can have just one methodology named GetCurrentTime() and it’ll present present time to the caller. Listed below are the definitions:

public interface ICurrentTimeProvider
{
   DateTime GetCurrentTime();
}
public class CurrentTimeProvider : ICurrentTimeProvider
{
     public DateTime GetCurrentTime()
     {
         return DateTime.Now;
     }
}

Add Startup Class To Inject Dependency

We’ll add one other class named Startup which can do all of the dependency mapping activity for us.

Right here we have to pull in two Nuget packages as proven under:

Right here is the code for Startup class:

utilizing Microsoft.Azure.Features.Extensions.DependencyInjection;
utilizing Microsoft.Extensions.DependencyInjection;
utilizing SampleFunctionApp.Companies;
public class Startup : FunctionsStartup
{
   public override void Configure(IFunctionsHostBuilder builder)
   {
      builder.Companies.AddHttpClient();
      builder.Companies.AddSingleton<ICurrentTimeProvider>((s) => { return new CurrentTimeProvider(); });
    }
}

Replace Operate To Accommodate Dependency Injection

utilizing Microsoft.AspNetCore.Http;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Azure.WebJobs;
utilizing Microsoft.Azure.WebJobs.Extensions.Http;
utilizing Microsoft.Extensions.Logging;
utilizing Newtonsoft.Json;
utilizing SampleFunctionApp.Companies;
utilizing System.Web.Http;
utilizing System.Threading.Duties;
public class Function1
{
        non-public readonly HttpClient httpClient;
        non-public readonly ICurrentTimeProvider currentTimeProvider;

        public Function1(IHttpClientFactory httpClientFactory, ICurrentTimeProvider timeProvider)
        {
            httpClient = httpClientFactory.CreateClient();
            currentTimeProvider = timeProvider;
        }

        [FunctionName("Function1")]
        public async Job<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP set off perform processed a request.");
            var currentDateTime = currentTimeProvider.GetCurrentTime();
            StringContent content material = new StringContent(JsonConvert.SerializeObject(currentDateTime.ToString()));
            var response = await httpClient.PostAsync("URL_TO_POST", content material);
            if (response.StatusCode == System.Web.HttpStatusCode.OK)
            {
                return new OkObjectResult(response.Content material);
            }
            else 
            {
                return new BadRequestObjectResult(response.StatusCode);
            }            
        }
}

Hope you loved studying this text. You can even discover the recording of this whole circulate right here.

Show More

Related Articles

Leave a Reply

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

Back to top button