2012-09-03 93 views
1

的對象讀取MSMQ消息時,我得到了以下錯誤消息......MSMQ無法轉換類型

無法投類型的對象System.ServiceModel.MsmqIntegration.MsmqMessage`1 [MSMQLibrary.MyEvents + Dashboard_Message]'鍵入'Dashboard_UserDetail'。

這裏發生錯誤...

MyEvents.Dashboard_UserDetail messageTest = 
    (MyEvents.Dashboard_UserDetail)dashboardMessage; 

這是爲什麼?

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)] 
public void ProcessMSMQMessage(MsmqMessage<MyEvents.Dashboard_Message> msg) 
{ 
    switch (msg.Body.GetType().Name) 
    { 
     case "DashboardTest": 
      MyEvents.Dashboard_UserDetail messageTest = 
       (MyEvents.Dashboard_UserDetail)dashboardMessage; 
      using (UpdateDashboardQueriesDataContext updateDashboardQueriesDataContext = new UpdateDashboardQueriesDataContext()) 
      { 
       updateDashboardQueriesDataContext.UpdateData(messageTest.ID, messageTest.StartDate); 
      } 
      break; 
     } 
    } 


public class MyEvents 
{ 
    [Serializable] 
    [DataContract] 
    public class Dashboard_Message 
    { 
     public Dashboard_Message(); 
    } 

    [Serializable] 
    [DataContract] 
    public class Dashboard_UserDetail : DashboardEvents.Dashboard_Message 
    { 
     public Dashboard_UserDetail(); 
     public Dashboard_UserDetail(string thisID, DateTime thisPeriod); 

     public DateTime Period { get; set; } 
     public string ID{ get; set; } 
    }  
} 
+0

你是否嘗試不將消息類放入MyEvents類(使獨立類,而不是子類)? –

回答

1

因爲您沒有告訴服務期望您的派生類型在請求中。該服務期望在消息中輸入Dashboard_Message,但它正在接收類型爲Dashboard_UserDetail的對象。

您必須使用ServiceKnownType屬性來指定您的服務應該期待請求中的其他類型。

相關問題