我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗 [英] Can I have an optional parameter for an ASP.NET SOAP web service

查看:25
本文介绍了我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这个签名构建一个网络服务,如果 param2 为空,它不会抛出异常.这可能吗?

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

异常是 System.ArgumentException,在尝试将空字符串转换为布尔值时抛出.

The exception is a System.ArgumentException that is thrown when trying to convert the empty string to boolean.

目前尚未奏效的想法:

  • webservices 不允许方法重载,比如

  • method overloading is not allowed for webservices, like

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

按照此处的建议:

  • 使 bool 可以为空 bool?.同样的例外.
  • 操作 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.

推荐答案

您可以在具有 MessageName 属性的 Web 服务中使用重载方法.这是实现重载功能的一种变通方法.

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

这些方法将作为 Add2Add3 对外可见.

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

这篇关于我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆