2012-02-20 36 views
0

我試圖發送一個類的wcf,我有問題序列化這個類System.Windows.Media.Media3D.Vector3d。我得到這個異常wcf調用System.Windows.Media.Media3D.Vector3d序列化的異常

嘗試序列化參數WEntityService:iState時發生錯誤。 InnerException消息是'類型'System.Windows.Media.Media3D.Vector3D'與數據合同名稱 'Vector3D:http://schemas.datacontract.org/2004/07/System.Windows.Media.Media3D'不是預期。考慮 使用DataContractResolver或將任何不知道的類型靜態添加到已知類型的列表中 - 例如,使用KnownTypeAttribute屬性的 或將它們添加到傳遞給DataContractSerializer的已知類型列表中。
有關更多詳細信息,請參閱InnerException。

[DataContract] 
    public ref class WData 
    { 
    public: 
    WData(); 

    [DataMember] 
    Vector3D^ mLinearVelocity; 

    [DataMember] 
    System::String^ mName; 
    }; 

WData::WData() 
    : mLinearVelocity(gcnew Vector3D(0.0, 0.0, 0.0))   
    , mName(gcnew System::String(' ', 1)) 
    { 

    } 

在MSDN網站http://msdn.microsoft.com/en-us/library/ms606682.aspx,你可以看到的Vector3D具有序列化attiribute。對於wcf serialisable類型,如果我參考這個網頁:http://msdn.microsoft.com/en-us/library/ms731923.aspx Vector3D應該是可序列化的wcf。有人可以解釋爲什麼它沒有序列化。 THKS。

回答

0

你可以直接將Vector3D添加到已知類型的列表中嗎?請參閱數據合同級別下面的示例。我認爲這應該解決你的問題。

[DataContract] 
public class Book { } 

[DataContract] 
public class Magazine { } 

[DataContract] 
[KnownType(typeof(Book))] 
[KnownType(typeof(Magazine))] 
public class LibraryCatalog 
{ 
    [DataMember] 
    System.Collections.Hashtable theCatalog; 
} 

如果你不能在數據契約級別添加已知類型,並有它僅在服務合同級別增加,你可以不喜歡下面 - 添加[ServiceKnownTypeAttribute]!

// Apply the ServiceKnownTypeAttribute to the 
// interface specifying the type to include. Apply 
// the attribute more than once if needed. 
[ServiceKnownType(typeof(Widget))] 
[ServiceKnownType(typeof(Machine))] 
[ServiceContract()] 
public interface ICatalog2 
{ 
    // Any object type can be inserted into a Hashtable. The 
    // ServiceKnownTypeAttribute allows you to include those types 
    // with the client code. 
    [OperationContract] 
    Hashtable GetItems(); 
}