CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Feb 2001
    Posts
    872

    WCF serialization - namespace? or KnownType/interface problem?

    hello

    I'm running into Serialization problem with WCF again and I'm not sure if it arise from namespace problem or KnownType/interface...

    Code:
    [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:
    Code:
    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:
    Code:
    [DataContract(Name = "Person", Namespace="Util")]
    public class Person : IHierachicalPropOwner
    {
    ...
    }
    I then fired it up again, and ran into a different error message
    Code:
    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...
    Code:
    [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:
    Code:
    [DataContract(Name = "Person", Namespace="SecurityServiceRef")]
    public class Person : IHierachicalPropOwner
    {
    ...
    }
    The error did NOT go away,
    Code:
    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
    Last edited by THY02K; March 13th, 2009 at 10:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured