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.

UseServiceScope in AspNetCore DI fails with exception

See original GitHub issue

Is this a bug report?

Yes

Can you also reproduce the problem with the latest version?

Yes, it fails in both the latest release and the current preview.

Environment

  1. Operating system: Windows 10
  2. Visual Studio version: VS 2017
  3. Dotnet version: .NET Core 2.2

Steps to Reproduce

(Write your steps here:)

  1. Using startup like this example
         services.AddMassTransit(provider => 
                Bus.Factory.CreateUsingAzureServiceBus(cfg =>
                {
                    cfg.UseServiceScope(provider);

                    var host = cfg.Host(Configuration.GetValue<string>("ServiceBus:ConnectionString"), sbcfg => { });

                    cfg.ReceiveEndpoint("test-queue", e =>
                    {
                        e.UseMessageRetry(c => c.Immediate(5));
                        e.Consumer<TestConsumer>(provider);
                    });
                }),
                x =>
                {
                    x.AddConsumer<TestConsumer>();
                }
            );

  1. Publish a message to the queue.
  2. The consumer will fail with

fail: MassTransit.Messages[0] R-FAULT sb://edgepoc.servicebus.windows.net/test-queue 36040000-dfb5-5428-cb4e-08d6bdeba4c8 Common.ITestMessage TestUseServiceScope.Worker.TestConsumer(00:00:00.0004802) The ConsumeContext was already set. System.InvalidOperationException: The ConsumeContext was already set. at MassTransit.Scoping.ScopedConsumeContextProvider.SetContext(ConsumeContext context) at MassTransit.ExtensionsDependencyInjectionIntegration.ScopeProviders.DependencyInjectionConsumerScopeProvider.MassTransit.Scoping.IConsumerScopeProvider.GetScope[TConsumer,T](ConsumeContext`1 context)

  1. Removing the UseServiceScope line stops this behaviour.

Expected Behavior

Message consumed successfully.

Actual Behavior

Failed with

fail: MassTransit.Messages[0] R-FAULT sb://edgepoc.servicebus.windows.net/test-queue 36040000-dfb5-5428-cb4e-08d6bdeba4c8 Common.ITestMessage TestUseServiceScope.Worker.TestConsumer(00:00:00.0004802) The ConsumeContext was already set. System.InvalidOperationException: The ConsumeContext was already set. at MassTransit.Scoping.ScopedConsumeContextProvider.SetContext(ConsumeContext context) at MassTransit.ExtensionsDependencyInjectionIntegration.ScopeProviders.DependencyInjectionConsumerScopeProvider.MassTransit.Scoping.IConsumerScopeProvider.GetScope[TConsumer,T](ConsumeContext`1 context)

Reproducible Demo

https://github.com/steverash/TestUseServiceScope

Included a demo aspnetcore app to show the startup and consumer. Also a console app to publish a message to test.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexeyzimarevcommented, May 14, 2019

@Crozin the less I use DI containers in my code, the better I sleep at night…

0reactions
phatboygcommented, Apr 22, 2019

@Crozin doubtful, as those type of abstractions end up creating more hassle than they’re worth, particularly when there is already the Microsoft.Extensions abstraction, which if anything would be the one supported somehow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core - How to handle exception inside a scoped ...
I have some repository classes added as scoped service and I retrieve a particular repository using: var rep = HttpContext.RequestServices.
Read more >
Error Handling and ExceptionFilter Dependency Injection for ...
The best way to handle this in your ASP.NET Core API is to create an ExceptionFilter that intercepts all exceptions and then returns...
Read more >
Dependency injection in ASP.NET Core
Inject the service into the middleware's Invoke or InvokeAsync method. Using constructor injection throws a runtime exception because it forces ...
Read more >
Using scoped services inside singletons - Sam Walpole
The exception appears because the DI container is trying to protect you from Captive Dependencies (see the disclaimer above). While it is ...
Read more >
New dependency injection features in .NET 6
In this post I talk about some of the new features added to the DI libraries in .NET 6, as well a performance...
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