2012-09-03 166 views
1

我有一個WCF Windows服務檢查MSMQ消息。 它選擇了消息,但是ProcessMSMQMessage事件似乎沒有被調用。WCF服務沒有處理MSMQ消息

任何想法爲什麼這樣?我是否正確設置了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類...

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); 
    } 
} 
+0

終於搞定了! 問題出現在IService1合同中。需要添加行動=「*」以下 [OperationContract(IsOneWay = true,Action =「*」)] – K09

+0

我得到這個...哎呀!您的答案無法提交,因爲: •聲譽低於10的用戶在提問後8小時內無法回答自己的問題。你可以在17分鐘內自行回答。在此之前請使用評論,或者編輯您的問題。 – K09

+0

這很好。錯誤消息告訴你發生了什麼。對用戶有一定的時間限制,取決於你的聲譽。同時。此限制已過期,現在您可以將答案放入新帖子中。 –

回答

1

得到它最後的工作。

問題出在IService1合同中。需要添加Action = "*"

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

...現在不要忘記將其標記爲可接受的解決方案。 ;-) –

+0

請將此標記爲可接受的解決方案。 –