K09
K09

Reputation: 201

WCF Service not Processing MSMQ Message

I have a WCF Windows Service that checks for MSMQ messages. It picks the messages up ok but the ProcessMSMQMessage event does not seem to get called.

Any ideas why this is? Have I set ProcessMSMQMessage event correctly? Or am I missing something?

My code is below. Thanks.

WCF Service Class...

public partial class MyService : ServiceBase
{

  private ServiceHost host;

  public MyService()
  {
    InitializeComponent();
  }

  protected override void OnStart(string[] args)
  {
    string queueName = ConfigurationManager.AppSettings["ProcessMsgQueueName"];
    if (!MessageQueue.Exists(queueName))
    {
      MessageQueue thisQueue = MessageQueue.Create(queueName, true);
      thisQueue.SetPermissions("Everyone", MessageQueueAccessRights.ReceiveMessage);
    }

    try
    {
      Uri serviceUri = new Uri("msmq.formatname:DIRECT=OS:" + queueName);

      // communicate to MSMQ how to transfer and deliver the messages
      MsmqIntegrationBinding serviceBinding = new MsmqIntegrationBinding();
      serviceBinding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;
      serviceBinding.Security.Transport.MsmqProtectionLevel = System.Net.Security.ProtectionLevel.None;

      serviceBinding.SerializationFormat = MsmqMessageSerializationFormat.Binary;

      host = new ServiceHost(typeof(MyService.Service1)); // add watcher class name
      host.AddServiceEndpoint(typeof(MyService.IService1), serviceBinding, serviceUri);
      host.Open();
    }
    catch (Exception ex)
    {
      EventLog.WriteEntry("SERVICE" + ex.Message, EventLogEntryType.Error);
    }
  }

  protected override void OnStop()
  {
    if (host != null)
     host.Close();
  }
}

IService1 Contract...

[ServiceContract(Namespace = "MyService")]
[ServiceKnownType(typeof(Events.Dashboard_Message))]
public interface IService1
{
  [OperationContract(IsOneWay = true)]
  void ProcessMSMQMessage(MsmqMessage<Events.Dashboard_Message> msg);
}

Service1 Class...

public class Service1 : IService1
{
  [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
  public void ProcessMSMQMessage(MsmqMessage<Events.Dashboard_Message> msg)
  {
    string msgName = msg.GetType().Name;

    // send to eventlog
    EventLog.WriteEntry("MyService", msgName);  
  }
}

Upvotes: 1

Views: 1267

Answers (1)

K09
K09

Reputation: 201

Got it working finally.

The issue was in IService1 contract. Needed to add Action = "*".

[OperationContract(IsOneWay = true, Action = "*")]

Upvotes: 1

Related Questions