views:

273

answers:

2

I want to build a webservice with this signature, which does not throw an exception if param2 is left empty. Is this possible?

[WebMethod]
public string HelloWorld(string param1, bool param2){}

The exception is a System.ArgumentException that is thrown when trying to convert the empty string to boolean.

Ideas that have not worked so far:

  • method overloading is not allowed for webservices, like

    public string HelloWorld(string param1) { return HelloWorld(param1, false); }

suggested here

  • make bool nullable bool?. Same exception.
  • manipulate the WSDL, see this answer

My question is related to this question, but the only answer points to WCF contracts, which I have not used yet.

+1  A: 

Hauke,

You can have a Overloaded Method in webservices with MessageName attribute. This is a workaround to achieve the overloading functionality.

Look at http://msdn.microsoft.com/en-us/library/byxd99hx%28VS.71%29.aspx

 [System.Web.Services.WebMethod(MessageName="Add3")]
    public double Add3(double dValueOne, double dValueTwo, double dValueThree)
    {
       return dValueOne + dValueTwo + dValueThree;
    }

    [System.Web.Services.WebMethod(MessageName="Add2")]
    public int Add2(double dValueOne, double dValueTwo)
    {
       return dValueOne + dValueTwo;
    }
Rasik Jain
Will both methods look like the same one to the outside world?
Hauke
Hauke, No, It will two different methods to outside world. This is a workaround for Overloading.
Rasik Jain
+1  A: 

You can not have overloaded Web Service methods. The SOAP protocol does not support it. Rasik's code is the workaround.

Chuck Conway