我使用RabbitMQ与C#,MassTransit,Autofac.
我有一个预定的(5秒)工作来消耗队列.
这项工作大约2个小时,然后突然停止.当我搜索日志时,我发现了这个:
Scope —> An exception occurred during bus creation (See inner
exception for det ails.) —> MassTransit.ConfigurationException: An
exception occurred during bus creation —>
System.OutOfMemoryException: Exception of type ‘System.OutOfMemoryException’ was thrown.
这些是我在MassTransit中配置Autofac的代码块:
builder.Register(context =>
{
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(new Uri(Globals.QueueUrl), h =>
{
h.Username(Globals.QueueUserName);
h.Password(Globals.QueuePassword);
});
cfg.ReceiveEndpoint("create_sales_queue",
ec =>
{
ec.Consumer<CreateSalesConsumer>(context);
ec.UseRetry(p => p.Incremental(10, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(200)));
});
});
return busControl;
})
.InstancePerDependency()
.As<IBusControl>()
.Named<IBusControl>(GenericIntegrationTask.CreateSalesTask.ToString());
这些是预定的工作代码:
try
{
var bus = container.ResolveNamed<IBusControl>(GenericIntegrationTask.CreateSalesTask.ToString());
bus.Start();
Thread.Sleep(2000);
bus.Stop();
}
catch (Exception e)
{
Console.WriteLine(e);
}
提前致谢.
解决方法:
您永远不应该为每个依赖项或每个请求注册您的IBus.它必须是单个实例.
您还需要在应用启动时启动总线,并在应用停止时停止总线.