【发布时间】: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