Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have an optional parameter for an ASP.NET SOAP web service

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);
    }
    

as 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.

like image 603
Hauke Avatar asked Nov 12 '09 15:11

Hauke


People also ask

Can parameters be optional?

The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Each optional parameter has a default value as part of its definition.

Can we make Web API method parameters as optional?

Optional Parameters in Web API Attribute Routing and Default Values: You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

Can we make out parameter optional in C#?

No. To make it "optional", in the sense that you don't need to assign a value in the method, you can use ref . A ref parameter is a very different use case.

Are optional parameters bad practice?

The thing with optional parameters is, they are BAD because they are unintuitive - meaning they do NOT behave the way you would expect it. Here's why: They break ABI compatibility ! so you can change the default-arguments at one place.


2 Answers

If you REALLY have to accomplish this, here's a sort of hack for the specific case of a web method that has only primitive types as parameters:

[WebMethod]
public void MyMethod(double requiredParam1, int requiredParam2)
{
    // Grab an optional param from the request.
    string optionalParam1 = this.Context.Request["optionalParam1"];

    // Grab another optional param from the request, this time a double.
    double optionalParam2;
    double.TryParse(this.Context.Request["optionalParam2"], out optionalParam2);
    ...
}
like image 148
Diego Avatar answered Oct 24 '22 15:10

Diego


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

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

[WebMethod(MessageName="Add2")]
public int Add(double dValueOne, double dValueTwo)
{
   return dValueOne + dValueTwo;
}

The methods will be made visible as Add2 and Add3to the outside.

like image 42
Rasik Jain Avatar answered Oct 24 '22 17:10

Rasik Jain