Queue showing empty

197 views Asked by At

So this is pretty new to me and I'm making progress. I currently have a wcf service using netmsmqbinding to send a simple message to a queue. Everything runs smooth and the event view even says that the message was put in the queue. Although when I goto the actual queue there is nothing in there. I have a service, service host, and asp web app. Note in the web app I have a service reference to the service. Also in the msmq log in the event viewer I get 3 events. Any assistance would be appreciated. Thanks.

Here's my setup.

Service Interface:

[ServiceContract]
public interface IMSMQService
{
    [OperationContract(IsOneWay = true)]
    void ShowMessage(string msg);
}

Service:

public class MSMQService : IMSMQService
{
    [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
    public void ShowMessage(string msg)
    {
        Debug.WriteLine(msg + " Received at: " + System.DateTime.Now.ToString());
    }
}

App.Config of Service:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>

  <appSettings>
    <!-- use appSetting to configure MSMQ queue name -->
    <add key="queueName" value=".\private$\TestQueue"/>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>

  <system.serviceModel>
    <services>
      <service name="MSMQNoSecurityService.MSMQService" behaviorConfiguration="MyBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001/msmq"/>
          </baseAddresses>
        </host>
        <endpoint address="net.msmq://localhost/private/TestQueue" binding="netMsmqBinding" bindingConfiguration="MyBinding" contract="MyService.IMSMQService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>        
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="MyBinding">
          <security mode="None"/>
        </binding>
      </netMsmqBinding>
    </bindings>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

Service Host

static void Main(string[] args)
{
    ServiceHost svcHost = null;
    try
    {
        // Get MSMQ queue name from appsettings in configuration.
        string queueName = ConfigurationManager.AppSettings["queueName"];

        // Create the transacted MSMQ queue if necessary.
        if (!MessageQueue.Exists(queueName))
            MessageQueue.Create(queueName, true);

        svcHost = new ServiceHost(typeof(MyService.MSMQService));
        svcHost.Open();
        Console.WriteLine("\n\nService is Running  at following address");
        Console.WriteLine("\nhttp://localhost:9001/MSMQService");

    }
    catch (Exception eX)
    {
        svcHost = null;
        Console.WriteLine("Service can not be started \n\nError Message [" + eX.Message + "]");
    }

    if (svcHost != null)
    {

        Console.WriteLine("\nPress any key to close the Service");
        Console.ReadKey();
        svcHost.Close();
        svcHost = null;
    }   
}

App.Config of Service Host:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>

  <appSettings>
    <add key="queueName" value=".\private$\TestQueue"/>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <services>
      <service name="MyService.MSMQService" behaviorConfiguration="MyBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001/msmq"/>
          </baseAddresses>
        </host>        
        <endpoint address="net.msmq://localhost/private/TestQueue" binding="netMsmqBinding" bindingConfiguration="MyBinding" contract="MyService.IMSMQService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>        
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="MyBinding"  >
          <security mode="None"/>
        </binding>
      </netMsmqBinding>
    </bindings>
  </system.serviceModel>
</configuration>

MVC Controller

public class WcfMsmqController : Controller
{        
    public ActionResult Index()
    {
        MSMQServiceClient proxy = new MSMQServiceClient();
        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
        {                
            proxy.ShowMessage("test");
            scope.Complete();
        }             
        proxy.Close();

        return View();
    }

}

Service model in Web.config (autogenerated):

<system.serviceModel>
    <bindings>
      <netMsmqBinding>
        <binding name="NetMsmqBinding_IMSMQService">
          <security mode="None" />
        </binding>
      </netMsmqBinding>
    </bindings>
    <client>
      <endpoint address="net.msmq://localhost/private/TestQueue" binding="netMsmqBinding"
        bindingConfiguration="NetMsmqBinding_IMSMQService" contract="ServiceReference1.IMSMQService"
        name="NetMsmqBinding_IMSMQService" />
    </client>
</system.serviceModel>

Events from MSMQ Log: Modified some of it for viewing on here

  1. Message with ID CN=msmq,CN=blah,OU=blah,OU=blah,DC=blah,DC=local\8280 was sent to queue PRIVATE=blah

  2. Message with ID CN=msmq,CN=blah,OU=blah,OU=blah,DC=blah,DC=local\8280 was put into queue PRIVATE=blah

  3. Message received

2

There are 2 answers

1
rodrigogq On

It seems to be related to your transaction.

You should try to place [TransactionFlow(TransactionFlowOption.Allowed)] on the operation in the service contract. Something like:

 [ServiceContract]
 public interface IMSMQService
 {
   [OperationContract(IsOneWay = true)]
   [TransactionFlow(TransactionFlowOption.Allowed)]
   void ShowMessage(string msg);
 } 

Anyway, you should be aware of local transactions if you decide to deploy this on web farm or something that involves multiple computers.

0
AudioBubble On

Figured it out. I had to add a namespace to the servicecontract. Prior it would default to some kind of tempuri.

[ServiceContract(Namespace = "http://MyService")]
public interface IMSMQService
{
    [OperationContract(IsOneWay = true)]
    [TransactionFlow(TransactionFlowOption.Allowed)]
    void ShowMessage(string msg);
}