Azure Function: Difference between IWebJobsStartup and FunctionsStartup

Viewed 349

I am using Azure Function(v3), I can register dependencies in startup.cs with both IWebJobsStartup and FunctionsStartup.

But which one we should use?

Registerd dependencies with IWebJobsStartup

[assembly: WebJobsStartup(typeof(Startup))]
namespace Notifications.Receiver
{
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddTransient<IEventValidator, EventValidator>();
            builder.Services.AddTransient<IEventReceiverHandler, EventReceiverHandler>();

            builder.Services.AddTransient<IEventHandler<InvoiceResultDto,InvoiceMessageEvent>, InvoiceMessageEventHandler>();
            builder.Services.AddTransient<IEventHandler<InvoiceResultDto, InvoiceFileEvent>, InvoiceFileEventHandler>();
            builder.Services.AddSingleton<IMessageBusFactory, AzureServiceBusFactory>();

            builder.Services.Configure<KestrelServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });
        }
    }
}

Same dependencies I can also register with FunctionsStartup

[assembly: FunctionsStartup(typeof(Startup))]
namespace Notifications.Receiver
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddTransient<IEventValidator, EventValidator>();
            builder.Services.AddTransient<IEventReceiverHandler, EventReceiverHandler>();

            builder.Services.AddTransient<IEventHandler<InvoiceResultDto,InvoiceMessageEvent>, InvoiceMessageEventHandler>();
            builder.Services.AddTransient<IEventHandler<InvoiceResultDto, InvoiceFileEvent>, InvoiceFileEventHandler>();
            builder.Services.AddSingleton<IMessageBusFactory, AzureServiceBusFactory>();

            builder.Services.Configure<KestrelServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });
        }
    }
}
1 Answers

AFAIK, in Azure Functions the preferred way now to do dependency injection is by using the FunctionsStartup.

Yes, we have to implement the Configure(IWebJobsBuilder builder) method if we implement the IWebJobsStartup interface in Startup.cs class.

Whereas the current MSFT Doc says that the Startup class to inherit from FunctionsStartup class and also the Configure Method takes in an IFunctionsHostBuilder.

This similar thread explains the problems of reading HTTP Headers on the request in using FunctionsStartup vs IWebJobsStartup in Azure Functions .NET Applications and finally suggest the using FunctionsStartup is the better approach.

Related