0

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

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

这用于处理队列中的所有消息,直到我开始重命名。我有点茫然,因为它确实被调用了——但只有一次。事件日志中不会记录任何错误。Window Service 主机继续运行,并迅速响应来自 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);
  }
}
4

1 回答 1

2

问题是缓冲区大小

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

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

增加各种缓冲区大小完全解决了这个问题。如果您看到类似的行为,请访问 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 有了一个很好的了解,但没有意外遗漏的样板化是很有价值的,因为在引用它时不可能输入错误的名称。

This tool was was present in the Tools menu of Visual Studio, but the path to the exe is in the Windows SDK so I'm not sure how it got onto my system, but I am sure google or Bing can help you if you don't already have it.

于 2012-06-14T12:20:49.693 回答