问题
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; }
}
}
回答1:
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.
来源:https://stackoverflow.com/questions/30598041/how-i-can-get-rid-of-specified-fields-while-creating-proxy-for-a-wcf-service