Olaf
Olaf

Reputation: 899

Calling a WebMethod from a web reference with optional parameters in C#

I have created a dummy web service with 2 optional parameters using the .Net Webservices, however in the real product we are going to have a lot more optional parameters(think: filters for a query). The problem is that it is not possible to leave out the optional parameters when calling the webservice, meaning there will be a potential dozens of NULL values in every call to the webservice when developing against the real webservice.

Right now this dummy webservice consists of only this, to support 2 optional string parameters:

[WebMethod]
public string HelloWorld(String PARAM_1="", String PARAM_2="")
{   return "";   }

In the WSDL it indeed displays these parameters with minvalue 0 and maxvalue 1:

<s:element minOccurs="0" maxOccurs="1" name="PARAM_1" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="PARAM_2" type="s:string"/>

The problem lies here: When adding this webservice through a web reference in Visual Studio 2010 it will indeed create the service object with the HelloWorld Method. However it does not take into account the optional parameters. It creates a function HelloWorld with no optional parameters. It always requires both PARAM_1 and PARAM_2, so the optional parameters have to be filled with NULLs regardless of which ones we actually would want to use.

The definition VS2010 generates out of the web reference is this, showing the issue:

public string HelloWorld(string PARAM_1, string PARAM_2) {
object[] results = this.Invoke("HelloWorld", new object[] { PARAM_1, PARAM_2});

Is there a way to use webservices with optional parameters in VS2010 without being forced to NULL a potential dozens of optional parameters with every call? We would like to keep using the convenient webreferences in Visual Studio without being forced to do this all the time.

Upvotes: 3

Views: 3612

Answers (4)

Andreas
Andreas

Reputation: 6465

You can create optional parameters if you wrap all parameters in a separate class. This represents a new message where you can decide on your own to make certain properties optional. You can also provide different constructors in a partial class declaration to the service generated classes.

This is IMO a better solution than method overloading, which doesn't even work with web services. You have to create a new and slightly different method for each overload.

Upvotes: 1

Rajesh
Rajesh

Reputation: 7876

The minOccurs = 0 refers to the occurance of the variable. Since its of type string minOccurs = 0 since it would have a default value.

Can you try to use optional parameter with a different type and check the wsdl.

Upvotes: 1

Andrew Florko
Andrew Florko

Reputation: 7750

I guess refactor method to accept structure with dozen of nullable properties (instead of dozen parameters) will make sence.

Upvotes: 0

jro
jro

Reputation: 7480

Optional parameters are a language-specific construct. SOAP-based web services know nothing about optional parameters, because SOAP-based web services are language agnostic.

If you want to have an optional parameter for the service call, you'll need to override the generated stub method with your own code if you want the client-side feel of optional parameters.

Upvotes: 3

Related Questions