需要解决IRabbitMqHost,以便在总线启动后向总线添加处理程序。
复制步骤
在masstransit配置中注册“ IRabbitMqHost” 尝试在autofac中解析“ IRabbitMqHost” 然后“引发了'Autofac.Core.Registration.ComponentNotRegisteredException类型的异常”
using Simple.OData.Client;
using System;
using System.Linq;
using System.Threading.Tasks;
class Program
{
public static void Main(string[] args)
{
Foo();
}
public static async Task Foo()
{
Console.WriteLine("This happened");
var client = new ODataClient("https://www.nuget.org/api/v2/");
var packages = await client.FindEntriesAsync("Packages?$filter=Title eq 'SDK'");
Console.WriteLine("Found packages: " + packages.Count());
foreach (var package in packages)
{
Console.WriteLine(package["Title"]);
}
}
}
预期行为 需要解析在IBus配置中配置的IRabbitMqHost,因为需要在总线启动后添加处理程序。 需要_rabbitMqHost.ConnectReceiveEndpoint()...
实际行为 container.Resolve引发异常belove。 https://github.com/MassTransit/MassTransit/issues/1470
中的图片答案 0 :(得分:0)
在autofac中注册到总线时,提供了创建总线实例并注册主机的委托功能。但是此委托尚未被调用。在构建containerbuilder并尝试解析IBus之后,它会调用委托和注册的IRabbitMqHost,但不会构建容器。然后,当我尝试解决IRabbitMqHost时,找不到注册组件,因为未构建添加的容器。
此代码对我有用;
builder.Register(context =>
{
var bus = Bus.Factory.CreateUsingRabbitMq(opt =>
{
var result = new List<string>();
Configuration.GetSection("RabbitMq:HostNames").Bind(result);
var host = opt.Host(result[0], Configuration.GetValue<string>("RabbitMq:VirtualHost"), h =>
{
h.Username(Configuration.GetValue<string>("RabbitMq:Username"));
h.Password(Configuration.GetValue<string>("RabbitMq:Password"));
});
ContainerBuilder b = new ContainerBuilder();
b.Register<IRabbitMqHost>(a => host).SingleInstance();
b.Update(ApplicationContainer);
});
return bus;
}).As<IBus>()
.As<IBusControl>()
.SingleInstance();
``