WCF MSMQ 仅触发一次

本文关键字:一次 MSMQ WCF | 更新日期: 2023-09-27 18:36:16

我编写了一个 C# WCF 服务,该服务应该处理传入的消息,分析它们包含的 XML 并将数据转录到数据库表。

当我启动承载我的 WCF MSMQ 服务实现的 Windows 服务时,它成功地处理了一条消息,然后停止。

这曾经处理队列中的所有消息,直到我开始重命名内容。我有点不知所措,因为它确实被调用了——但只有一次。事件日志中未记录任何错误。窗口服务主机继续运行,并迅速响应来自 SCM 的服务停止指令。

  <netMsmqBinding>
    <binding name="Binding.MSMQ.TransportSecurity" maxReceivedMessageSize="32767">
      <readerQuotas maxBytesPerRead="32767" maxStringContentLength="32767" maxArrayLength="32767"/>
      <security mode="Transport">
        <transport msmqAuthenticationMode="None" msmqEncryptionAlgorithm="RC4Stream"
            msmqProtectionLevel="None" msmqSecureHashAlgorithm="Sha1" />
        <message clientCredentialType="Windows" />
      </security>
    </binding>
  </netMsmqBinding>

<services>
  <service behaviorConfiguration="Behavior.CommonSense.ChapterWriter"
    name="CommonSense.ChapterWriter">
    <endpoint address="net.msmq://localhost/private/bob.logwriter"
      binding="netMsmqBinding" bindingConfiguration="Binding.MSMQ.TransportSecurity"
      name="Endpoint.CommonSense.ChapterWriter.msmq" contract="CommonSense.IChapterWriter">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/CommonSense/ChapterWriter/" />
      </baseAddresses>
    </host>
  </service>
</services>

using System.ServiceModel;
namespace CommonSense
{
  [ServiceContract]
  public interface IChapterWriter
  {
    [OperationContract(IsOneWay = true)]
    void Write(string xml);
  }
}

WCF MSMQ 仅触发一次

问题是缓冲区大小

它不是第一次运行,而是运行一次

我的测试用例中使用的消息生成器恰好被配置为生成两条大消息和一条小消息 (16K)。小的正在正常处理,一旦遇到大的异常,致命的异常就会使 WCF 服务脱轨,而不会停止 Windows 服务主机进程的等待线程。

增加各种缓冲区大小完全解决了这个问题。如果您看到类似的行为,请访问 ReaderQuotas 设置和绑定 maxReceivedMessageSize。WCF 配置编辑器使这变得简单。

<netMsmqBinding> 
  <binding name="Binding.MSMQ.TransportSecurity" maxReceivedMessageSize="65535"> 
    <readerQuotas 
      maxBytesPerRead="65535" 
      maxStringContentLength="65535" 
      maxArrayLength="65535"/> 
    <security mode="Transport"> 
      <transport msmqAuthenticationMode="None" msmqEncryptionAlgorithm="RC4Stream" 
          msmqProtectionLevel="None" msmqSecureHashAlgorithm="Sha1" /> 
      <message clientCredentialType="Windows" /> 
    </security> 
  </binding> 
</netMsmqBinding> 

WCF 配置编辑器

我不能高度推荐这个工具。虽然我已经对如何配置 WCF 有了很好的了解,但无意外遗漏的模板很有价值,因为在引用名称时不可能键入错误的名称也是如此。

这个工具出现在Visual Studio的"工具"菜单中,但是exe的路径在Windows SDK中,所以我不确定它是如何进入我的系统的,但我相信谷歌或Bing可以帮助你,如果你还没有它。