THY02K
March 16th, 2009, 09:45 PM
Problem: trying to wire down the following class through WCF and run into deserialization problem:
[KnownType("GetKnownType")]
[DataContract(Name = "Person", Namespace="xxxxx.Util")]
public class Person : IPerson, IGenericObjectOwner
{
#region KnownType attribute - Generic type, REF: http://msdn.microsoft.com/en-us/library/ms730167.aspx
private static Type[] GetKnownType()
{
Type[] t = new Type[3];
// t[0] = typeof(xxxxx.Util.Security.SystemUser); // I purposedly comment this out for reasons below.
t[0] = typeof(xxxxx.Util.Generic.GenericObject);
t[1] = typeof(xxxxx.Util.Security.BasicAudit);
t[2] = typeof(xxxxx.Util.Name);
return t;
}
...
[DataMember()]
public IGenericObject GenericObj { get... set...} // interface IGenericObjectOwner contains an IGenericObject (which contains parent/child Person)
}
[DataContract(Name = "SystemUser", Namespace = "xxxxx.Util.Security")]
[GenericObjectAttribute(ObjectClassifier = "Person")]
public class SystemUser : Person, ISystemUser
{
...
}
public interface IGenericObjectOwner
{
IGenericObject GenericObj
{
get;
set;
}
...
}
public class GenericObject : IGenericObject
{
...
[DataMember()]
public virtual List<IGenericObjectOwner> Parents // IGenericObject has "Parents" and "Children"
{
// Commenting this out fixed the person proxy/serialization problem mentioned below
/*
get { return _lstParents; }
set { _lstParents = value; }
*/
get { return new List<IGenericObjectOwner>(); }
set { }
}
[DataMember()]
public virtual List<IGenericObjectOwner> Children
{
// Commenting this out fixed the person proxy/serialization problem mentioned below
/*
get { return _lstChildren; }
set { _lstChildren = value; }
*/
get { return new List<IGenericObjectOwner>(); }
set { }
}
...
}
I checked Reference.cs,
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISecurityService/GetPerson", ReplyAction="http://tempuri.org/ISecurityService/GetPersonResponse")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxxxx.Util.Security.SystemUser))] << QUESTION: Why is it not xxxxx.Util.Person?
...
xxxxx.Util.Person GetPerson(string strEmail);
And when I actually invoked the Service, I keep getting this error:
Type 'PersonProxy9ab9661107bd4ad9a11c877daa3d0e7d' with data contract name 'Person:xxxxx.Util' is not expected. 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.
I then proceed to modify Reference.cs manually to add xxxxx.Util.Person to known type (replace "xxxxx.Util.Security.SystemUser" above"), I still end up with the same error.
My mind is not really functioning now after long struggles with this last few days but I stumbled on this:
http://eddiedebear.blogspot.com/2008/05/wcf-and-inheritance.html
Not sure if it has to do with IExtensibleDataObject but I gave it a try and did NOT resolve the problem. Read further and figured out that IExtensibleDataObject only has to do with versioning (not what I'm facing right now, see this: http://codeidol.com/csharp/wcf/Data-Contracts/Versioning)
Thanks and hope someone can point me to right direction in this regards.
[KnownType("GetKnownType")]
[DataContract(Name = "Person", Namespace="xxxxx.Util")]
public class Person : IPerson, IGenericObjectOwner
{
#region KnownType attribute - Generic type, REF: http://msdn.microsoft.com/en-us/library/ms730167.aspx
private static Type[] GetKnownType()
{
Type[] t = new Type[3];
// t[0] = typeof(xxxxx.Util.Security.SystemUser); // I purposedly comment this out for reasons below.
t[0] = typeof(xxxxx.Util.Generic.GenericObject);
t[1] = typeof(xxxxx.Util.Security.BasicAudit);
t[2] = typeof(xxxxx.Util.Name);
return t;
}
...
[DataMember()]
public IGenericObject GenericObj { get... set...} // interface IGenericObjectOwner contains an IGenericObject (which contains parent/child Person)
}
[DataContract(Name = "SystemUser", Namespace = "xxxxx.Util.Security")]
[GenericObjectAttribute(ObjectClassifier = "Person")]
public class SystemUser : Person, ISystemUser
{
...
}
public interface IGenericObjectOwner
{
IGenericObject GenericObj
{
get;
set;
}
...
}
public class GenericObject : IGenericObject
{
...
[DataMember()]
public virtual List<IGenericObjectOwner> Parents // IGenericObject has "Parents" and "Children"
{
// Commenting this out fixed the person proxy/serialization problem mentioned below
/*
get { return _lstParents; }
set { _lstParents = value; }
*/
get { return new List<IGenericObjectOwner>(); }
set { }
}
[DataMember()]
public virtual List<IGenericObjectOwner> Children
{
// Commenting this out fixed the person proxy/serialization problem mentioned below
/*
get { return _lstChildren; }
set { _lstChildren = value; }
*/
get { return new List<IGenericObjectOwner>(); }
set { }
}
...
}
I checked Reference.cs,
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISecurityService/GetPerson", ReplyAction="http://tempuri.org/ISecurityService/GetPersonResponse")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(xxxxx.Util.Security.SystemUser))] << QUESTION: Why is it not xxxxx.Util.Person?
...
xxxxx.Util.Person GetPerson(string strEmail);
And when I actually invoked the Service, I keep getting this error:
Type 'PersonProxy9ab9661107bd4ad9a11c877daa3d0e7d' with data contract name 'Person:xxxxx.Util' is not expected. 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.
I then proceed to modify Reference.cs manually to add xxxxx.Util.Person to known type (replace "xxxxx.Util.Security.SystemUser" above"), I still end up with the same error.
My mind is not really functioning now after long struggles with this last few days but I stumbled on this:
http://eddiedebear.blogspot.com/2008/05/wcf-and-inheritance.html
Not sure if it has to do with IExtensibleDataObject but I gave it a try and did NOT resolve the problem. Read further and figured out that IExtensibleDataObject only has to do with versioning (not what I'm facing right now, see this: http://codeidol.com/csharp/wcf/Data-Contracts/Versioning)
Thanks and hope someone can point me to right direction in this regards.