首页 文章

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

提问于
浏览
0

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

尝试序列化参数WEntityService时出错:iState . InnerException消息是'Type ' System.Windows.Media.Media3D.Vector3D ' with data contract name ' Vector3D:http://schemas.datacontract.org/2004/07/System.Windows.Media.Media3D ' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to 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具有Serializable attiribute . 对于wcf serialisable类型,如果我引用此网页:http://msdn.microsoft.com/en-us/library/ms731923.aspx Vector3D应该可以序列化为wcf . 有人可以解释我为什么没有序列化 . THKS .

1 回答

  • 0

    你能将Vector3D添加到已知类型列表中吗?请参阅下面的数据 Contract 级别的示例 . 我认为应该解决你的问题 .

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

    如果您无法在数据协定级别添加已知类型并且必须仅在服务 Contract 级别添加它,则可以执行以下操作 - 添加[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();
    }
    

相关问题