我有一个本地私有队列。我在MVC应用程序中还有一个WCF服务,它使用msmqIntegrationBinding侦听队列。问题是,当消息排队时,服务契约永远不会被调用,但消息会在稍后消失。该消息不在毒药队列中。下面是我声明绑定到队列的配置部分:
<services>
<service name="SkruvInfo.Web.Service.QueueMessageReceiver">
<endpoint address="msmq.formatname:DIRECT=OS:LEIAprivate$screwinfo_autotests_messagequeue"
binding="msmqIntegrationBinding"
bindingConfiguration="MsmqBinding"
contract="SkruvInfo.Web.Service.IQueueMessageReceiver" />
</service>
</services>
这是合同:
[ServiceContract(Namespace = "http://localhost/SkruvWeb/Service")]
public interface IQueueMessageReceiver
{
[OperationContract(IsOneWay = true, Action = "*")]
void PutScrewInfoMessage(MsmqMessage<string> msg);
}
这是服务中的方法:
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void PutScrewInfoMessage(System.ServiceModel.MsmqIntegration.MsmqMessage<string> msg)
{
log4net.Config.XmlConfigurator.Configure();
var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
try
{
log.Debug("Message from queue: " + msg.Body.ToString(CultureInfo.InvariantCulture));
var xDoc = new XmlDocument();
xDoc.LoadXml(msg.Body);
CacheScrewInfoModelFromScrewInfoXmlDoc(xDoc);
}
catch (Exception e)
{
log.Error("Error parsing message from queue",e);
EventLog.WriteEntry("Application","Message error for screws");
}
}
有什么建议可以解释为什么消息消失但没有调用服务?
尝试使用ServiceKnowType属性修改您的服务合同:
[ServiceContract(Namespace = "http://localhost/SkruvWeb/Service")]
[ServiceKnownType(typeof(String))]
public interface IQueueMessageReceiver
{
[OperationContract(IsOneWay = true, Action = "*")]
void PutScrewInfoMessage(MsmqMessage<string> msg);
}
更新
如果您使用MsmqIntegrationBinding,我假设您的队列客户端是像VB6客户端这样的遗留应用程序?如果是这样,则需要在服务绑定配置中指定序列化格式。例如:
<msmqIntegrationBinding>
<binding name="MsmqBinding" serializationFormat="ActiveX">
<security mode="None" />
</binding>
</msmqIntegrationBinding>
此处记录了可允许的值。
根据我的经验,这种行为是由序列化失败或消息过大(64KB是默认的最大消息大小)引起的。这导致WCF以静默方式崩溃(是的,我也不理解这种设计选择),但仍然从队列中删除消息。
只需启用跟踪,您就会很快在WCF中看到导致这种情况的异常。
将其添加到web.config中(我在</system.webServer>下面有它),以进行粗略的跟踪设置。请确保AppPool用户对日志记录文件夹具有写访问权限。
<system.diagnostics>
<trace autoflush="true" indentsize="4" />
<sources>
<source name="System.ServiceModel" switchValue="Warning">
<listeners>
<add name="textLogger" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="textLogger"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:logswebbackend_wcf_log.txt" />
</system.diagnostics>