问题
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.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
I guess refactor method to accept structure with dozen of nullable properties (instead of dozen parameters) will make sence.
来源:https://stackoverflow.com/questions/7930593/calling-a-webmethod-from-a-web-reference-with-optional-parameters-in-c-sharp