不使用netMsmqBinding接收消息

时间:2013-10-08 14:51:12

标签: c# .net wcf iis-7 msmq

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

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

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

如果您构建以下内容并使用下面的测试行,则只会写出第二行。第一个将进入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();
    }
}

App.config中:

<?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;
    }
}

的Web.config:

<?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);
        }
    }
}

的Web.config:

<?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设置中检查一些选项?

此致 罗布。

1 个答案:

答案 0 :(得分:1)

您应该执行以下操作:

1 - 启用WCF跟踪

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

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

2 - 启用源日记功能

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

3 - 启用负面源日记

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

4 - 启用MSMQ事件记录

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