zoukankan      html  css  js  c++  java
  • 关于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;
            }
        }

    这样也可以实现重载。

    打开服务后,可看到

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

  • 相关阅读:
    用AVIFile函数制做AVI文件基本步骤
    RHEL5下源码安装Mysql
    RHEL 6.2/i686配置使用CentOS YUM源
    教你选择最稳定的 MySQL 版本
    RHEL 6.2/i686桌面版解决风扇狂转的问题 安装官方闭源ATI显卡驱动
    Ubuntu 11.10下解决JUK播放MP3乱码的方法
    Ubuntu 10.04下SVN+Apache安装、配置与使用
    Ubuntu 11.10安装(卸载)ATI闭源驱动导致黑屏进不了系统的解决办法
    ubuntu 11.10下创建eclipse桌面快捷方式
    Ubuntu 11.10与Windows双系统的硬盘安装方法
  • 原文地址:https://www.cnblogs.com/love828/p/3266258.html
Copyright © 2011-2022 走看看