简体   繁体   中英

Not Receiving Messages using netMsmqBinding

Documentation examples can be found here http://msdn.microsoft.com/en-us/library/ms789008.aspx and http://msdn.microsoft.com/en-us/library/ms751493.aspx

I am aware of the issues around IIS7 App Pools not starting up (I do use the pre-warming technique to try and get around this), however I cannot get my WCF web service to receive messages from MSMQ even when running under WebDev in Visual Studio 2010 on my local PC.

The below simple test is based on the above documentation, but even their simple demonstrations don't seem to work, which makes me think I've overlooked something really obvious.

If you build the following, and use the below test lines, only the second one will get written out. The first one will go into MSMQ, and then never get picked up by the destination.

a) "This is a test." b) "!This is a different test."

I built a console client which sends a piece of data to a service endpoint:

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 is a simple service which receives the input, and sends it into 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 is a simple service which receives the input, and logs it to a local file.

[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>

This last service doesn't seem to "receive" messages from MSMQ. This occurs both when running the solution from my desktop, or if I deploy the services to a Server 2008 / IIS7 deployment and run the client either remotely or locally.

Have I missed something, or is there some options I should be checking in my Windows settings?

Regards, Rob.

You should do the following:

1 - Enable WCF tracing

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

This should log any failures in the WCF stack and should be enabled on both service and client.

2 - Enable source journaling

Add useSourceJournal="true" into your client netMsmqBinding configuration. This will journal messages which have been sent.

3 - Enable negative source journaling

Add 'deadLetterQueue="System" into your service netMsmqBinding configuration. This will cause non-delivered messages to go to the system dead letter queue.

4 - Enable MSMQ event logging

On service, go to Event viewer -> Applications and Services Logs -> Microsoft -> Windows -> MSMQ -> End2End -> Enable Log. This will log everything that happens in msmq on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM