开发者

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.

Id开发者_如何学JAVAeas 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.


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


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.


I know this post is little old. But I think the method names should be same for the example from Rasik. If both method names are same, then where overloading comes there. I think it should be like this:

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


In accordance with MinOccurs Attribute Binding Support and Default Attribute Binding Support:

  1. Value type accompanied by a public bool field that uses the Specified naming convention described previously under Translating XSD to source - minOccurs value of output <element> element 0.

    [WebMethod]
    public SomeResult SomeMethod(bool optionalParam, [XmlIgnore] bool optionalParamSpecified)
    result:
    <s:element minOccurs="0" maxOccurs="1" name="optionalParam" type="s:boolean" />

  2. Value type with a default value specified via a System.Component.DefaultValueAttribute - minOccurs value of output <element> element 0. In the <element> element, the default value is also specified via the default XML attribute.

    [WebMethod]
    public SomeResult SomeMethod([DefaultValue(true)] bool optionalParam)
    result:
    <s:element minOccurs="0" maxOccurs="1" default="true" name="optionalParam" type="s:boolean" />


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


Making your parameter bool? and having your caller send an explicit null also works as a workaround.


Here is the best solution that has proven to work for me.

  • Don't bother creating function overloads.
  • Don't use different name for functions.
  • Don't use optional parameters.
  • Don't use this solution either Web Service Method with Optional Parameters

Simply have a data class with all your desired parameters as properties and simply have that class as the only parameter. This way every property of the class will be serialized optionally and will the default value if it was not passed in.

Here is what I am talking about

[Serializable]
public class DataObj
{
    public int a { get; set; }
    public int b { get; set; }
    public int c { get; set; }
}

[WebMethod(MessageName = "MyMethod")]
public void MyMethod(DataObj dataObj)
{}


Make all optional arguments strings. If no argument is passed, the input is treated as null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜