Skip to content
This repository has been archived by the owner on Oct 18, 2018. It is now read-only.

Handlers are resolved from root scope when using Autofac #157

Closed
robpex opened this issue Sep 18, 2017 · 4 comments
Closed

Handlers are resolved from root scope when using Autofac #157

robpex opened this issue Sep 18, 2017 · 4 comments

Comments

@robpex
Copy link

robpex commented Sep 18, 2017

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.

@dougbu
Copy link
Member

dougbu commented Nov 28, 2017

@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.

@robpex
Copy link
Author

robpex commented Nov 29, 2017

@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.

@dougbu
Copy link
Member

dougbu commented Dec 5, 2017

@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.

@aspnet-hello
Copy link

This issue was moved to aspnet/AspNetWebHooks#25

@aspnet aspnet locked and limited conversation to collaborators Mar 13, 2018
@aspnet-hello aspnet-hello removed this from the Backlog milestone Mar 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants