我可以有一个ASP.NET SOAP网络服务的可选参数可选、网络服务、有一个、参数

2023-09-02 10:32:53 作者:谁念西风独自凉

我要建立与此签名,如果参数2为空不抛出异常的Web服务。这可能吗?

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

唯一的例外是试图转换为Boolean空字符串时引发一个System.ArgumentException。

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:

方法重载是不允许的web服务,比如

method overloading is not allowed for webservices, like

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

作为建议here:

布尔可空布尔?。同样的异常。 操纵WSDL,请参见这个答案 make bool nullable bool?. Same exception. manipulate the WSDL, see this answer

我的问题是有关这个问题,但唯一的答案要点WCF的合同,我还没用过。

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

推荐答案

您可以有一个重载的方法的web服务与MessageName属性。这是一种解决方法,实现了重载功能。

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

看 http://msdn.microsoft。 COM / EN-US /库/ 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;
}

该方法将变为可见作为 ADD2 ADD3 到外面。