(Azure)BrokeredMessage.GetBody
问题描述:
我试图把一个'通用'订户放在一起,我可以(重新)使用Azure ServiceBus
。(Azure)BrokeredMessage.GetBody <xxx>
但我坚持如下; 我的代码曾经剥离过非必要部分,看起来像这样。
Subscribing.Client.OnMessage((recdMessage =>
{
var msgBody = recdMessage.GetBody<myClass>();
}, options);
我希望我的msgBody
是已序列化到消息体的类型。 事实上,如果myClass
是类似于TelephonyEventMessage
的东西,并且收到的消息是那种类型,那么我的msgBody
将是该类型的正确实例化/再水合的对象。 但虽然我可以使用recdMessage
。 ContentType来获取该消息中类的字符串名称....我只是不能确定我需要在上面的myClass
中放置什么。 我现在已经掌握了我的知识,没有多少搜索对我来说看起来像是一个答案。我是否需要为消息中可能存在的每种类型添加特定版本?
答
你可以使用这个,如果你期待许多不同的对象类型,从订阅接收消息:
public void ReceiveMessageFromSubscription<T>(string topicPath, string subscriptionName, Action<T> action)
{
var client = SubscriptionClient.CreateFromConnectionString(ConnectionString, topicPath, subscriptionName);
client.OnMessage((message) =>
{
try
{
_logger.Information("Processing message");
action(message.GetBody<T>());
message.Complete();
}
catch(Exception ex)
{
_logger.Error(ex, "Error processing message");
message.Abandon();
}
});
}
再经过中知道如何处理的对象,如下面的方法。你可以有一些这些方法,都调用ReceiveMessageFromSubscription。
public void ProcessObject()
{
_serviceBusService.ReceiveMessageFromSubscription<MyObject>(mytopic, mysubscription, _myobjectService.ProcessObject);
}