关于wcf调用的System.Windows.Media.Media3D.Vector3d序列化的例外情况

时间:2012-02-20 23:27:53

标签: c# wcf serialization

我正在尝试在wcf上发送一个类,我在序列化这个类System.Windows.Media.Media3D.Vector3d时遇到了问题。我得到了这个例外

尝试序列化参数WEntityService:iState时出错。 InnerException消息是'Type'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具有Seri​​alizable attiribute。对于wcf serialisable类型,如果我引用此网页:http://msdn.microsoft.com/en-us/library/ms731923.aspx Vector3D应该可以为wcf序列化。有人可以解释我为什么没有序列化。 THKS。

1 个答案:

答案 0 :(得分: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();
}
相关问题