下面的代码注册使用者接口实现,并注册通用使用者接口的通用装饰器。
builder.AddMassTransit();
builder.Register
(
context =>
{
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
foreach (var messageConfiguration in messageConfigurations)
{
var type = messageConfiguration.Type; // MESSAGE TYPE
if (messageConfiguration.Consumers.Any())
cfg.ReceiveEndpoint($"{type.Namespace}:{type.Name}", ec =>
{
foreach (var cType in messageConfiguration.Consumers)
ec.ConfigureConsumer(context, cType); //REGISTERS CONSUMER IMPLEMENTATION
});
}
});
return busControl;
}
)
.SingleInstance()
.As<IBusControl>()
.As<IBus>();
builder.RegisterGenericDecorator(typeof(ConsumerDecorator<>), typeof(IConsumer<>)); //DECORATES IConsumer interfaces.
因此,可以在没有其装饰器的情况下解决消费者实现。
我需要将使用者注册为他们的接口,而不是使用其实现来装饰其使用者的实现。因此,我可以使用autofac注册一个通用的消费者装饰器。