views:

126

answers:

3

I have two classes (Person and Address) that i need to send via wcf, the classes look like this:

public class PersoanaFizica :IExtensibleDataObject
{
    [DataMember]
    private Guid _id;
    [DataMember(Name = "Id")]
    protected virtual Guid Id
    {
        get { return _id; }
        set { _id = value; }
    }

    private ExtensionDataObject _extensionData;
    public virtual ExtensionDataObject ExtensionData
    {
        get
        {
            return _extensionData;
        }
        set
        {
            _extensionData = value;
        }
    }


    private string _firstName;
    [Searchable(PropertyName="FirstName")]
    [DataMember]
    public virtual string FirstName 
    {
        get { return this._firstName; }
        set { this._firstName = value; }
    }

    private string _lastName;
    [Searchable(PropertyName="LastName")]
    [DataMember]
    public virtual string LastName 
    {
        get { return this._lastName; }
        set { this. _lastName = value; }
    }

    private Address _address;
    [Searchable(PropertyName="Address")]
    [DataMember]
    public virtual Address Address 
    {
        get { return this._address; }
        set { this._address = value; }
    }
}

public class Address : IExtensibleDataObject
{
 [DataMember]
    private Guid _id;
    [DataMember]
    public virtual Guid Id
    {
        get { return _id; }
        set { _id = value; }
    }

    private ExtensionDataObject _extensionData;
    public virtual ExtensionDataObject ExtensionData
    {
        get
        {
            return _extensionData;
        }
        set
        {
            _extensionData = value;
        }
    }

        private string _country;
    [Searchable(PropertyName="Country")]
    [DataMember]
    public virtual string Country 
    {
        get { return this._country; }
        set { this._country = value; }
    }
// and some other properties related to the address
}

The problem is that when i try to send them via wcf, the client receives the Id properties set to 00000-0000-00000-00000 or something like this.

Any idea why this is happening? And how to serialize the proper values?

A: 

You don't initialize the properties in some way, so they have their default value assigned. If you assign a value to them, that should be properly send.

Femaref
I don't know. I'm using nhibernate to extract the data from the db and then just send them to the client (nothing else). And when i debug the code, the values are ok just before sending.
Denis Rosca
+1  A: 

You should not be marking both the field and the property with DataMember attributes! I have a feeling this is probably what's causing the issue you're seeing but I don't know that for sure. But basically by marking both the field and its backing property as DataMember's you are serializing the value twice and it will be deserialized twice and depending upon how your client-side code is generated this may even result in storing the value twice.

So long story short, mark either your fields as DataMember or the properties, but not both. Marking the fields may require you to specify a Name on the DataMemberAttribute in order for the client-side code generation to create the expected property names.

Josh Einstein
+1 good catch! Yes, this would definitely cause odd behaviors...
marc_s
Also don't forget to re-generate the client-side generated code (right click -> update service reference if using VS code generation) after making the change.
Josh Einstein
nope... did not help.I just looked at the code generated by svcutil.exe (thats what i use to generate my client) and i couldn't find an Id property at all. I tried changing the protected keyword to public and everything worked fine.It seems that the problem is with the fact that i need the properties to be protected and not public.
Denis Rosca
+2  A: 

nope... did not help. I just looked at the code generated by svcutil.exe (thats what i use to generate my client) and i couldn't find an Id property at all. I tried changing the protected keyword to public and everything worked fine. It seems that the problem is with the fact that i need the properties to be protected and not public.

Denis - any chance at all that your WCF client uses the XmlSerializer (which only serializes public read/write properties with a get and set method) instead of the DataContractSerializer?? The DataContractSerializer would definitely serialize a protected property or field - it really doesn't care about the .NET visibility modifiers....

You should see this in the WCF client side proxy being generated - do you have [DataContract] and [DataMember] attributes there, or do you see [XmlElement] and so forth?? Does your class in the WCF client side proxy have a [DataContractAttribute] or a [XmlTypeAttribute] on it??

marc_s
I use [DataContract] and the generated client code does have [DataContract] and [DataMemeber] on it. But stil no protected properties.
Denis Rosca
Ok, please disregard my last comment. I definitly used the DataContractSerializer before. Maybe i was doing something else wrong, because now after a few changes in the morning (a good night sleep does wonders) it works. Have no idea why.
Denis Rosca