今天在写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;
        }
    }

这样也可以实现重载。

打开服务后,可看到

支持下列操作。有关正式定义,请查看服务说明

最新文章

  1. 2016/12/28_javascript
  2. Linux 命令
  3. 一个Eclipse代码显示主题
  4. Linux 命令 - file: 确定文件类型
  5. uva 10892
  6. StarUML启动时候出现&quot;System Error. Code:1722. RPC服务器不可用.&quot;错误的解决办法
  7. Tamper Data 安装与使用
  8. nginx与apache配合反向代理技术1
  9. [Tjoi2013]循环格
  10. LeetCode之“链表”:Reverse Nodes in k-Group
  11. Java JFrame图形界面 ----一个简单的窗口
  12. .net mvc + layui做图片上传(二)—— 使用流上传和下载图片
  13. R-shiny服务器安装及配置
  14. git报错failed to push some refs to &#39;git@github.com:Markprint/github.git&#39;
  15. spark 学习_rdd常用操作
  16. Android上的单元测试
  17. python读取文件,python读取的1变成\ufeff1
  18. day4之内置函数、匿名函数,递归函数
  19. Oracle VM VirtualBox技巧
  20. Java基础-StringBuffer类与StringBuilder类简介

热门文章

  1. 演示:纯CSS实现自适应布局表格
  2. DD-WRT相关资源
  3. HDOJ/HDU 2537 8球胜负(水题.简单的判断)
  4. tyvj P1952 Easy(递推+期望)
  5. 在PHP网页中,如何把$_session[&quot;yyy&quot;]赋值到一个文本框中?
  6. 385. Mini Parser
  7. Oracle RAC中的投票算法
  8. kafka offset-check工具失效的问题
  9. eclipse运行项目特别慢,出现Java heap space溢出
  10. Matrix的set,pre,post调用顺序