【问题标题】:Exception on System.Windows.Media.Media3D.Vector3d serialisation on wcf callwcf 调用上 System.Windows.Media.Media3D.Vector3d 序列化的异常
【发布时间】:2012-02-20 23:27:53
【问题描述】:

我正在尝试在 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属性。对于 wcf 可序列化类型,如果我参考此网页:http://msdn.microsoft.com/en-us/library/ms731923.aspx Vector3D 对于 wcf 应该是可序列化的。有人可以解释一下为什么它没有被序列化。谢谢。

【问题讨论】:

    标签: c# wcf serialization


    【解决方案1】:

    你能把 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多