Viktor Shevchenko
Viktor Shevchenko

Reputation: 33

.Net Core 2.1 + MassTransit - Cannot access a disposed object.Object name: 'IServiceProvider'

I am working on the app using .NET Core 2.1 and MassTransit. I get follow error:

Object name: 'IServiceProvider'., System.ObjectDisposedException: Cannot access a disposed object.

In ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
        services.InitializeTelegramBot(Configuration);
        //RabbitMQ register consumer
        services.Configure<RabbitMqConfiguration>(Configuration.GetSection("RabbitMqConfiguration"));
        services.AddMassTransit(configurator =>
        {
            configurator.AddConsumer<Sender>();
        });
        services.AddScoped<Sender>();
        //services.AddSingleton<IApplicationLifetime, ApplicationLifetime>();
}

In Configure

public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider, IHostingEnvironment env, IApplicationLifetime applicationLifetime)
{
        //RabbitMq register msg bus
        var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
        {
            var rabbitOptions = new RabbitMqConfiguration();
            this.Configuration.GetSection("RabbitMqConfiguration").Bind(rabbitOptions);
            IRabbitMqHost host = cfg.Host(new Uri(rabbitOptions.HostAdress), _ =>
            {
                _.Username(rabbitOptions.Username);
                _.Password(rabbitOptions.Password);
            });
            cfg.ReceiveEndpoint(host, rabbitOptions.TerminalCfgEndpoint, conf =>
            {
                conf.LoadFrom(serviceProvider);
            });
            cfg.PrefetchCount = 1;//Consume messages 1 by 1
        });
        applicationLifetime.ApplicationStarted.Register(bus.Start);
        applicationLifetime.ApplicationStopped.Register(bus.Stop);
}

Full StackTrace

 Object name: 'IServiceProvider'., System.ObjectDisposedException: Cannot 
    access a disposed object.
    Object name: 'IServiceProvider'.
    at 
 Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at MassTransit.ExtensionsDependencyInjectionIntegration.DependencyInjectionConsumerScopeProvider.MassTransit.Scoping.IConsumerScopeProvider.GetScope[TConsumer,T](ConsumeContext`1 context)
   at MassTransit.Scoping.ScopeConsumerFactory`1.Send[TMessage](ConsumeContext`1 context, IPipe`1 next)
   at MassTransit.Pipeline.Filters.ConsumerMessageFilter`2.GreenPipes.IFilter<MassTransit.ConsumeContext<TMessage>>.Send(ConsumeContext`1 context, IPipe`1 next)

I will appreciate any help. I hope it's clear what I am asking about.

Upvotes: 0

Views: 2194

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33457

MassTransit container configuration is well documented.

There is also a configuration section for .NET Core.

Also, LoadFrom is no longer supported in modern versions of MassTransit.

Upvotes: 0

Related Questions