c# - 为什么我排队的 WCF 消息悄无声息地消失了?

标签 c# .net wcf msmq netmsmqbinding

我在服务器 THOR 上设置了事务性 MSMQ 队列。我可以使用以下代码从工作站向该队列发布消息:

var queue = new MessageQueue("FormatName:Direct=OS:thor\\private\\myqueue");
using (var tx = new MessageQueueTransaction())
{
   tx.Begin();
   queue.Send("test", tx);
   tx.Commit();
}

但是,当我尝试使用 WCF 进行连接时,我的消息从未出现在队列中。这是我正在使用的配置:

<system.serviceModel>
  <bindings>
    <netMsmqBinding>
      <binding name="ClientNewsFeedServiceBinding" durable="true" exactlyOnce="true">
        <security mode="None" />
      </binding>
    </netMsmqBinding>
  </bindings>

  <client>
    <!-- NewsFeed Service -->
    <endpoint name="INewsFeedService"
              address="net.msmq://thor/private/myqueue"
              binding="netMsmqBinding"
              bindingConfiguration="ClientNewsFeedServiceBinding"
              contract="Service.Contract.INewsFeedService" />
  </client>
</system.serviceModel>

还有代码:

using (var tx = new TransactionScope())
{
   var cf = new ChannelFactory<INewsFeedService>("INewsFeedService");
   var service = cf.CreateChannel();
   service.TestMessage("test");
   ((IChannel)service).Close();
   tx.Complete();
}

我没有收到任何异常,但 THOR 上的队列中没有发布任何消息。有任何想法吗?我什至不知道如何调试它,因为它只是默默地失败了。

更新

如果我将我的 MSMQ URI 更改为“net.msmq://localhost/private/myqueue”,那么它将发布到我设置的本地事务队列。队列本身的设置是相同的(例如,我执行了相同的步骤来创建本地主机和 THOR 队列)。

最佳答案

我相信如果您在 MSMQ 服务器端使您的队列具有事务性,您需要在 WCF 绑定(bind)配置中指定更多设置 - 试试这个:

<bindings>
    <netMsmqBinding>
      <binding name="ClientNewsFeedServiceBinding" 
               durable="true" exactlyOnce="true">
        <security mode="None" />
      </binding>
    </netMsmqBinding>
  </bindings>

如果我没记错的话,您需要将 durable="true"exactlyOnce="true" 属性添加到您的 netMsmq 绑定(bind)中才能使其正常工作。

关于如何让 MSMQ 和 WCF 很好地协同工作,有一个非常好的教程:

Tom 在第 3 部分介绍了事务队列,并提到:

The exactlyOnce="true" attribute is WCF-speak for using a transactional message queue.

durable=true 仅表示将消息立即刷新到磁盘,而不是将它们保存在服务器内存中。它速度较慢,但​​在服务器崩溃或电源中断的情况下,消息不会丢失。经典的速度与可靠性权衡....

更新:既然您要“跨越”机器边界,并且您正在使用事务队列 - 您是否检查了所有相关机器上的 DTC(分布式事务协调器)?查看 Tom 的博客第 3 部分:

Check DTC Configuration

Our epic journey is almost at an end. In fact if you're still playing along at home, you can try running the application with the transactional queues to see if it's working. If it's failing, one possible cause is problems with your Distributed Transaction Coordinator configuration. Here are a few things to try:

关于c# - 为什么我排队的 WCF 消息悄无声息地消失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3865439/

相关文章:

使用 https 的 WCF 服务自托管

具有 Http 基本身份验证的 WCF

c# - asp.net mvc 可以自动生成时间戳到 js 代码吗?

c# - 如何在txt文件中保存异常?

c# - TimeSpan.ParseExact(毫秒)

c# - 如何明确、精确地控制构图范围?

c# - 使用本地服务的成员资格表不正确

.net - 如何更改包含数据的 DataTable 中的列数据类型?

c# - 如何取消 Microsoft ServiceBus MessageReceiver Receive 调用?

wcf - 如何识别调用您的操作的 WCF 端点?