Reputation: 607
I'm trying to send a class on wcf, I am having problems to serialize this class System.Windows.Media.Media3D.Vector3d. I get this exception
There was an error while trying to serialize parameter WEntityService:iState.
The InnerException message was '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.'.
Please see InnerException for more details.
[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))
{
}
On the msdn web site http://msdn.microsoft.com/en-us/library/ms606682.aspx, you can see that Vector3D has Serializable attiribute. For the wcf serialisable type if I refer to this web page: http://msdn.microsoft.com/en-us/library/ms731923.aspx Vector3D should be serializable for the wcf. Can someone explain me why It's not serialized. Thks.
Upvotes: 0
Views: 357
Reputation: 1918
Can you just add Vector3D to the list of known types? See example below at the data contract level. I think that should fix your problem.
[DataContract]
public class Book { }
[DataContract]
public class Magazine { }
[DataContract]
[KnownType(typeof(Book))]
[KnownType(typeof(Magazine))]
public class LibraryCatalog
{
[DataMember]
System.Collections.Hashtable theCatalog;
}
If you can't add a known type at the data contract level and have to add it at only the service contract level, you can do something like below -- add a [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();
}
Upvotes: 0