MSMQ无法转换类型的对象

时间:2012-09-03 13:54:04

标签: wcf 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; }
    }    
}

1 个答案:

答案 0 :(得分:1)

因为您没有告诉服务在请求中期望您的派生类型。该服务期望消息中的类型Dashboard_Message,而是接收类型为Dashboard_UserDetail的对象。

您必须使用ServiceKnownType属性来指定您的服务应该在请求中预期其他类型。