标题与Azure服务总线一起发布,如下所示:
string content = "body";
await _busPublisher.Activator.Bus.Publish(content, headers);
如何在订户上同时检索标头和内容?
class Handler : IHandleMessages<string>
{
public Handler(IMessageContext messageContext, ILog log)
{
_messageContext = messageContext;
_log = log;
}
public async Task Handle(string message)
{
Console.WriteLine("Handle(string message): {0}", message);
}
}
更新
以下是一种解决方案。这是最好的解决方案吗?
public Handler(IMessageContext messageContext, ILog log)
{
_messageContext = messageContext;
_log = log;
}
public async Task Handle(string message)
{
Console.WriteLine("Handle(string message): {0} ", message);
Console.WriteLine("headers: {0} ", string.Join(' ', _messageContext.Headers));
}
当像下面实例化处理程序时,可以使用依赖注入代替吗?
var Activator = new BuiltinHandlerActivator();
Activator.Register((mc) =>
{
return new Handler(mc, log); //no new?
}
答案 0 :(得分:1)
接受的IMessageContext
注入到处理程序的构造函数中是可行的方法:
public class Handler : IHandleMessages<string>
{
readonly IMessageContext messageContext;
public Handler(IMessageContext messageContext, ILog log)
{
this.messageContext = messageContext;
}
public async Task Handle(string message)
{
var headers = messageContext.Headers;
// do stuff
}
}
如果您使用的是BuiltinHandlerActivator
,则可以像这样注入它:
activator.Register(context => new Handler(context));
或者如果您的处理程序中还需要IBus
:
activator.Register((bus, context) => new Handler(bus, context));