question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integrate external DI scope for ISendEndpointProvider.

See original GitHub issue

MT 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:closed
  • Created 3 years ago
  • Comments:52 (40 by maintainers)

github_iconTop GitHub Comments

1reaction
phatboygcommented, Apr 28, 2020

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

1reaction
NooNameRcommented, Apr 28, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration
Adding MassTransit, as shown above, will configure the service collection with ... IReceiveEndpointConnector (singleton); ISendEndpointProvider (scoped) ...
Read more >
Cannot resolve scoped service from root provider .Net Core 2
Another way to get the instance of scoped dependency is to inject service provider ( IServiceProvider ) into the middleware constructor, create ...
Read more >
Dependency injection guidelines - .NET
Discover effective dependency injection guidelines and best ... Use the factory pattern to create an instance outside of the parent scope.
Read more >
Dependency Injection Scopes in Blazor
Scopes are used to control the lifetime of objects retrieved from the DI container. The default DI system from Microsoft in ASP.NET Core...
Read more >
How do I work with per-request lifetime scope?
Under the web integration always request dependencies from the dependency resolver or the request lifetime scope, never from the container itself. What this ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found