servicestack Servicestack RabbitMq:消息头中的返回响应类型
有没有办法将响应dto的类型添加到rabbitmq响应消息的headers集合中 (我的使用者正在使用spring的rabbitmq处理程序,它在反序列化时似乎依赖于mq头中的显式类型信息。) 目前servicestack的mq producer已返回服务器头,如“content\u type='application/json” 我需要一个额外的头,例如“typeId”=“HelloResponse”,以便消费web应用知道如何反序列化消息,即使在响应队列名称是某种GUID的RPC情况下也是如此
servicestack Servicestack RabbitMq:消息头中的返回响应类型,
servicestack,rabbitmq,
servicestack,Rabbitmq,有没有办法将响应dto的类型添加到rabbitmq响应消息的headers集合中 (我的使用者正在使用spring的rabbitmq处理程序,它在反序列化时似乎依赖于mq头中的显式类型信息。) 目前servicestack的mq producer已返回服务器头,如“content\u type='application/json” 我需要一个额外的头,例如“typeId”=“HelloResponse”,以便消费web应用知道如何反序列化消息,即使在响应队列名称是某种GUID的RPC情况下也是如
是否有某种配置使我能够记录这种行为?或者在消息发布之前使用一些钩子,这样我就可以自己添加头了?我已经添加了对自动填充RabbitMQ的
IBasicProperties.Type中的消息正文类型的支持,以及对发布和GetMessage筛选器的支持
下面是一个使用自定义处理程序配置RabbitMqServer
的示例,您可以在消息发布和接收时修改消息及其元数据属性:
string receivedMsgApp = null;
string receivedMsgType = null;
var mqServer = new RabbitMqServer("localhost")
{
PublishMessageFilter = (queueName, properties, msg) => {
properties.AppId = "app:{0}".Fmt(queueName);
},
GetMessageFilter = (queueName, basicMsg) => {
var props = basicMsg.BasicProperties;
receivedMsgType = props.Type; //automatically added by RabbitMqProducer
receivedMsgApp = props.AppId;
}
};
mqServer.RegisterHandler<Hello>(m =>
new HelloResponse { Result = "Hello, {0}!".Fmt(m.GetBody().Name) });
mqServer.Start();
string receivedMsgApp=null;
字符串receivedMsgType=null;
var mqServer=new RabbitMqServer(“localhost”)
{
PublishMessageFilter=(队列名称、属性、消息)=>{
properties.AppId=“app:{0}”.Fmt(queueName);
},
GetMessageFilter=(queueName,basicMsg)=>{
var props=basicMsg.BasicProperties;
receivedMsgType=props.Type;//由RabbitMqProducer自动添加
receivedMsgApp=props.AppId;
}
};
mqServer.RegisterHandler(m=>
新Hello响应{Result=“Hello,{0}!”.Fmt(m.GetBody().Name)});
mqServer.Start();
配置后,任何发布或接收的消息都将通过上述处理程序处理,例如:
using (var mqClient = mqServer.CreateMessageQueueClient())
{
mqClient.Publish(new Hello { Name = "Bugs Bunny" });
}
receivedMsgApp.Print(); // app:mq:Hello.In
receivedMsgType.Print(); // Hello
using (IConnection connection = mqServer.ConnectionFactory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
var queueName = QueueNames<HelloResponse>.In;
channel.RegisterQueue(queueName);
var basicMsg = channel.BasicGet(queueName, noAck: true);
var props = basicMsg.BasicProperties;
props.Type.Print(); // HelloResponse
props.AppId.Print(); // app:mq:HelloResponse.Inq
var msg = basicMsg.ToMessage<HelloResponse>();
msg.GetBody().Result.Print(); // Hello, Bugs Bunny!
}
使用(var mqClient=mqServer.CreateMessageQueueClient())
{
Publish(newhello{Name=“Bugs Bunny”});
}
receivedMsgApp.Print();//app:mq:Hello.In
receivedMsgType.Print();//你好
使用(IConnection connection=mqServer.ConnectionFactory.CreateConnection())
使用(IModel channel=connection.CreateModel())
{
var queueName=QueueNames.In;
channel.RegisterQueue(queueName);
var basicMsg=channel.BasicGet(queueName,noAck:true);
var props=basicMsg.BasicProperties;
props.Type.Print();//Hello响应
props.AppId.Print();//app:mq:helloreresponse.Inq
var msg=basicMsg.ToMessage();
msg.GetBody().Result.Print();//你好,Bugs Bunny!
}
此更改可从ServiceStackv4.0.33+获得,即现在的版本。能否提供有关如何使用MQ服务的更多上下文?在什么时候需要类型名。您正在从mq:helloreresponse.inq
读取消息吗?在这种情况下,类型为mq名称。我们不使用默认响应队列(e.q.mq:HelloResponse.inq),而是使用临时队列(amq.*)。因此,我们无法从响应队列名称推断响应类型。为了选择正确的json反序列化程序,我们需要提供一个类型名。谢谢。