【问题标题】:.Net Core 2.1 + MassTransit - Cannot access a disposed object.Object name: 'IServiceProvider'.Net Core 2.1 + MassTransit - 无法访问已处置的对象。对象名称:'IServiceProvider'
【发布时间】:2018-07-28 20:57:20
【问题描述】:

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

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

在配置服务中

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

完整的堆栈跟踪

 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)

我将不胜感激。我希望很清楚我在问什么。

【问题讨论】:

  • 接受的答案不是答案。你可以解释一下你是如何使用链接文档来解决问题的

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


【解决方案1】:

MassTransit container configuration 有据可查。

还有一个configuration section for .NET Core

此外,现代版本的 MassTransit 不再支持 LoadFrom

【讨论】:

  • 好吧,考虑到这是一个古老的问题(按 .NET Core 标准)并且现在有许多更好的示例,我将更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 2022-01-11
  • 2020-04-23
相关资源
最近更新 更多