Skip to content

Extension startup hook

Shyju Krishnankutty edited this page May 11, 2022 · 3 revisions

The dotnet-isolated worker programming model supports the ability for worker extensions to participate in the startup routine of the function application, thus enabling extension authors to register additional services relevant to the extension.

Follow the below steps to participate in the startup process

  1. Add a reference to the latest version of Microsoft.Azure.Functions.Worker.Core package in your extension project.

  2. Create a class which derives from WorkerExtensionStartup. Override the Configure method. An instance of IFunctionsWorkerApplicationBuilder will be passed into the Configure method, which can be used to configure the function worker application builder options.

    • The class should be public.
    • The class should have a public parameterless constructor.
  3. Use the WorkerExtensionStartup assembly level attribute to specify the startup implementation class created in step 1.

Here is an example startup implementation which register the StampHttpHeadersMiddleware middleware and the IFooService. You can also see that we are using the WorkerExtensionStartup attribute to specify the type information of our extension startup class.

[assembly: WorkerExtensionStartup(typeof(MyHttpExtensionStartup))]

namespace Microsoft.Azure.Functions.Worker.Extensions.Http
{
    public class MyHttpExtensionStartup : WorkerExtensionStartup
    {
        public override void Configure(IFunctionsWorkerApplicationBuilder applicationBuilder)
        {
            applicationBuilder.UseMiddleware<StampHttpHeadersMiddleware>();
            applicationBuilder.Services.AddSingleton<IMyFooService, MyFooService>();
        }      
    }
}

Testing extensions end to end

When testing the extension with the startup hook in an isolated-worker function application, make sure to use the latest version of the Microsoft.Azure.Functions.Worker.Sdk package.

Clone this wiki locally