通过msmqIntegrationBinding将消息发送到msmq队列

时间:2016-12-12 11:48:06

标签: c# wcf msmq msmqintegrationbinding

所以我尝试配置我的客户端代码以将消息发送到MSMQ队列。我按照此处描述的步骤操作:https://msdn.microsoft.com/en-us/library/ms789008(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp,我的客户端代码如下所示:

class Program
{
    static void Main(string[] args)
    {
        var binding = new MsmqIntegrationBinding("MyMessagesBinding");
        var address = new EndpointAddress(@"msmq.formatname:DIRECT = OS:.\private$\MyMessages");
        var channelFactory = new ChannelFactory<IDataRelayService>(binding, address);
        var channel = channelFactory.CreateChannel();

        while (true)
        {
            var message = new MyMessage
            {
                Content = "this is content!!!",
                Id = "random uuid"
            };
            var msmqWrapper = new MsmqMessage<MyMessage>(message)
            {
                Priority = MessagePriority.Highest
            };

            channel.PassMessage(msmqWrapper);
            Console.WriteLine("message sent");
            Console.ReadLine();
        }
    }
}

App.config中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <system.serviceModel>
    <client>
      <endpoint name="MyResponseEndpoint"
                address="msmq.formatname:DIRECT=OS:.\private$\MyMessages"
                binding="msmqIntegrationBinding"
                bindingConfiguration="MyMessagesBinding"
                contract="Client.IDataRelayService">
      </endpoint>
    </client>
    <bindings>
      <msmqIntegrationBinding>
        <binding name="MyMessagesBinding">
          <security mode="None" />
        </binding>
      </msmqIntegrationBinding>
    </bindings>
  </system.serviceModel>
</configuration>

IDataRelayService.cs:

[ServiceContract]
public interface IDataRelayService
{
    [OperationContract(IsOneWay = true, Action = "*")]
    void PassMessage(MsmqMessage<MyMessage> message);
}

IDataRelayServiceChannel.cs:

public interface IDataRelayServiceChannel : IDataRelayService, System.ServiceModel.IClientChannel
{
}

它编译并运行没有问题,但是当我打开evet查看器时,没有为MSMQ记录任何事件。如果我打开计算机管理工具来查看队列,它会在我的队列中显示0条消息。我在这里做错了什么?

修改

我启用了MSMQ的事件跟踪,以及事件查看器向我显示的内容:

enter image description here

1 个答案:

答案 0 :(得分:0)

您的问题几乎肯定与权限相关。

运行邮件发件人的服务帐户必须对其尝试发送到的队列具有以下MSMQ权限:

  • 发送消息
  • 获取属性
  • 获取权限

如果您的发件人与您的收件人位于不同的域,则需要将这些权限授予名为ANONYMOUS_LOGON的本地用户。

此外,启用MSMQ E2E事件记录:https://technet.microsoft.com/en-us/library/cc730882(v=ws.11).aspx。这应该告诉你发生了什么。

如果队列的两端都有WCF,则应该使用netMsmqBinding而不是msmqIntegrationBinding。