AutoFac Lifetime scopes for Isolated Azure Functions

30 Views Asked by At

We are looking to migrate several existing Azure Function apps that use AutoFac for DI from the in-process model to the isolated model.

As part of our current apps, we have some code taken from this AutoFac documentation to handle management of AutoFac lifetime scopes. However, the interface IJobActivatorEx (and presumably runtime behavior) used in the article comes from the package Microsoft.Azure.WebJobs.Host. In Microsoft's Guide for Upgrading to the isolated model, they state

Your isolated worker model application should not reference any packages in the Microsoft.Azure.WebJobs.* namespaces or Microsoft.Azure.Functions.Extensions. If you have any remaining references to these, they should be removed.

How do you establish the lifetime scope for AutoFac when running in the Isolated model? Is is still necessary to have code to establish the lifetime scope in our app or can this be removed and is no longer necessary? If it is still necessary to establish the lifetime scope in the isolated runtime, how can I do it without IJobActivatorEx ?

At present, our app Program.cs look like this

var host = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services => {
  //is this code in ConfigurServices() still necessary 
  //in an isolated function app in .net 6 or later?
  services.AddScoped<LifetimeScopeWrapper>();                
  services.Replace(ServiceDescriptor.Singleton(typeof(IJobActivator), typeof(AutofacJobActivator)));
  services.Replace(ServiceDescriptor.Singleton(typeof(IJobActivatorEx), typeof(AutofacJobActivator)));
});

Additional references https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting

0

There are 0 best solutions below