简体   繁体   English

如何在WCF中序列化受保护的属性

[英]How to serialize a protected property in wcf

I have two classes (Person and Address) that i need to send via wcf, the classes look like this: 我需要通过wcf发送两个类(人员和地址),这些类如下所示:

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. 问题是,当我尝试通过wcf发送它们时,客户端收到的ID属性设置为00000-0000-00000-00000或类似的东西。

Any idea why this is happening? 知道为什么会这样吗? And how to serialize the proper values? 以及如何序列化适当的值?

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. 我只是看了svcutil.exe生成的代码(这就是我用来生成客户端的代码),而我根本找不到Id属性。 I tried changing the protected keyword to public and everything worked fine. 我尝试将protected关键字更改为public,并且一切正常。 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?? Denis-您的WCF客户端使用XmlSerializer( 使用get和set方法序列化公共读取/写入属性)而不是DataContractSerializer的任何可能性? The DataContractSerializer would definitely serialize a protected property or field - it really doesn't care about the .NET visibility modifiers.... DataContractSerializer肯定会序列化受保护的属性或字段-它实际上并不关心.NET可见性修饰符。

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?? 您应该在正在生成的WCF客户端代理中看到这一点-您在那里具有[DataContract][DataMember]属性,还是看到[XmlElement]等? Does your class in the WCF client side proxy have a [DataContractAttribute] or a [XmlTypeAttribute] on it?? WCF客户端代理中的类上是否具有[DataContractAttribute][XmlTypeAttribute]

You should not be marking both the field and the property with DataMember attributes! 您不应同时使用DataMember属性标记字段和属性! 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. 但是基本上通过将字段及其支持属性都标记为DataMember,可以将值序列化两次,然后将其反序列化两次,具体取决于客户端代码的生成方式,这甚至可能导致值存储两次。

So long story short, mark either your fields as DataMember or the properties, but not both. 长话短说,将您的字段标记为DataMember或将属性标记为,但不要同时将两者标记为。 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. 标记字段可能需要您在DataMemberAttribute上指定一个Name,以便客户端代码生成可以创建期望的属性名称。

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. 如果为它们分配值,则应正确发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM