THY02K
March 13th, 2009, 10:41 AM
hello
I'm running into Serialization problem with WCF again and I'm not sure if it arise from namespace problem or KnownType/interface...
[DataContract(Name = "Person")]
public class Person : IHierachicalPropOwner
{
...
IHierarchProp HierarchicalProp
{
get {...}
set {...}
}
...
}
public interface IHierarchicalPropOwner
{
...
IHierarchProp HierarchicalProp;
...
}
[KnownType(typeof(Person))] // Because List<IHierarchicalPropOwner> is actually List<Person>, I include Person as KnownType as well.
[DataContract()]
public class HierarchicalProp : IHierarchProp
{
...
[DataMember()]
public List<IHierarchicalPropOwner> Parents
{
get ...
set ...
}
[DataMember()]
public List<IHierarchicalPropOwner> Children
{
get ...
set ...
}
...
}
Now all seems good initially up until I added Parents/Children (from IHierarchProp) - i.e. Person class serialize/deserialize successfully BEFORE I added Parents/Children (which are also of type Person) ... I checked SrvTraceViewer, the message was:
Type 'PersonProxy84be8e34abd44adf9c275dce3b694daf' cannot have DataContractAttribute attribute Namespace set to null.
I don't know why I must add a namespace but I did change Person's DataContract attrib:
[DataContract(Name = "Person", Namespace="Util")]
public class Person : IHierachicalPropOwner
{
...
}
I then fired it up again, and ran into a different error message
There was an error while trying to serialize parameter http://tempuri.org/:AuthenticateUserResult. The InnerException message was 'Type 'PersonProxy9ae63f0c5e1b403881f64f08908cf759' with data contract name 'Person: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.'. Please see InnerException for more details
Adding to service contract ServiceKnownType didn't help it but I made sure I have...
[ServiceKnownType(typeof(Person))]
Now I was suspecting perhaps DataContractSerializer need Person's namespace as specified in [DataContract] namespace attib be same as that of "Service Reference" when you "Add Service Reference" from Visual Studio (you set service namespace at that time). So I go ahead and make the change:
[DataContract(Name = "Person", Namespace="SecurityServiceRef")]
public class Person : IHierachicalPropOwner
{
...
}
The error did NOT go away,
There was an error while trying to serialize parameter http://tempuri.org/:AuthenticateUserResult. The InnerException message was 'Type 'PersonProxyebe18eee73cd41db908097f0886b787e' with data contract name 'Person:ServiceServiceRef' 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.'. Please see InnerException for more details.
Any idea? Many Thanks!
REF:
http://msdn.microsoft.com/en-us/library/ms730167.aspxvv
I'm running into Serialization problem with WCF again and I'm not sure if it arise from namespace problem or KnownType/interface...
[DataContract(Name = "Person")]
public class Person : IHierachicalPropOwner
{
...
IHierarchProp HierarchicalProp
{
get {...}
set {...}
}
...
}
public interface IHierarchicalPropOwner
{
...
IHierarchProp HierarchicalProp;
...
}
[KnownType(typeof(Person))] // Because List<IHierarchicalPropOwner> is actually List<Person>, I include Person as KnownType as well.
[DataContract()]
public class HierarchicalProp : IHierarchProp
{
...
[DataMember()]
public List<IHierarchicalPropOwner> Parents
{
get ...
set ...
}
[DataMember()]
public List<IHierarchicalPropOwner> Children
{
get ...
set ...
}
...
}
Now all seems good initially up until I added Parents/Children (from IHierarchProp) - i.e. Person class serialize/deserialize successfully BEFORE I added Parents/Children (which are also of type Person) ... I checked SrvTraceViewer, the message was:
Type 'PersonProxy84be8e34abd44adf9c275dce3b694daf' cannot have DataContractAttribute attribute Namespace set to null.
I don't know why I must add a namespace but I did change Person's DataContract attrib:
[DataContract(Name = "Person", Namespace="Util")]
public class Person : IHierachicalPropOwner
{
...
}
I then fired it up again, and ran into a different error message
There was an error while trying to serialize parameter http://tempuri.org/:AuthenticateUserResult. The InnerException message was 'Type 'PersonProxy9ae63f0c5e1b403881f64f08908cf759' with data contract name 'Person: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.'. Please see InnerException for more details
Adding to service contract ServiceKnownType didn't help it but I made sure I have...
[ServiceKnownType(typeof(Person))]
Now I was suspecting perhaps DataContractSerializer need Person's namespace as specified in [DataContract] namespace attib be same as that of "Service Reference" when you "Add Service Reference" from Visual Studio (you set service namespace at that time). So I go ahead and make the change:
[DataContract(Name = "Person", Namespace="SecurityServiceRef")]
public class Person : IHierachicalPropOwner
{
...
}
The error did NOT go away,
There was an error while trying to serialize parameter http://tempuri.org/:AuthenticateUserResult. The InnerException message was 'Type 'PersonProxyebe18eee73cd41db908097f0886b787e' with data contract name 'Person:ServiceServiceRef' 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.'. Please see InnerException for more details.
Any idea? Many Thanks!
REF:
http://msdn.microsoft.com/en-us/library/ms730167.aspxvv