Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 122551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:08:48+00:00 2026-05-11T04:08:48+00:00

I’m a bit new to WCF and I don’t think I completely understand what

  • 0

I’m a bit new to WCF and I don’t think I completely understand what the deal is with DataContracts. I have this ‘RequestArray’ class:

[DataContract] public class RequestArray {   private int m_TotalRecords;   private RequestRecord[] m_Record;    [System.Xml.Serialization.XmlElement]   [DataMember]   public RequestRecord[] Record   {      get { return m_Record; }   }    [DataMember]   public int TotalRecords   {     get { return m_TotalRecords; }     set {       if (value > 0 && value <= 100) {         m_TotalRecords = value;     m_Record = new RequestRecord[value];     for (int i = 0; i < m_TotalRecords; i++)       m_Record[i] = new RequestRecord();       }     }   } } 

The idea is when the client says requestArray.TotalRecords=6; the Record array will be allocated and initialized (I realize that I’m hiding an implementation behind an assignment, this is out of my control).

The problem is that when the client does this, the TotalRecord’s set code is not called, breakpoints in the service confirm this as well. Rather, some sort of generic setter has been generated that is called instead. How do I get the client to use my setter instead?

EDIT: It looks like I didn’t quite get how the [DataContract] works, but it makes sense that the client wouldn’t be executing this code. Like I mentioned in a comment, if I ‘manually’ do the work of the setter, I see that the set code does get executed right when I call the service function.

The serialization I’m still uncertain on. The contents of the RequestRecord[] array are not getting carried over. The Record class has setters/getters, I feel like I need a helper function somewhere to help it serialize the whole class.

Thanks all for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:08:48+00:00Added an answer on May 11, 2026 at 4:08 am

    The premise that the client proxy will execute the same code on the client and on the server is flawed because WCF is based on interfaces. I explain this point in bullet #2 below.

    Rules of Sharing WCF Interfaces & Implementation

    If you want to share the implementation of the data contract you will need to factor the RequestArray class into a class library that holds NOTHING BUT the data contract classes, including presumably also the RequestRecord class.

    Rules I live by:

    1. Group all data contracts by themselves (100%) into one or more assemblies, no exceptions.

    2. Group all service contracts into one or more assemblies.

    3. Group all service types, i.e., the classes that implement the service contract, into one ore more assemblies.

    4. Group all client channel proxies, i.e., the class that invokes the methods defined on the service contract interface, into one or more assemblies.

    5. In a generic framework where all client software operates as a WCF service (I avoid duplex connections), it is safe to merge rules 2, 3 & 4 so that service contracts, service types and channel proxies are grouped together into one assembly.

    The main reason for separating the interfaces into a more flexible dependency chain is that it is possible to deploy a limited set of assemblies to the client without exposing unnecessary and potentially proprietary implementation details. Another reason is that it makes refactoring so much easier, especially in cases where you want to implement or extend a generic framework through inheritance or delegation.

    Examining the Code

    There are a few BIG problems with the code for RequestArray…

    1. The setter logic will overwrite any of the modified elements of the m_Record array variable when the DataContract instance is deserialized. This violates deserialization principles.

    2. The Record property will not be able to be deserialized because the Record property on the RequestArray class is Read-only (since it lacks a setter). Generally, I find that for DataContract classes the best approach for read-only properties is simply a method. It is a bad idea to get into the habit of treating data contracts like they are anything more than bit buckets. What the attributes are basically doing is dynamically creating an interface definition specifically used for data serialization and deserialization. I believe that it is a mistake to think of the data on the wire as objects. Rather, it is an opt-in way of specifying the relevant data parts of an object that need to persist over the wire.

    3. The TotalRecords property becomes dangerous if it ends up (correctly) just allowing the m_TotalRecords variable to be set, since it will be totally independent of the internal array. In order for me to get this to work acceptably in my sample code (below), I had to shield the set with if (m_TotalRecords == 0). In the sample code I have saved for future use, I comment out the TotalRecords property altogether, but I leave m_TotalRecords just to illustrate that the private object is in fact preserved over the wire.

    Fixed Code

    I adapted bendewey’s sample code (thanks!) and came up with this complete test. Note: I had to define RequestRecord. Also, please see the code comments. If there are any bugs or anything that is unclear, please let me know.

    #region WCFDataContractTest     [DataContract] // The enclosed type needs to also be attributed for WCF     public class RequestRecord     {         public RequestRecord() { }         [DataMember] // This is CRUCIAL, otherwise the Name property will not be preserved.         public string Name { get; set; }     }     [DataContract] // Encloses the RequestRecord type     public class RequestArray     {         private int m_TotalRecords; // should be for internal bookkeeping only         private RequestRecord[] m_Record;          [System.Xml.Serialization.XmlElement]         [DataMember]         public RequestRecord[] Record         {             get { return m_Record; }             // deserialization will not work without the set             set { m_Record = value; }         }          [DataMember] // is not really needed         public int TotalRecords         {             get { return m_TotalRecords; }             set             {                 if (m_TotalRecords == 0)                     m_TotalRecords = value;             }         }          // The constructor is not called by the deserialization mechanism,         // therefore this is the right place to specify the array size and to         // perform the array initialization.         public RequestArray(int totalRecords)         {             if (totalRecords > 0 && totalRecords <= 100)             {                 m_TotalRecords = totalRecords;                 m_Record = new RequestRecord[totalRecords];                 for (int i = 0; i < m_TotalRecords; i++)                     m_Record[i] = new RequestRecord() { Name = 'Record #' + i.ToString() };                  m_TotalRecords = totalRecords;             }             else                 m_TotalRecords = 0;         }     }     public static void TestWCFDataContract()     {         var serializer = new DataContractSerializer(typeof(RequestArray));          var test = new RequestArray(6);          Trace.WriteLine('Array contents after 'new':');         for (int i = 0; i < test.Record.Length; i++)             Trace.WriteLine('\tRecord #' + i.ToString() + ' .Name = ' + test.Record[i].Name);          //Modify the record values...         for (int i = 0; i < test.Record.Length; i++)             test.Record[i].Name = 'Record (Altered) #' + i.ToString();          Trace.WriteLine('Array contents after modification:');         for (int i = 0; i < test.Record.Length; i++)             Trace.WriteLine('\tRecord #' + i.ToString() + ' .Name = ' + test.Record[i].Name);          using (var ms = new MemoryStream())         {             serializer.WriteObject(ms, test);              ms.Flush();             ms.Position = 0;              var newE = serializer.ReadObject(ms) as RequestArray;              Trace.WriteLine('Array contents upon deserialization:');             for (int i = 0; i < newE.Record.Length; i++)                 Trace.WriteLine('\tRecord #' + i.ToString() + ' .Name = ' + newE.Record[i].Name);         }     } #endregion 

    The listing for this sample program, after running TestWCFDataContract is:

    Array contents after ‘new’:

        Record #0 .Name = Record #0     Record #1 .Name = Record #1     Record #2 .Name = Record #2     Record #3 .Name = Record #3     Record #4 .Name = Record #4     Record #5 .Name = Record #5 

    Array contents after modification:

        Record #0 .Name = Record (Altered) #0     Record #1 .Name = Record (Altered) #1     Record #2 .Name = Record (Altered) #2     Record #3 .Name = Record (Altered) #3     Record #4 .Name = Record (Altered) #4     Record #5 .Name = Record (Altered) #5 

    Array contents upon deserialization:

        Record #0 .Name = Record (Altered) #0     Record #1 .Name = Record (Altered) #1     Record #2 .Name = Record (Altered) #2     Record #3 .Name = Record (Altered) #3     Record #4 .Name = Record (Altered) #4     Record #5 .Name = Record (Altered) #5 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.