我怎样才能摆脱指定的字段在创建代理的WCF服务? [英] How I can get rid of specified fields while creating proxy for a WCF service?

查看:170
本文介绍了我怎样才能摆脱指定的字段在创建代理的WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然从WCF服务创建代理,这是我里面的服务代理创建多一个布尔类型指定字段声明的每个值类型的成员。有没有什么办法可以摆脱这一点,并继续进行交易与服务?

我有以下服务描述类

  [DataContract]
公共类客户
{
    私人诠释的customerID;

    [数据成员]
    公众诠释客户ID
    {
        {返回的customerID; }
        集合{=的customerID值; }
    }
}
 

我有这样的代理类,同时创建代理

 公共部分类客户
{
     私人诠释customerIDField;

     私人布尔customerIDFieldSpecified;
     公众诠释客户ID
     {
         {返回this.customerIDField; }
         集合{this.customerIDField =价值; }
     }
}
 

我怎么能在代理摆脱cus​​tomerIDFieldSpecified布尔类型的成员?仅通过设置customerIDField以及如何我可以继续服务。​​

我希望我的代理类是这样

 公共部分类客户
{
     私人诠释customerIDField;
     公众诠释客户ID
     {
         {返回this.customerIDField; }
         集合{this.customerIDField =价值; }
     }
}
 

解决方案
  

我怎样才能得到摆脱cus​​tomerIDFieldSpecified布尔类型的成员   代理?

它曾经是常见的类型BOOL,INT,小数,或暴露在服务边界的任何XSD兼容值类型的类成员ASMX Web服务有一个相当于 ... FieldSpecified 属性除了定义为实际的字段包含值。

这之所以被放入生成的代理code是相当简单:当XmlSerializer的序列化类型的XML,因为在.net中这些类型(如果它们没有被指定)返回的默认,所得到的消息负载将包含一个布尔值或十进制/ INT /等字段的值假值或分别为0。

现在,如果省略一个值在一个类型,然后该类型被序列化,并设置为它的默认值,则这是不希望的,因为默认值是实际值,这是误导性的,会造成缺陷。因此,要解决这个问题的 ... FieldSpecified 属性进行了介绍。

当时的想法是,如果你想被列入外地,你也不得不相当于FieldSpecified属性设置为true,这将指示XmlSerializer的服务端反序列化(并因此分配)的值到服务器重新类型的presentation。如果没有指定,则XmlSerializer的会跳过等价的属性,只是移动到下一个字段中的XML。

使用WCF,微软推出DataContractSerializer的,作为替代XmlSerializer的。 DataContractSerializer的不表现出对反序列化相同的行为,并不会尝试在XML的任何值分配给字段不是present,所以这个额外的领域不再需要。然而,在某些情况下,WCF倒在XmlSerializer的,当它产生的服务元数据,这是我的猜测,您如何结束了与他们的客户端code。

While creating proxy from a WCF service,the each value type members which I declared inside service creating one more bool type specified field in proxy. Is there any way i can get rid of this and continue to transact with service?

i have below class described in service as

[DataContract]
public class Customer
{
    private int customerID;

    [DataMember]
    public int CustomerID
    {
        get { return customerID; }
        set { customerID = value; }
    }
}

while creating proxy i am having proxy class like this

public partial class Customer
{
     private int customerIDField;

     private bool customerIDFieldSpecified;
     public int CustomerID 
     {
         get { return this.customerIDField; }
         set { this.customerIDField = value; }
     }
}

How i can get rid of customerIDFieldSpecified bool type member in proxy? and also how can i continue with service by only setting customerIDField.

I wanted my proxy class to be like this

public partial class Customer
{
     private int customerIDField;
     public int CustomerID 
     {
         get{ return this.customerIDField; }
         set{ this.customerIDField = value; }
     }
}

解决方案

How i can get rid of customerIDFieldSpecified bool type member in proxy?

It used to be common with ASMX web services for class members of type bool, int, decimal, or any XSD compatible value type exposed over the service boundary to have an equivalent ...FieldSpecified property defined in addition to the actual field to contain the value.

The reason this was put into generated proxy code is quite simple: that when the XmlSerializer serializes types to XML, and because in .net these types (if they are not specified) return default values, the resulting message payload would contain a bool or decimal/int/etc field with a value false or 0 respectively.

Now, if you omit a value in a type and then this type gets serialized and set to it's default value, then this is undesirable, because default values are actual values, which is misleading and will cause defects. So to get around this the ...FieldSpecified property was introduced.

The idea was that if you wanted the field to be included, you also had to set the equivalent FieldSpecified property to true, and that would instruct the XmlSerializer on the service side to deserialize (and therefore assign) the values into the server's representation of the type. If this was not specified then the XmlSerializer would skip the equivalent property and just move onto the next field in the XML.

With WCF, Microsoft introduced the DataContractSerializer, as a replacement for the XmlSerializer. The DataContractSerializer does not exhibit the same behavior on deserialization and will not try to assign any value to fields not present in the XML, so this extra field is no longer needed. However, under certain conditions, WCF falls back on the XmlSerializer when it's generating client code from service metadata, which is my guess as to how you ended up with them.

这篇关于我怎样才能摆脱指定的字段在创建代理的WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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