Dependency injection in .NET Azure Functions


 

Dependency injection in .NET Azure Functions



Azure Functions supports the dependency injection (DI) software design pattern, which is a technique to achieve Inversion of Control (IoC) between classes and their dependencies.
Dependency injection in Azure Functions is built on the .NET Core Dependency Injection features.

1.Register services

To register services, create a method to configure and add components to an IFunctionsHostBuilder instance. The Azure Functions host creates an instance of IFunctionsHostBuilder and passes it directly into your method.

To register the method, add the FunctionsStartup assembly attribute that specifies the type name used during startup.

using DemofunctionApp.Services.CronService;
using DemofunctionApp.Services.DapperService;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
[assembly: FunctionsStartup(typeof(DemofunctionApp.Startup))]
namespace DemofunctionApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<IDataCronService, DataCronService>();
            builder.Services.AddSingleton<ICronDapperService, CronDapperService>();
        }
    }
}


2.Calling Service Method

In our Function1 method we have injected the dependency  for IDataCronService and we are calling the method PoolingBatchProcess.

using System;
using DemofunctionApp.Services.CronService;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace DemofunctionApp
{
    public class Function1
    {
        private readonly IDataCronService _dataCronService;
        public Function1(IDataCronService dataCronService)
        {
            _dataCronService = dataCronService;
        }
        [FunctionName("Function1")]
        public void Run([TimerTrigger("%Time%")] TimerInfo myTimer, TraceWriter log, ExecutionContext context)
        {
            _dataCronService.PoolingBatchProcess(context, log);
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
        }
    }
}


We have also a service model PoolingAPIResponse

using System;
using System.Collections.Generic;
using System.Text;

namespace DemofunctionApp.Model
{
    public class PoolingAPIResponse
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Our cronservice interface is here

using DemofunctionApp.Model;
using Microsoft.Azure.WebJobs.Host;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DemofunctionApp.Services.CronService
{
    public interface IDataCronService
    {
        Task<List<PoolingAPIResponse>> PoolingBatchProcess(Microsoft.Azure.WebJobs.ExecutionContext executionContext, TraceWriter log);
    }
}

and the DataCronService class is implimenting the IDataCronService interface

using Dapper;
using DemofunctionApp.Model;
using DemofunctionApp.Services.DapperService;
using Microsoft.Azure.WebJobs.Host;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
namespace DemofunctionApp.Services.CronService
{
   
    public class DataCronService : IDataCronService
    {
        private readonly ICronDapperService _cronDapperService;
        public DataCronService(ICronDapperService cronDapperService)
        {
            _cronDapperService = cronDapperService;
        }
        public async Task<List<PoolingAPIResponse>> PoolingBatchProcess(Microsoft.Azure.WebJobs.ExecutionContext context, TraceWriter log)
        {
            int classId = 1;
            DynamicParameters dynamicOffsetListParameters = new DynamicParameters();
            dynamicOffsetListParameters.Add("@classId", classId, System.Data.DbType.Int32, System.Data.ParameterDirection.Input, null);
            var listOffsetModel = await _cronDapperService.GetAll<PoolingAPIResponse>("proc_GetStudentList", dynamicOffsetListParameters);
            return listOffsetModel.ToList();
        }
    }
}
On the above code we are using DynamicParameters  and also passing the data by calling the dapper service method.
await _cronDapperService.GetAll<PoolingAPIResponse>("proc_GetStudentList", dynamicOffsetListParameters);






Comments

Popular posts from this blog

Azure Functions CRUD operation using Dapper Service

Top Microsoft Technology Trends 2022