none
WCF Service - How to keep private members for a DataContract object? RRS feed

  • Question

  • I have a class named Cube that I'd like to keep most of the operations on the server side. Cube has a private data member A, which is a large array. A is not exposed to the client; therefore, A is not declared as a [DataMember].

    The problem is that every time Cube is passed back and forth between server and client, the contents of A is lost.

    What is the best way to handle A since it has to be associated with Cube?

    Saturday, August 22, 2015 1:07 AM

Answers

  • My question is: how do I handle A? If I put A as a private member in Cube, the contents are lost every time Cube is serialized and deserialized (sent back and forth between server and client).

    You can't I have told you that you can't do it. WCF is not going to allow private access data to travel through it.

    If I place A somewhere else outside class Cube, then how do I access A from Cube? Will a static variable work in this case?

    You need another object other than Cube that you are going to pass to the client without this data you are talking about in it, since the client can't access it anyway. You can put the ID(s) of there other data objects and send it up. Then on the way back you map this data from the custom object back to Cube. You also take the ID(s) of this mysterious data and you populate Cube again with this data by getting by its ID. 

    I have told where it should happen in the Business Layer who has the responsibility to make the decision on how it is going to pass data between the WCF service and client and the other way around.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://en.wikipedia.org/wiki/Data_transfer_object

    http://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

    If you don't understand N-Tier, then that is where you need to start to solve your problem.

    You have to figure it out yourself

    • Marked as answer by T. J. Fan Monday, August 31, 2015 11:21 PM
    Tuesday, August 25, 2015 1:13 AM
  • Hi T.J.Fan,

    Such as DA924 said, the >>WCF is not going to allow private access data to travel through it.

    If we want to return a array object we can use data contract with data member.

    For example, I create a project I read a file to a byte array. Then I can write the array to a file.

    Also, you can return the byte array. Code shown below:

    [DataContract]
    public class arrayFile
    {
            [DataMember]
            public string Title { get; set; }
            [DataMember]
            public byte[] Data { get; set; }
    }
    
    public class Service : IService
        {
           public arrayFile  IService.DownloadFile()
            {
                arrayFile  myFile = new File();
                myFile.Title = "WCF  Service";
                myFile.Data = File.ReadAllBytes(@"D:\MyFile\WCF Service.doc"); 
                return myFile;
            }
        }

    Then client may be like below:

    ServiceReference1.ServiceClient client = new ServiceClient();
    client.arrayFile myFile = client.DownloadFile();
    File.WriteAllBytes(@"D:\MyDownloadedFile\" +myFile.Title, myFile.Data);

    I hope that will be helpful to you.

    Best Regards,

    Grady

    Tuesday, August 25, 2015 3:12 AM
    Moderator

All replies

  • The Datamember attribute has to be on the property, and it has to be public, otherwise it is not going to be sent, if I recall correctly.  You shouldn't send any data that you don't want accessible.
    Saturday, August 22, 2015 1:51 AM
  • That's exactly what my problem is. I have so many private data members that I don't want them to be accessible by the client, but I lose them (contents all become null) when the object is passed back and forth between server and client.

    What should I do now?

    Monday, August 24, 2015 9:11 PM
  • What should I do now?

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://en.wikipedia.org/wiki/Multitier_architecture

    http://imar.spaanjaars.com/573/aspnet-n-layered-applications-introduction-part-1

    It is the Business Layer objects that should be making the decision as to what objects are to be accessible. It may have to use a new List of custom objects that are accessible without the data you are talking about and send it on. It will be the Business Layer objects that will return the objects to WCF with the needed data objects  as data anyway you can get them back into the List and send them on.

    BLL

    WCF

    DAL

    OR

    WCF

    BLL

    DAL

    There is no reason that the Business object can't create data objects with things as private, but sending data through WCF it has to be public.

    Monday, August 24, 2015 11:14 PM
  • Maybe I did not state my problem in a more specific way. Let me try again.

    Say I have an object named Cube. It contains several data members such as MaxRows and MaxCols. These are declared as [DataMember] and sent back and forth between server and client. No problem.

    Suppose that internally there is a large array double[,] A that stores all the binary data of Cube. This array A is never accessed by the client; therefore, it should stay at the server side. There are a lot of internal operations on A that read/write/update contents of A and they should all be handled by the server and never made known to the client.

    My question is: how do I handle A? If I put A as a private member in Cube, the contents are lost every time Cube is serialized and deserialized (sent back and forth between server and client).

    If I place A somewhere else outside class Cube, then how do I access A from Cube? Will a static variable work in this case?

    Tuesday, August 25, 2015 12:41 AM
  • My question is: how do I handle A? If I put A as a private member in Cube, the contents are lost every time Cube is serialized and deserialized (sent back and forth between server and client).

    You can't I have told you that you can't do it. WCF is not going to allow private access data to travel through it.

    If I place A somewhere else outside class Cube, then how do I access A from Cube? Will a static variable work in this case?

    You need another object other than Cube that you are going to pass to the client without this data you are talking about in it, since the client can't access it anyway. You can put the ID(s) of there other data objects and send it up. Then on the way back you map this data from the custom object back to Cube. You also take the ID(s) of this mysterious data and you populate Cube again with this data by getting by its ID. 

    I have told where it should happen in the Business Layer who has the responsibility to make the decision on how it is going to pass data between the WCF service and client and the other way around.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://en.wikipedia.org/wiki/Data_transfer_object

    http://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

    If you don't understand N-Tier, then that is where you need to start to solve your problem.

    You have to figure it out yourself

    • Marked as answer by T. J. Fan Monday, August 31, 2015 11:21 PM
    Tuesday, August 25, 2015 1:13 AM
  • Hi T.J.Fan,

    Such as DA924 said, the >>WCF is not going to allow private access data to travel through it.

    If we want to return a array object we can use data contract with data member.

    For example, I create a project I read a file to a byte array. Then I can write the array to a file.

    Also, you can return the byte array. Code shown below:

    [DataContract]
    public class arrayFile
    {
            [DataMember]
            public string Title { get; set; }
            [DataMember]
            public byte[] Data { get; set; }
    }
    
    public class Service : IService
        {
           public arrayFile  IService.DownloadFile()
            {
                arrayFile  myFile = new File();
                myFile.Title = "WCF  Service";
                myFile.Data = File.ReadAllBytes(@"D:\MyFile\WCF Service.doc"); 
                return myFile;
            }
        }

    Then client may be like below:

    ServiceReference1.ServiceClient client = new ServiceClient();
    client.arrayFile myFile = client.DownloadFile();
    File.WriteAllBytes(@"D:\MyDownloadedFile\" +myFile.Title, myFile.Data);

    I hope that will be helpful to you.

    Best Regards,

    Grady

    Tuesday, August 25, 2015 3:12 AM
    Moderator