WCF服务代理不设置" FieldSpecified"属性 [英] WCF service proxy not setting "FieldSpecified" property

查看:390
本文介绍了WCF服务代理不设置" FieldSpecified"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF DataContract 看起来像下面这样:

I've got a WCF DataContract that looks like the following:

namespace MyCompanyName.Services.Wcf
{
  [DataContract(Namespace = "http://mycompanyname/services/wcf")]
  [Serializable]
  public class DataContractBase
  {
    [DataMember]
    public DateTime EditDate { get; set; }

    // code omitted for brevity...
  }
}

当我引用添加到该服务在Visual Studio中,这个代理code产生:

When I add a reference to this service in Visual Studio, this proxy code is generated:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mycompanyname/services/wcf")]
public partial class DataContractBase : object, System.ComponentModel.INotifyPropertyChanged {

    private System.DateTime editDateField;

    private bool editDateFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public System.DateTime EditDate {
        get {
            return this.editDateField;
        }
        set {
            this.editDateField = value;
            this.RaisePropertyChanged("EditDate");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool EditDateSpecified {
        get {
            return this.editDateFieldSpecified;
        }
        set {
            this.editDateFieldSpecified = value;
            this.RaisePropertyChanged("EditDateSpecified");
        }
    }

    // code omitted for brevity...
}

正如你所看到的,除了产生后盾属性 EditDate ,一个额外的&LT; propertyName的&GT;指定属性被产生。一切都很好,只是当我做到以下几点:

As you can see, besides generating a backing property for EditDate, an additional <propertyname>Specified property is generated. All good, except that when I do the following:

DataContractBase myDataContract = new DataContractBase();
myDataContract.EditDate = DateTime.Now;

new MyServiceClient.Update(new UpdateRequest(myDataContract));

EditDate 没有得到拾起服务(没有出现在传输XML)。

the EditDate was not getting picked up by the endpoint of the service (does not appear in the transmitted XML).

我调试的code和发现,虽然我设置 EditDate EditDateSpecified 属性WASN 'T被设置为因为我希望;因此,XML序列被忽略的价值 EditDate ,即使它设置为一个有效的值。

I debugged the code and found that, although I was setting EditDate, the EditDateSpecified property wasn't being set to true as I would expect; hence, the XML serializer was ignoring the value of EditDate, even though it's set to a valid value.

作为快速劈我修改了 EditDate 属性如下所示:

As a quick hack I modified the EditDate property to look like the following:

   /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public System.DateTime EditDate {
        get {
            return this.editDateField;
        }
        set {
            this.editDateField = value;

            // hackhackhack
            if (value != default(System.DateTime))
            {
              this.EditDateSpecified = true;
            }
            // end hackhackhack

            this.RaisePropertyChanged("EditDate");
        }
    }

现在的code正常工作,但当然我每次时重新生成代理,我的修改都将丢失。我可以改变调用code以下内容:

Now the code works as expected, but of course every time I re-generate the proxy, my modifications are lost. I could change the calling code to the following:

DataContractBase myDataContract = new DataContractBase();
myDataContract.EditDate = DateTime.Now;
myDataContract.EditDateSpecified = true;

new MyServiceClient.Update(new UpdateRequest(myDataContract));

但也似乎是一个时间劈十岁上下的浪费。

but that also seems like a hack-ish waste of time.

所以最后,我的问题:有没有人对如何让过去的Visual Studio的服务代理生成的这种直观(和国际海事组织碎)的行为,还是我只是失去了一些东西的建议

So finally, my question: does anyone have a suggestion on how to get past this unintuitive (and IMO broken) behavior of the Visual Studio service proxy generator, or am I simply missing something?

推荐答案

这可能会有点不直观(与了我个措手不及和缫丝,太!) - 但它是唯一正确的方式来处理元素,可能会或可能不会在你的XML架构来指定。

It might be a bit unintuitive (and caught me off guard and reeling, too!) - but it's the only proper way to handle elements that might or might not be specified in your XML schema.

和它也似乎违反直觉,你必须设置 xyzSpecified 标记自己 - 但最终,这给你更多的控制权,和WCF是所有关于< A HREF =htt​​p://www.patnys.com/archive/2008/07/10/understanding-tenets-of-soa.aspx>四个信条是非常明确和清晰的了解你的意图SOA 的

And it also might seem counter-intuitive that you have to set the xyzSpecified flag yourself - but ultimately, this gives you more control, and WCF is all about the Four Tenets of SOA of being very explicit and clear about your intentions.

所以基本上 - 这是它,习惯它:-)有没有办法,过去这种行为方式 - 它是WCF系统设计的方式,有很好的理由,太

So basically - that's the way it is, get used to it :-) There's no way "past" this behavior - it's the way the WCF system was designed, and for good reason, too.

什么,你总是可以做的是捕获并处理了 this.RaisePropertyChanged(EditDate); 事件,并设置 EditDateSpecified 标记中的事件处理程序的事件。

What you always can do is catch and handle the this.RaisePropertyChanged("EditDate"); event and set the EditDateSpecified flag in an event handler for that event.

这篇关于WCF服务代理不设置&QUOT; FieldSpecified&QUOT;属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆