Search code examples
c#.netwcfserialization

Return public members of BL object


I'm trying to return a BL object from WCF server, but I get all the private properties only.

How to return only the BL public properties?

That's the BL Class in some DLL I refernce to WCF server:

[Serializable()]
public class Account
{
    #region properties

    private int _accountId;
    public int AccountID
    {
        get
        {
            return _accountId;
        }
        set
        {
            _accountId = value;
        }
    }

    
    private string _title;
   public string Title
    {
        get
        {
           return _title;
        }
        set
        {
            _title= value;
        }
    }


    private string _name;
   public string Name
    {
        get
        {
            return _name;
        }
        set
        {
           _name= value;
        }
    }}

That's the WCF method in the interface:

   [OperationContract]
   [WebGet(UriTemplate = "{Key}/{Client}/Registrations?eventID={eventID}&regStatus={regStatus}")]
    List<Registration> GetRegistrations(string key, string client, int eventID, int regStatus);

The method that makes the return list:

public List<Registration> GetRegistrations(string key, string client, int eventID, int regStatus)
    {
        if (Validation.ValidateClient(key, client) == false)
            return null;

        List<Registration> regs = Registration.GetRegByColumnandValue(eventID, (Registration.RegStatusFlags)regStatus);
        return regs;
    }

Solution

  • WCF does not use the [Serializable] attribute.

    Write your BL object (or a DTO ) like this:

    [DataContract]
    public class Account
    {  
        private int _accountId;    
        [DataMember]
        public int AccountID
        {
           // get/set
        }
    
        private string _title;
        [DataMember]    
        public string Title
        {
           // get/set
        }
    }
    

    The [Serializable] is wrecking the way the type is serialized, it's not suitable for WCF.

    If it is embedded in another layer and you cannot remove it, then create a DTO (Data Transfer Object) class. You'll need code to copy the relevant properties, at least server-side.