所以我正在尝试配置我的客户端代码以将消息发送到 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();
}
}
}
应用程序配置:
<?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 启用了事件跟踪,这是事件查看器向我显示的内容: