当我在massTransit 3.x中时,我使用follow代码在发送之前设置消息头,它的工作。
public class SendObserver : ISendObserver
{
public Task PostSend<T>(SendContext<T> context) where T : class
{
return Task.FromResult<object>(null);
}
public Task PreSend<T>(SendContext<T> context) where T : class
{
//1. get current thread identity object.
var currentThreadIdentityObject = Thread.CurrentPrincipal.Identity is JustIMIdentity ? (Thread.CurrentPrincipal.Identity as JustIMIdentity).IdentityObject : null;
if (currentThreadIdentityObject != null)
{
//2. set the headers.
context.Headers.Set(AppSettingConfig.RabbitMqIdentityHeaderKey, currentThreadIdentityObject.StaffID);
}
return Task.FromResult<object>(null);
}
public Task SendFault<T>(SendContext<T> context, Exception exception) where T : class
{
return Task.FromResult<object>(null);
}
}
我在autofac中注册
//register in autofac
builder.Register(context =>
{
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var uri = AppSettingConfig.RabbitMqBaseUri;
var host = cfg.Host(new Uri(uri), x =>
{
x.Username(AppSettingConfig.RabbitMqUserName);
x.Password(AppSettingConfig.RabbitMqUserPassword);
});
});
busControl.ConnectSendObserver(new SendObserver());
busControl.Start();
return busControl;
})
.SingleInstance()
.As<IBusControl>()
.As<IBus>();
builder.RegisterGeneric(typeof(RequestClient<,>)).AsSelf().AsImplementedInterfaces();
但是当我将massTransit更新为4.X时,队列中的消息没有标题,看起来像set header不起作用,是怎么回事。