2

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

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

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

如果您构建以下内容并使用以下测试行,则只会写出第二个。第一个将进入 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 设置的选项?

问候,罗布。

4

1 回答 1

0

您应该执行以下操作:

1 - 启用 WCF 跟踪

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

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

2 - 启用源日志

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

3 - 启用负面来源日记

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

4 - 启用 MSMQ 事件记录

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

于 2013-10-09T08:48:43.610 回答