Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with Azure durable activity function #15

Closed
goldytech opened this issue Jun 25, 2018 · 19 comments · Fixed by #17
Closed

Not working with Azure durable activity function #15

goldytech opened this issue Jun 25, 2018 · 19 comments · Fixed by #17

Comments

@goldytech
Copy link

goldytech commented Jun 25, 2018

Hi,
I'm using this library in my Azure durable functions app.
I've defined my DIConfig as follows, and when the function app starts I can see the ctor of this class is called with my activity function class name as functionName parameter value.

 public  class DIConfig
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DIConfig"/> class.
        /// </summary>
        /// <param name="functionName">
        /// The function name.
        /// </param>
        public DIConfig(string functionName)
        {
            DependencyInjection.Initialize(
                containerBuilder =>
                    {
                        containerBuilder.RegisterType<AuthenticationService>().As<IOAuthService>();
                        containerBuilder.RegisterType<GenericAPIService>().As<IGenericAPIService>();
                        containerBuilder.RegisterType<EmailTemplateNotificationService>().As<IEmailTemplateNotificationService>();
                        containerBuilder.RegisterType<AicGenericSdk>().As<IAicGenericSdk>();
                       containerBuilder.RegisterType<AgreementExpiryService>().As<IAgreementExpiryService>();
                        containerBuilder.RegisterType<EmailReminderService>().As<IEmailReminderService>();
                    },
                functionName);
        }
    }

Below is the code of my activity function , where I'm want to use DI

  [DependencyInjectionConfig(typeof(DIConfig))]
    public static  class EmailReminderActivities
    {
     
        [FunctionName("A_GetAgreementsForEmailReminder")]
        public static List<AgreementModel> GetAgreementsForEmailReminder(
            [ActivityTrigger] string input,
            TraceWriter log,
            [Inject] IEmailReminderService emailReminderService)
        {
            
          return (List<AgreementModel>)emailReminderService.GetAgreementsForReminderDate(input);
        }
    }

This is the error I'm getting

A ScriptHost error has occurred
[6/25/2018 8:20:44 AM] Exception while executing function: A_GetAgreementsForEmailReminder. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'emailReminderService'. AzureFunctions.Autofac: DependencyInjection.Initialize must be called before dependencies can be resolved.
If I would have not use DI then this is how I would have initialize my EmailReminderService

var emailReminderSvc = new EmailReminderService(new AicGenericSdk(new GenericAPIService(new AuthenticationService()),null ));

I hope I've registered dependencies in the right order.
Please help

@rawrspace
Copy link
Member

rawrspace commented Jun 25, 2018

Do you have a repository you can share that reproduces the issue?

Also validate that AuthenticationService has a parameter less constructor.

That GenericApiService has a constructor that takes an IOAuthService.

That AicGenericSdk has an constructor that takes an IGenericAPIService.

And an EmailReminderService constructor that takes an IAicGenericSdk.

If you don't want to have those constructors take interfaces as the parameters you will need to use builder.Register instead of RegisterType.

@goldytech
Copy link
Author

All the ctors have proper parameters the way you described it.

Here is the code link

https://drive.google.com/file/d/1ZVO98BNwpJcX85iI0AP17MOD10_DVRVj/view?usp=sharing

@fingersmagoo
Copy link

I am experiencing the same issue. (ServiceBus Queue Trigger)

@rawrspace
Copy link
Member

@goldytech It looks like the files have been removed from Google drive. Can you share them via GitHub?
@fingersmagoo Do you have a sample project I can verify this issue with?
Additionally what version of the package are you using and is it a v1(.net framework) or v2(.net core) function? I am currently using durable functions with both http triggers and service bus triggers without this error.

@aboutdomtime
Copy link

I didn't get to look at the source in detail, but I would classify the problem the OP is having as a limitation rather than a bug. The issue he's having would arise in the case of Durable functions more so because there is an increased likelihood of having the pattern class A { function x,y,z } (an orchestrator with multiple nested activity functions). The line if (containers.ContainsKey(functionName)) in DependencyInjection.cs would most likely fail as containers would only contain the name of the Declaring Type as the Key which it would try to match against the functionName which would be different. Also if the class name is different from the functionName it seems like it might fail as well.

@tstone84
Copy link

Is there a workaround for this or will there be a change to allow this to work with Durable Functions? I was planning on using this for my Durable Functions (which I just started building), but obviously will need to find another solution if this one doesn't work with them. Thanks for providing this library!

@rawrspace
Copy link
Member

I have not been able to replicate this issue. I am currently using this package with durable functions and it is working as expected. Can you provide a sample project so that I can validate this issue?

@tstone84
Copy link

@vandersmissenc I haven't experienced an issue yet, but was assuming based on the OP that it didn't support Durable Functions. It's great to hear that that's not the case! I will give it a go. I was just trying to hedge before getting too far down a given path. Thanks again for a great solution!

@tstone84
Copy link

tstone84 commented Jul 31, 2018

@vandersmissenc Just gave it a shot with my Durable Functions and I'm getting this:
Method not found: 'Void AzureFunctions.Autofac.Configuration.DependencyInjection.Initialize(System.Action`1<Autofac.ContainerBuilder>, System.String

It's very possible i'm doing something incorrectly, but I followed the information from the ReadMe.

Any help would be greatly appreciated!

@rawrspace
Copy link
Member

rawrspace commented Jul 31, 2018

Is this an azure functions v1(.net framework) or v2(.net core)? Do you have either the steps to reproduce this or a reproduction repo I can check out? @tstone84

@rawrspace
Copy link
Member

I have been able to replicate this issue locally. The problem appears to be that when the dependency injection config is created it registers for the class and it uses the class name as the key but when the individual method tries to resolve objects it uses the function name. If you only have one function in your class as the name is the same as the class name you would not see this behavior.

I have made an update in the code and I am currently testing to verify it is resolved. I will keep you updated. Thanks for reporting the issue.

rawrspace added a commit that referenced this issue Aug 1, 2018
… name used to resolved is registered at the time of creation instead of the time of injection.
@rawrspace
Copy link
Member

I pushed the fix to nuget as a beta package. I will validate tomorrow that is resolves the issues when installed via nuget and if so I will push the full version and resolve this issue.

@tstone84
Copy link

tstone84 commented Aug 1, 2018

@vandersmissenc Sorry, I wasn't able to respond last night. Thanks for looking into this and providing a fix! I'll pull down the pre-release version and give it a go.

@rawrspace
Copy link
Member

I was testing it this morning and if you create a fresh project and just install Microsoft.Azure.WebJobs.Extensions.DurableTask and AzureFunctions.Autofac then it works fine. There appears to be a conflict with another package because in my more complex solution I am seeing the method not found exception still. After you test it out let me know what you see and if possible the packages you are referencing in your project @tstone84

@rawrspace
Copy link
Member

rawrspace commented Aug 1, 2018

I have narrowed down the issue to Autofac. When I install my external package it installs Autofac 4.6.2 which then produces the method not found exception. I have tried upgrading to 4.8.1 but the same error occurs. I have the package right now relying on Autofac 4.2.1 since that is what Azure Functions Host uses but it seems like I have to set a hard restriction to that version.

@tstone84
Copy link

tstone84 commented Aug 1, 2018

@vandersmissenc Yep, I'm still seeing the issue with the beta package. Originally, I was on 4.8.1 of Autofac and downgraded to 4.2.1 to try to resolve it after seeing some threads online about that issue, but didn't have any success. Again, I appreciate your quick response and you looking into this. Thx!
BTW, I'm currently using v1 of Azure functions because I'm stuck with some .Net Framework internal libs that I can't update to net standard for now.

@rawrspace
Copy link
Member

rawrspace commented Aug 1, 2018

Can you install 3.02-beta04 and see if you still have issues? I was able to remove my dependency on Autofac in my external package and I am no longer having issues with this version of the package. If you are still having issues can you provide a repo project I can check out? Thanks @tstone84

rawrspace added a commit that referenced this issue Aug 3, 2018
* Resolved #15 - Modified the way DI resolves objects so that the class name used to resolved is registered at the time of creation instead of the time of injection.

* Removed auto generation of symbols
@tvperez76
Copy link

It appears that this issue has sprung up again. I am using Autofac 4.9.4, Autofac.Extensions.DependencyInjection 4.4.0 and AzureFunctions.Autofac 3.0.7. Was there a change that caused such a regression?

@rawrspace
Copy link
Member

You must use Autofac 4.2.1 due to restrictions in Azure Functions. Please try with that version and see if you still have problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants