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

Handlers are resolved from root scope when using Autofac #25

Open
aspnet-hello opened this issue Mar 13, 2018 · 3 comments
Open

Handlers are resolved from root scope when using Autofac #25

aspnet-hello opened this issue Mar 13, 2018 · 3 comments

Comments

@aspnet-hello
Copy link

From @robpex on Monday, September 18, 2017 8:25:29 AM

I believe this is the same question that was brought up in issue #22, but I didn't want to reopen an old issue. In that issue, @cwoolum posted some code they used to work around the issue by resolving things from the AutofacWebRequest lifetime scope, but I had some trouble getting that working in my application. I put some debugging around Autofac resolving components, and I noticed that my WebHookHandler is resolved from the root scope rather than AutofacWebRequest and, according to http://docs.autofac.org/en/latest/lifetime/ ...

It is important to always resolve services from a lifetime scope and not the root container.

As a result, the rest of my dependencies are also resolved from root which isn't desired. This seems like something that should be handled via WebHooks, perhaps in one of the registration extensions? Hacking around to get it working with Autofac seems like more work than just implementing web hooks the traditional way so it's difficult to justify using the project without this fixed.

Copied from original issue: aspnet/WebHooks#157

@aspnet-hello
Copy link
Author

From @dougbu on Tuesday, November 28, 2017 3:40:51 PM

@robpex what would need to change in ASP.NET WebHooks itself? The only mention of Autofac I can see is in a sample.

FYI that sample is now found under https://github.com/aspnet/WebHooks/tree/dev/samples/GenericReceivers.DependencyInjection and Autofac is used in its WebApiConfig class.

@aspnet-hello
Copy link
Author

From @robpex on Tuesday, November 28, 2017 5:06:43 PM

@dougbu it's been a while, but I tried refreshing my memory from #22 (I think that issue has more detail if you haven't checked it out yet). From @HenrikFrystykNielsen comment on that issue,

DI is very much supported. In fact, all of WebHooks extensions (handlers, receivers, and a bunch more) are wired up so that it first look in DI and only if nothing is there does it fall back on it's default behavior.

So, I suspect that somewhere in those WebHooks extensions, where it "first looks in DI", is where it's resolving from the root scope. But I don't remember whether or not I actually stepped through that code myself. I realize that's not the most helpful, so I'll have another look to see if I can reproduce this again... but I might not have the time to do it until the new year.

@aspnet-hello
Copy link
Author

From @dougbu on Tuesday, December 5, 2017 3:55:21 PM

@robpex if we're understanding this issue correctly, one option may be to use ASP.NET's GetDependencyScope(...) extension method when resolving handlers. But, changing the handler resolution pattern i.e. which IDependencyScope we use would be a breaking change -- unfortunately a subtle break. We don't think that's the right approach but an opt-in switch makes it an interesting feature.

We do not plan to make any changes here. But, I'm leaving it open and marking it as up-for-grabs.

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

No branches or pull requests

1 participant