views:

217

answers:

3

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'm running the disco.exe tool to generate .wsdl and .disco files from this web service for use in SharePoint. The following WSDL for the parameters is being generated:

<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 the int parameter have minOccurs set to 1 instead of 0 and how do I change it?

I've tried the following without success:

  • [XmlElementAttribute(IsNullable=false)] in the parameter declaration: makes no difference (as expected when you think about it)

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

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

  • [XmlIgnore] in the parameter declaration: the parameter is never output to the WSDL at all

A: 

I am guessing it is because the int being a value type it cannot be null and therefore must be present, where as the strings do not. I am guessing there is probably nothing you can do about it if you cannot change the signature. If you can change the signature can you specify it as a nullable int (i.e. int? noOfItems)?

tyranid
@tyranid: I can change the signature but no luck unfortunately (please see revised q)
Alex Angas
+1  A: 

It's because an int is not nullable that it must occur at least once, so setting IsNullable=false probably isn't going to change anything. That said, I'm pretty sure IsNullable=true doesn't help either, nor does making the object nullable.

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

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

public int noOfItems { get; set; }

That is, of course, if you wrap your arguments up in a single object that you can add this code to.

pdr
@pdr: This makes the parameter not present in the resultant WSDL at all (please see revised q)
Alex Angas
See clarification - this is an extra property in addition to your property, not the same one with an attribute added
pdr
+1  A: 

You can make the WSDL look the way you want with the following:

[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 party it must be a integer but your internal type can be string, therefore making the field not required.

Harry L