我们的团队正在构建一项服务,使用WCF(通过msmqIntegrationBinding)处理来自多个远程MSMQ队列的消息。我们希望能够根据他们通常消耗的资源量来限制不同的队列组(通过serviceThrottling)。
我的想法是让单个服务类型处理来自多个队列的消息,并根据消息类型确定如何处理这些消息。不幸的是,我无法找出使用MsmqMessage<T>
的通用方法,因为它期望确切的消息类型。 MsmqMessage<object>
无法正常工作,因为我认为它正在尝试为类型object
找到序列化程序。
关于如何获得这项工作或替代方法的任何想法?最好还是使用WCF,因为它已经内置了死信处理。
示例配置:
<services>
<service name="MessageProcessor.LowResourceMsmqReceiverService" behaviorConfiguration="LowResourceMsmqServiceBehavior">
<endpoint address="msmq.formatname:DIRECT=OS:.\private$\EmailQueue" binding="msmqIntegrationBinding" bindingConfiguration="IncomingMessageBinding" contract="MessageProcessor.IMsmqReceiverService" />
<endpoint address="msmq.formatname:DIRECT=OS:.\private$\LoggingQueue" binding="msmqIntegrationBinding" bindingConfiguration="IncomingMessageBinding" contract="MessageProcessor.IMsmqReceiverService" />
</service>
<service name="MessageProcessor.HighResourceMsmqReceiverService" behaviorConfiguration="HighResourceMsmqServiceBehavior">
<endpoint address="msmq.formatname:DIRECT=OS:.\private$\DataImportQueue" binding="msmqIntegrationBinding" bindingConfiguration="IncomingMessageBinding" contract="MessageProcessor.IMsmqReceiverService" />
<endpoint address="msmq.formatname:DIRECT=OS:.\private$\DataExportQueue" binding="msmqIntegrationBinding" bindingConfiguration="IncomingMessageBinding" contract="MessageProcessor.IMsmqReceiverService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="LowResourceMsmqServiceBehavior">
<serviceThrottling maxConcurrentCalls="50" />
</behavior>
<behavior name="HighResourceMsmqServiceBehavior">
<serviceThrottling maxConcurrentCalls="3" />
</behavior>
</serviceBehaviors>
</behaviors>
示例合约:
[ServiceContract]
[ServiceKnownType(typeof(object))]
public interface IMsmqReceiverService
{
[OperationContract(IsOneWay = true, Action = "*")]
void Receive(MsmqMessage<object> message);
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall)]
public abstract class TransactionalMsmqReceiverService : IMsmqReceiverService
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
[TransactionFlow(TransactionFlowOption.Allowed)]
public void Receive(MsmqMessage<object> message)
{
// TODO: Handle multiple message types here
}
}
public sealed class LowResourceMsmqReceiverService : TransactionalMsmqReceiverService { }
public sealed class HighResourceMsmqReceiverService : TransactionalMsmqReceiverService { }
答案 0 :(得分:1)
问题实际上并非由MsmqMessage<object>
引起。
当排队的消息采用XML格式时,该服务使用ServiceKnownTypeAttribute
来确定XML(de)序列化服务支持的类型。在这种情况下,object
并非真正有效的可序列化类型,因此可能会被忽略。
为了支持XML消息的通用处理,您可以将[ServiceKnownType(typeof(XElement))]
添加到服务合同中,并接受MsmqMessage<object>
作为服务方法的参数。这将允许您检查MsmqMessage<T>
对象的属性以确定应如何处理它。另一种可能的选择是使用接受方法参数的ServiceKnownTypeAttribute
的{{3}}来动态构建支持的类型列表。
我检查的唯一其他overload是Binary
,因此请记住,它们的处理方式可能不同。具体而言,对于Binary
格式,不需要ServiceKnownTypeAttribute
,因为类型信息包含在二进制有效负载中(仅使用System.Guid
对其进行测试)。如果您打算使用Binary
格式,那么重要的是继续使用MsmqMessage<object>
而不是MsmqMessage<XElement>
,因为实际的对象类型将会通过,而不是XElement