Home How to set [DataMember] on an autogenerated proxy property
Reply: 1

How to set [DataMember] on an autogenerated proxy property

Guido Dell Olivo
1#
Guido Dell Olivo Published in 2016-05-23 17:35:09Z

I am working on a wcf webservice. This service uses a third party webservice which I have added as a service reference. Now I want to publish some properties of this proxyclient to clients who uses my wcfservice, without defining an own class and doing the mapping.

The auto generated code is done as partial class.

    public partial class Person : object,
     System.ComponentModel.INotifyPropertyChanged 
         {
         public string FirstName;
         public string LastName;
...
         }

I tried to override these properties by using the MetadataType-Attribute and adding the [DataMember]-Attribute to properties. But this seams to work only for EF.

[DataContract]
[MetadataType(typeof(PersonMetaData))]
public partial class Person 
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}

public class PersonMetaData
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}

Trying to implement an interface didn't help, the properties are invisible on client.

[DataContract]
public partial class Person : IPerson
{}

public interface IPerson
{
     [DataMember]
     string FirstName { get; set; }

     [DataMember]
     string LastName { get; set; }
}

Any idea?

Guido

Wcf
Guido Dell Olivo
2#
Guido Dell Olivo Reply to 2016-05-27 18:15:58Z

On my service XmlSerializer failed to serialize the auto-generated class cause of PropertyChanged-event.
If I work with DataContractSerializer and decorate my auto-generated class with [DataContract], I'm not able to decorate the properties by inheritance with [DataMember] because the attibute is not inheritable.

So I extended this partial class by wrapper properties.

[DataContract]
public partial class Person 
{
    [DataMember]
    public string FirstNameWrapper 
    {
        get
        {
            return this.FirstName;
        }
        set
        { 
            this.FirstName = value; 
        }
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.344818 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO