.Net Core 2.1 + MassTransit-无法访问已处置的对象。对象名称:“ IServiceProvider”

时间:2018-07-28 20:57:20

标签: c# asp.net asp.net-core masstransit

我正在使用.NET Core 2.1和MassTransit开发该应用程序。 我收到关注错误:

  

对象名称:“ IServiceProvider”。System.ObjectDisposedException:无法访问已处置的对象。

在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>();
}

在“配置”中

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);
}

完整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)

我将不胜感激。我希望我的问题很清楚。

1 个答案:

答案 0 :(得分:0)

您可能会看到这个示例,说明如何在.NET核心应用程序中设置和使用MassTransit,包括使用内置的依赖项注入服务。

https://github.com/phatboyg/Sample-DotNetCore-DI