Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

wcf - Why does a Web Service DataMember's Specified Attribute need to be set for int and Data but not for String

I have created a web service via WCF. Then I exposed it as a web service to make it usable with a .NET 2.0 application. I created some DataContract with DataMember that could be used for by the exposed OperationContract.

I notice that when I try to create DataClass to be passed in the web service that each DataContract attribute now has a partner "Specified" attribute for each member.

For example:

[DataContract]
public class Sales
{

  [DataMember]
  public int InvoiceNo;

...
}

When I create an instance of Sales in the web service client. I get attribute named InvoiceNo and InvoiceNoSpecified.

Now here is my question, when the attribute is of type string, I do not need to set the corresponding "Specified" attribute to true, but when the attribute type is a int or DateTime, if I do not set the corresponding "Specified" attribute to true, the value becomes null in the web service host. Is there a way to avoid setting the Specified attribute? Cause I need to call the web service functions in a lot of places in my code. It would really be difficult to keep track of them all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default parameters for the DataMember attribute are:

bool EmitDefaultValue (default true)
bool IsRequired (default false)

If the property you are exposing is a non-nullable value type you should use:

[DataMember(IsRequired = true)]
public int InvoiceNo;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...