关于webservice不支持方法重载的解决办法
今天在写WebService时,出现了这样的错误:
Count(Int32, Int32) 和 Count(Int32) 同时使用消息名称“Count”。使用 WebMethod 自定义特性的 MessageName 属性为方法指定唯一的消息名称。
原来,必须在方法中指定messagename来用户唯一标识且在类中指示不支持1.1标准,
由于用到方法重载,没想到在web服务中会出现错误。
原来WebService中是不支持方法的重载的。
为什么WebService中不支持方法的重载?
WebService中不支持方法的重载,这还得从WebService的工作机制中说起,当客户端调用一个WebService的方法时,首先要将方法名称和需要传递的参数包装成XML,也就是SOAP包,通过HTTP协议传递到服务器端,然后服务器端解析这段XML,得到被调用的方法名称和传递过来的参数,进而调用WebService相应的方法,方法执行完毕后,将返回结果再次包装为XML,也就是SOAP响应,发送到客户端,最后客户端解析这段XML,最终得到返回结果,关键在于服务器端解析XML时无法识别重载的方法,WebService只认方法的名称,而且两个方法的名称相同,服务器端不知道该调用哪个相应的方法。
具体解说,可参看http://www.cnblogs.com/menglin2010/archive/2012/03/29/2421445.html
解决方法如下:
[WebService(Namespace = "http://www.efreer.cn/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
public class email : System.Web.Services.WebService
{
[WebMethod(EnableSession=true)]
public int Count(int i)
{
i = i + 1;
return i;
}
[WebMethod(EnableSession = true,MessageName="Count1",Description="数量")]
public int Count(int i,int j)
{
return i + j;
}
}
后来在博客园中http://www.cnblogs.com/VisualStudio/archive/2008/10/11/1308541.html又看到了另一个解决方法,此方法是在遵守BP1.1的规范下,实现重载的。
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/", Description = "测试", Name = "Service1")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1, Name = "OverLoadCount", EmitConformanceClaims=true)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
[SoapDocumentMethod(OneWay = true)]
public void OneWay(int i)
{
i = i + 1;
}
public static int i;
[WebMethod(EnableSession = true, TransactionOption = TransactionOption.RequiresNew)]
public int Count()
{
i = i + 1;
return i;
}
[WebMethod(EnableSession = true,MessageName="Count1",Description="数量")]
[SoapDocumentMethod( Binding="OverLoadCount")]
public int Count(int i,int j)
{
return i + j;
}
}
这样也可以实现重载。
打开服务后,可看到
支持下列操作。有关正式定义,请查看服务说明。
- Count
- Count
MessageName="Count1"
数量 - HelloWorld
- OneWay
关于webservice不支持方法重载的解决办法的更多相关文章
- 卧槽,好强大的魔法,竟能让Python支持方法重载
1. 你真的了解方法重载吗? 方法重载是面向对象中一个非常重要的概念,在类中包含了成员方法和构造方法.如果类中存在多个同名,且参数(个数和类型)不同的成员方法或构造方法,那么这些成员方法或构造方法就被 ...
- win10 家庭版不支持gpedit.msc的解决办法
win10 家庭版不支持gpedit.msc的解决办法 1.建立一个批处理文件内容如下: @echo off pushd "%~dp0" dir /b %systemroot%\W ...
- IE6不支持li:hover的解决办法,一句代码让IE6支持li:hover
如果不是因为工作需要,我根本不会理会IE6的兼容问题,甚至我都不想理会IE的所有内核,不过IE9用了下,我还是重新对IE报以期待的.话题扯远了,下面回到话题上来吧.这次要说的内容就是,如果让IE支持l ...
- WCF不支持 ASP.NET 兼容性 解决办法
错 误提示:无法激活服务,因为它不支持 ASP.NET 兼容性.已为此应用程序启用了 ASP.NET 兼容性.请在 web.config 中关闭 ASP.NET 兼容性模式或将 AspNetCompa ...
- jsp连接mysql出现不支持认证协议的解决办法
错误提示 com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Client does not support authent ...
- Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法
前言 我在实验进入linux系统启动xwindow server而不启动KDE GNOME等桌面系统时遇到的问题.只启动x server而不启动桌面系统,在xserver之上运行一个全屏的图形界面程序 ...
- MySQL不支持远程连接的解决办法
如果mysql不支持远程连接,会出现提示:错误代码是1130,ERROR 1130: Host * is not allowed to connect to this MySQL server ,解决 ...
- Spring cloud Feign不支持对象传参解决办法[完美解决]
spring cloud 使用 Feign 进行服务调用时,不支持对象参数. 通常解决方法是,要么把对象每一个参数平行展开,并使用 @RequestParam 标识出每一个参数,要么用 @Reques ...
- [转载]tomcat的配置文件server.xml不支持中文注释的解决办法
原文链接:http://tjmljw.iteye.com/blog/1500370 启动tomcat失败,控制台一闪而过,打开catalina的log发现错误指向了conf/server.xml,报错 ...
随机推荐
- PL/SQL 异常处理
SQL> set serveroutput on SQL> declare name varchar2(10); begin select ename into name from ...
- Hosting Multiple Service Implementations On The Same Port With WCF
Hosting Multiple Service Implementations On The Same Port With WCF Recently I have been playing arou ...
- 【转】压缩Virtualbox的vdi文件
原文网址:http://i.rexdf.org/blog/2014/10/06/ya-suo-virtualboxde-vdiwen-jian/ 问题实际上比较简单,我在Arch Linux杂记中给出 ...
- (转载)PHP isset()函数作用
(转载)http://www.cnblogs.com/neve/archive/2011/03/21/1990165.html isset函数是检测变量是否设置. 格式:bool isset ( mi ...
- TortoiseSVN优化设置
设置log messages的字体 TortoiseSVN默认的字体太小了,看着难受: 可以在Settings > 左侧目录树General > Dialogs 1中进行设置: 使用Bey ...
- Bzoj 2241: [SDOI2011]打地鼠 暴力,枚举,贪心
2241: [SDOI2011]打地鼠 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1022 Solved: 651[Submit][Status ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdoj 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- disconf实践(二)
因为有些系统的配置文件会随着业务更改,如某些控制开关,当大批量集群时,按照上一篇文章的配置就不够啦,需要做到热加载. 研究了一下,还好,比较简单,只要替换上一篇文章第4步的配置文件(spring-di ...