MSMQ,消息被放入队列并消失,但从未被服务合同接收

时间:2012-07-25 07:57:11

标签: c# asp.net-mvc wcf msmq msmqintegrationbinding

我有一个本地私人队列。我在MVC应用程序中也有一个WCF服务,它使用msmqIntegrationBinding来侦听队列。问题是,当消息排队时,永远不会调用服务契约,但消息会消失。消息不在毒药队列中。这是配置部分,我声明绑定到队列:

<services>
  <service name="SkruvInfo.Web.Service.QueueMessageReceiver">
    <endpoint address="msmq.formatname:DIRECT=OS:LEIA\private$\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");
        }
    }

有关邮件消失但未调用服务的任何建议?

2 个答案:

答案 0 :(得分:1)

尝试使用ServiceKnownType属性修改服务合同:

[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>

允许值记录为here

答案 1 :(得分:0)

根据我的经验,此行为是由序列化失败或大型消息(64KB是默认的最大消息大小)引起的。这会导致WCF以静默方式崩溃(是的,我也不理解该设计选择),但仍然会从队列中删除该消息。

只需启用跟踪,您就可以在WCF中快速查看导致此问题的异常。

将此添加到您的web.config(我在下面&lt; /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:\logs\webbackend_wcf_log.txt" />
</system.diagnostics>