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

Support Scoped dependency injection when underlying container does #1399

Open
hwoodiwiss opened this issue Apr 25, 2024 · 1 comment
Open
Labels
.NET Pull requests that update .net code

Comments

@hwoodiwiss
Copy link
Member

hwoodiwiss commented Apr 25, 2024

Is your feature request related to a problem? Please describe.

When handling messages, I often ending up having to writing factories for cases that in, for example, ASP.NET I'd just be able to use a scoped service, as the DI container will handle disposing services when the scope is disposed at the end of a request.

I feel like that same paradigm can be applied here as well, with the scope wrapping resolution and execution of the handler.

Describe the solution you'd like

When the underlying DI container supports it, I would like to be able to resolve handlers within a service scope so that their dependencies can have their lifetimes managed by the DI service scope.

Describe alternatives you've considered

In the past I've used a wrapper approach, in which the wrapper handler has an IServiceProvider injected, which I then use to create a scope and resolve the actual handler. This approach is quite clunky, and means I still can't use scoped services in the "outer" handler without potentially leaking memory.

@hwoodiwiss hwoodiwiss added help-wanted .NET Pull requests that update .net code and removed help-wanted labels Apr 25, 2024
@hwoodiwiss hwoodiwiss changed the title Support Scoped dependency injection underlying container does Support Scoped dependency injection when underlying container does May 19, 2024
@hwoodiwiss
Copy link
Member Author

I've finally managed to have a look at this. Ideally, I'd wanted to have it so that the whole middleware pipeline could support scoped DI, but it looks like we resolve a lot of that up-front.

I think this might need to be reduced in scope a little to just cover scoped handler resolution, which would then allow dependencies of the handler and it's dependencies to be scoped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Pull requests that update .net code
Projects
None yet
Development

No branches or pull requests

1 participant