我们的团队正在构建一个服务,用于使用WCF (通过msmqIntegrationBinding )处理来自多个远程MSMQ队列的消息。我们希望能够控制不同的队列组(根据它们通常消耗的资源数量通过serviceThrottling )。
我的想法是让单个服务类型处理来自多个队列的消息,并根据消息类型确定如何处理它们。不幸的是,我无法找到使用MsmqMessage的通用方法,因为它期望消息的确切类型。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 { }发布于 2015-10-15 17:09:00
这个问题实际上不是由MsmqMessage<object>引起的。
当排队的消息是XML格式时,服务使用ServiceKnownTypeAttribute来确定服务支持哪些类型用于XML (反)序列化。在本例中,object实际上不是一个有效的可序列化类型,因此可能被忽略。
为了支持XML消息的一般处理,可以将[ServiceKnownType(typeof(XElement))]添加到服务契约中,并接受MsmqMessage<object>作为服务方法的参数。这将允许您检查MsmqMessage<T>对象的属性,以确定应如何处理它。另一个可能的选择是使用接受方法参数的过载的ServiceKnownTypeAttribute来动态构建受支持类型的列表。
我检查的唯一其他序列化格式是Binary,所以请记住,它们可能都是不同的处理方式。具体来说,对于Binary格式,不需要ServiceKnownTypeAttribute,因为类型信息包含在二进制有效负载中(仅用System.Guid测试了这一点)。如果您打算使用Binary格式,那么继续使用MsmqMessage<object>而不是MsmqMessage<XElement>是很重要的,因为实际的对象类型将代替XElement。
https://stackoverflow.com/questions/33152760
复制相似问题