在int类型的Web服务参数上设置minOccurs =“0”(不需要)

时间:2010-04-08 04:35:50

标签: asp.net web-services

我有一个带有以下签名的ASP.NET 2.0 Web方法:

[WebMethod]
public QueryResult[] GetListData(
    string url, string list, string query, int noOfItems, string titleField)

我正在运行disco.exe工具,以便从此Web服务生成.wsdl和.disco文件,以便在SharePoint中使用。正在生成以下参数的WSDL:

<s:element minOccurs="0" maxOccurs="1" name="url" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="list" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="query" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="noOfItems" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="titleField" type="s:string" />

为什么int参数将 minOccurs设置为1 而不是0而如何更改

我尝试了以下但没有成功:

    参数声明中的
  • [XmlElementAttribute(IsNullable=false)]:没有区别(考虑到它时的预期)

  • 参数声明中的
  • [XmlElementAttribute(IsNullable=true)]:对于值类型System.Int32,给出错误“IsNullable可能不是'true'。请考虑使用Nullable。”

  • 将参数类型更改为int? :保留minOccurs="1"并添加nillable="true"

  • 参数声明中的
  • [XmlIgnore]:参数永远不会输出到WSDL

3 个答案:

答案 0 :(得分:5)

这是因为int不可为空,它必须至少出现一次,所以设置IsNullable=false可能不会改变任何东西。也就是说,我很确定IsNullable=true也没有帮助,也没有使对象可以为空。

从记忆中,我认为你可以做这样的事情

[XmlIgnore]
public bool noOfItemsSpecified { get; set; }

public int noOfItems { get; set; }

当然,也就是说,如果将参数包装在一个可以添加此代码的对象中。

答案 1 :(得分:4)

您可以使用以下内容使WSDL看起来如您所愿:

[WebMethod]
public QueryResult[] GetListData(
    string url, 
    string list, 
    string query, 
    [XmlElement(DataType = "integer")] string noOfItems, 
    string titleField)

您的想法是,您可以告诉对方它必须是integer,但您的内部类型可以是string,因此不需要该字段。

答案 2 :(得分:0)

我猜这是因为int是一个值类型,它不能是null因此必须存在,而字符串则不存在。我猜你可能无法改变签名,你可能无能为力。如果您可以更改签名,可以将其指定为可以为空的int(即int? noOfItems)?

相关问题