Integrate external DI scope for ISendEndpointProvider.
See original GitHub issueMT provides great integration for DI containers when run as receive endpoint. Custom filters added to consume pipeline can make use of services accessible form IServiceProivder
. They’re also ran in within scope of DI container. Filters added to send/publish pipelines can also make good use of those if they’re triggered from consume pipeline as they have access to wrapping ConsumeContext
and its IServiceProvider
. However when using a send-only bus, where we publish something from DI top-level scope (e.g. ASP.NET HostedService) or from non-MT initiated DI scope (e.g. ASP.NET HTTP Controller) using IBus
send/publish filters can’t access ConsumeContext
as such does not exist.
I’m wondering if/how we could bring DI-scoped IServiceProvider
to send filters in such a case? Consider following ASP.NET Controller:
public class MyController : ApiController
{
private readonly IBus bus;
// constructor
public async Task<string> DoSth()
{
await bus.Publish(...); // should behave as current implemention
await bus.AbcScoped().Publish(...); // could run IServiceProivder.CreateScope() under the hood
await bus.AbcExternallyScoped(RequestServices).Publish(...); // could run with externally provided `IServiceProvider`
return "OK";
}
}
This would require some “proxy ISendEndpointProivder/IPublishEndpointProvider” to be returned by IBus.Abc...Scoped()
which would set up DI scope and before sending anything through send/publish pipelines I guess?
Issue Analytics
- State:
- Created 3 years ago
- Comments:52 (40 by maintainers)
@kooshan actually, it does, you just call
GetResponse<T1>(false)
,GetResponse<T2>()
which then sends the response, but you are right, the syntax isn’t the same but the net result is. I wonder if this is poorly documented. Anyway.@kooshan , should be fixed by: https://github.com/MassTransit/MassTransit/pull/1841