Setting minOccurs = "0" (not required) for int type web service parameters

I have an ASP.NET 2.0 web method with the following signature:

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

I am running the disco.exe tool to create .wsdl and .disco files from this web service for use in SharePoint. The following WSDL is created for the parameters:

 <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" /> 

Why does int parameter have minOccurs for 1 instead of 0 and how to change it ?

I tried the following without success:

  • [XmlElementAttribute(IsNullable=false)] in parameter declaration: doesn't matter (as expected when you think about it)

  • [XmlElementAttribute(IsNullable=true)] in the parameter declaration: gives the error "IsNullable could not be" true "for the value type System.Int32. Please consider using Nullable."

  • changing the parameter type to int ?: saves minOccurs="1" and adds nillable="true"

  • [XmlIgnore] in parameter declaration: parameter is never displayed on WSDL at all

+4
source share
3 answers

You can make WSDL the way you want as follows:

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

The idea is that you can tell the other side that it must be an integer , but your internal type may be a string , so this field is not required.

+4
source

This is because int not NULL, that it should happen at least once, so setting IsNullable=false will probably not change anything. However, I am sure that IsNullable=true does not help either, and also makes the object null.

From memory, I think you can do something like this, though

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

This, of course, if you wrap your arguments in one object to which you can add this code.

+5
source

I assume that this is because int is a value type, it cannot be null and therefore must be present where, as a string there is no. I guess you probably can't do anything about it if you can't change the signature. If you can change the signature, can you specify it as a nullable int (i.e. int? noOfItems )?

0
source

Source: https://habr.com/ru/post/1306328/


All Articles