本文介绍了为什么需要为int和Data而不是为String设置Web Service DataMember的指定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过WCF创建了一个Web服务。然后,我将其作为Web服务公开,以使其可用于.NET 2.0应用程序。我用DataMember创建了一些DataContract,供暴露的OperationContract使用。

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.

我注意到,当我尝试创建要在Web服务中传递的DataClass时,每个DataContract属性现在为每个成员都有一个伙伴指定属性。

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.

例如:

[DataContract]
public class Sales
{

  [DataMember]
  public int InvoiceNo;

...
}

创建实例时Web服务客户端中的销售数量。我得到名为InvoiceNo和InvoiceNoSpecified的属性。

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

现在这是我的问题,当属性为字符串类型时,不需要将相应的 Specified属性设置为true,但是当属性类型为int或DateTime时,如果我未将相应的 Specified属性设置为true,则该值在Web服务主机中将为null。有没有一种方法可以避免设置Specified属性?原因我需要在代码中的很多地方调用Web服务功能。

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.

推荐答案

DataMember属性的默认参数是:

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;

这篇关于为什么需要为int和Data而不是为String设置Web Service DataMember的指定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 22:14