CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: WCF exception

  1. #1
    Join Date
    Mar 2008
    Posts
    24

    WCF exception

    Hi all,
    I am developing a WCF application and i can send simple DataContract classes without problems. But when i try to send a class having a different type of class as a field, i get CommunicationException. Class structure is like below;
    Code:
        [DataContract]
        public class ClassA
        {
            [DataMember]
            public ClassB  myObject1;
    
            [DataMember]
            public ClassC  myObject2;
        }
    And i also defined both ClassB and ClassC as DataContract. I don't know if i am doing something wrong. Any help would be appreciated.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: WCF exception

    what is the message and innerexception of the CommuncationException.

    Sound like you did the correct thing, but without any more information about the exception, we can't help you

  3. #3
    Join Date
    Mar 2008
    Posts
    24

    Re: WCF exception

    Thanks for the reply Danny,
    The reason of the CommunicationException seems that WCF cannot serialize the class B and class C objects in class A. I made some search and found out that i should use Surrogates to resolve that problem to make those classes serializable. But it requires much work to do that since if i use Surrogates i need to create Surrogate classes for class A and B to make them known types for the WCF service. And i found another thing showing that we can use KnownType or ServiceKnownType attributes for those inner class objects but all examples about those attributes are implemented on base class objects such as using a base Person class for both Employee and Employer classes.
    Is there any other way to manage that.
    Thanks in advance


    Quote Originally Posted by dannystommen View Post
    what is the message and innerexception of the CommuncationException.

    Sound like you did the correct thing, but without any more information about the exception, we can't help you

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: WCF exception

    Following is a codesnippet from my own project, which works fine only using DataContract and DataMember attributes.

    Code:
       [DataContract]
       public class User {
    
          [DataMember]
          public int ID {
             ...
          }
    
          [DataMember]
          public string Username {
             ...
          }
    
          [DataMember]
          public UserRole UserRole {
             ...
          }
       }
    
       [DataContract]
       public class UserRole {
          [DataMember]
          public int ID {
             ...
          }
    
          [DataMember]
          public string Description {
             ...
          }
       }
    As you can see, it has the same syntax as your example. I don't use Surrogates or (Service)KnownType, and it works just fine.


    The reason of the CommunicationException seems that WCF cannot serialize the class B and class C objects in class A
    Can you please quote the CommuncationException (message and innerexception), instead of saying only what is causing it.

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