WCF服务不处理MSMQ消息

时间:2012-09-03 08:54:21

标签: wcf msmq

我有一个检查MSMQ消息的WCF Windows服务。 它接收消息,但似乎没有调用ProcessMSMQMessage事件。

为什么会这样?我是否正确设置了ProcessMSMQMessage事件?或者我错过了什么?

我的代码如下。感谢。

WCF服务类......

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合同......

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

1 个答案:

答案 0 :(得分:1)

终于有了工作。

问题出在IService1合同中。需要添加Action = "*"

[OperationContract(IsOneWay = true, Action = "*")]
相关问题