开发者

WCF, generated DataMember List<> proxy class property is null?

I'm working with a simple object in WCF decorated with the DataContract and DataMember attributes. I have a List<T> property and purposely designed it to instantiate the backing field on first access (if null). An abbreviated version of the class is below.

[DataContract]
public class FieldSetData 
{
    private List<FormFieldData> _formFields;

    [DataMember]
    public List<FormFieldData> FormFields
    {
        get
        {
            if (this._formFields == null)
            {
                this._formFields = new List<FormFieldData>();
            }
            return this._formFields;
        }
        set
        {
            this._formFields = value;
        }
    }
}

The problem is, on the generated client/proxy class, I can't access the property without instantiating it manually the first time because it's null (which is what the if logic above was supposed to handle).

The second line of code below returns null:

//proxy class version
FieldSetData data = new FieldSetData();
data.FormFields.Add(new FormFieldData());  //FormFields property is null

I have to do this instead:

//instantiate the List<T> property
FieldSetData data = new FieldSetData { FormFields = new List<FormFieldData>() };
data.FormFields.Add(new FormFieldData());

I'm fairly new to WCF so perhaps I'm missing something here? I figured the proxy class generation would honor the if logic in the DataMember property?

I'm just using the b开发者_如何学Cuilt in VS 2010 WCF tools to generate the proxy classes, etc. and haven't gotten into custom serialization.

Any insight would be appreciated!


Generated code doesn't copy your implementation, just your structure which is why you're having to initialise your property on the client side.

If you want to have the same implementation on client and server you need to look at shared contracts.

It's basically where you define your contracts in a seperate assembly and then use the same one on the client and server side.

http://msdn.microsoft.com/en-us/library/aa480190.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜