未使用 netMsmqBinding 接收消息

本文关键字:消息 netMsmqBinding 未使用 | 更新日期: 2023-09-27 18:34:37

文档示例可以在这里找到 http://msdn.microsoft.com/en-us/library/ms789008.aspx 和 http://msdn.microsoft.com/en-us/library/ms751493.aspx

我知道有关 IIS7 应用程序池无法启动的问题(我确实使用预热技术来尝试解决此问题(,但是即使在本地 PC 上的 Visual Studio 2010 中的 WebDev 下运行,我也无法让我的 WCF Web 服务接收来自 MSMQ 的消息。

下面的简单测试基于上述文档,但即使是他们的简单演示似乎也不起作用,这让我觉得我忽略了一些非常明显的东西。

如果您构建以下内容,并使用以下测试行,则只会写出第二行。 第一个将进入MSMQ,然后永远不会被目的地接走。

a( "这是一个考验。"b) '"!这是一个不同的测试。

我构建了一个控制台客户端,它将一段数据发送到服务终结点:

class Program
{
    static void Main(string[] args)
    {
        string input;
        while (!string.IsNullOrEmpty(input = Console.ReadLine()))
        {
            if (input.StartsWith("!") && input.Length > 1)
            {
                using (var client = new MSMQClient.DirectServiceRef.Service1Client())
                {
                    client.Log(input.Substring(1));
                }
            }
            else
            {
                using (var client = new MSMQClient.LogServiceRef.Service2Client())
                {
                    client.Log(input);
                }
            }
        }
        Console.WriteLine("Done.");
        Console.ReadKey();
    }
}

应用配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService2">
                    <security mode="None" />
                </binding>
                <binding name="BasicHttpBinding_IService1">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1910/Service2.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService2" contract="LogServiceRef.IService2"
                name="BasicHttpBinding_IService2" />
            <endpoint address="http://localhost:1909/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="DirectServiceRef.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

Service2 是一个简单的服务,它接收输入,并将其发送到 MSMQ。

[ServiceContract]
public interface IService2
{
    [OperationContract]
    bool Log(string data);
}
public class Service2 : IService2
{
    public Service2()
    {
        var queueName = ConfigurationManager.AppSettings["queueName"];
        if (!MessageQueue.Exists(queueName))
        {
            MessageQueue.Create(queueName, true);
        }
    }
    [OperationBehavior]
    public bool Log(string data)
    {
        using (var client = new MSMQService2.MSMQServiceRef.Service1Client())
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                client.Log(data);
                scope.Complete();
            }
        }
        return true;
    }
}

网络配置:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="queueName" value=".'private$'Service1.svc" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="msmqBinding">
          <security mode="None" />
        </binding>
      </netMsmqBinding>
    </bindings>
    <services>
      <service name="logService">
        <endpoint binding="basicHttpBinding"
                  contract="MSMQService2.IService2" />
      </service>
    </services>
    <client>
      <endpoint address="net.msmq://localhost/private/Service1.svc"
                binding="netMsmqBinding"
                bindingConfiguration="msmqBinding"
                contract="MSMQServiceRef.IService1" />
    </client>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Service1 是一个简单的服务,它接收输入,并将其记录到本地文件中。

[ServiceContract]
public interface IService1
{
    [OperationContract(IsOneWay=true)]
    void Log(string data);
}
public class Service1 : IService1
{
    public Service1()
    {
        var queueName = ConfigurationManager.AppSettings["queueName"];
        if (!MessageQueue.Exists(queueName))
        {
            MessageQueue.Create(queueName, true);
        }
    }
    [OperationBehavior(TransactionAutoComplete=true, TransactionScopeRequired=true)]
    public void Log(string data)
    {
        using (var log = new FileInfo(ConfigurationManager.AppSettings["logFilePath"]).AppendText())
        {
            log.WriteLine(data);
        }
    }
}

网络配置:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="logFilePath" value="C:'testlog.txt"/>
    <add key="queueName" value=".'private$'Service1.svc"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="msmqBinding">
          <security mode="None" />
        </binding>
      </netMsmqBinding>
    </bindings>
    <services>
      <service name="logService">
        <endpoint address="net.msmq://localhost/private/Service1.svc"
                  binding="netMsmqBinding"
                  bindingConfiguration="msmqBinding"
                  contract="MSMQService1.IService1" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

最后一个服务似乎没有"接收"来自 MSMQ 的消息。 从桌面运行解决方案时,或者将服务部署到 Server 2008/IIS7 部署并远程或本地运行客户端时,都会发生这种情况。

我是否错过了某些内容,或者是否应该在我的 Windows 设置中检查一些选项?

问候抢。

未使用 netMsmqBinding 接收消息

您应该执行以下操作:

1 - 启用 WCF 跟踪

https://stackoverflow.com/a/4271597/569662

这应记录 WCF 堆栈中的任何故障,并应在服务和客户端上启用。

2 - 启用源日记

useSourceJournal="true"添加到客户端 netMsmqBinding 配置中。这将记录已发送的消息。

3 - 启用负源日记

将 'deadLetterQueue="System" 添加到您的服务 netMsmqBinding 配置中。这将导致未传递的消息进入系统死信队列。

4 - 启用 MSMQ 事件日志记录

在服务上,转到事件查看器 -> 应用程序和服务日志 -> Microsoft -> Windows -> MSMQ -> End2End -> 启用日志。这将记录服务器上 msmq 中发生的所有事情。