Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 441 forks source link

Integrate Microsoft.Azure.Functions.Extensions with Autofac #4429

Open MauriRojas opened 5 years ago

MauriRojas commented 5 years ago

Is your question related to a specific version? If so, please specify:

Is not related to any version.

What language does your question apply to? (e.g. C#, JavaScript, Java, All)

C#

Question

Is it possible to integrate Microsoft.Azure.Functions.Extensions with Autofac using this approach? The idea is to register the dependencies using Autofac and resolve them in the functions.

So far I've tried using the IServiceCollection.AddAutofac() method of Autofac.Extensions.DependencyInjection with the following implementation:

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddAutofac(b => b.RegisterModule<MyModule>());
        }

But it fails when resolving the registered dependencies in the functions class:

    public class MyFunctions
    {
        private readonly IMyDependency myDependency;

        public QnASynchronizationFunctions(IMyDependency myDependency,)
        {
            this.myDependency = myDependency;
        }

        [FunctionName("MyFunction")]
        public async Task MyFunction([TimerTrigger(KnownTimerConfigs.OnTheHour, RunOnStartup = true)]TimerInfo timer)
        {
             myDependency.Handle();
        }
ranouf commented 5 years ago

Hi, I have the same issue, I'm interested to get the answer too.

Error message returned is:

Executed 'MyFunction' (Failed, Id=1fbfba73-3dc8-482d-b71a-0a3a02e61481) [29-May-19 18:45:44] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'MyProject.Services.IMyService' while attempting to activate 'MyProject.MyFunction'.