改变客户端访问时的方法名
- 客户端无法重载方法(可以通过判断arguments数量来模拟)
- 如果服务器端出现了方法重载?
使用WebServiceAttribute指定客户端方法名
使用和真正的WebService相同的做法
[WebMethod(MessageName="…")]
- 并非出现重载才能改变方法名称
一个改变客户端访问时的方法名的示例
首先创建一个名为MethodOverloadService.asmx的WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
///MethodOverloadService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class MethodOverloadService : System.Web.Services.WebService
{
[WebMethod]
public int GetRandom()
{
return new Random(DateTime.Now.Millisecond).Next();
}
[WebMethod]
public int GetRandom(int minValue, int maxValue)
{
return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
}
}
然后我们创建一个页面,引用这个WebService,并设置ScriptManager的InlineScript="true",这样生成的代理就直接写到页面上了,我们可以看到,页面中只注册下面的一个GetRandom方法,因为第一个方法已经被覆盖
如果我们要避免这种客户端对同名方法的覆盖,我们就要改变客户端访问这个方法时的名字,只需要在任意一个这样的方法下面加上如下代码就可以实现了
[WebMethod(MessageName = "GetRangeRandom")]
这时我们就可以在页面中找到它注册了两个方法 ,方法名分别是GetRandom和GetRangeRandom,好了,成功啦
使用HTTP GET访问WebService方法
- 使用ScriptMethodAttribute进行标记(UseHttpGet属性设置为true),出于安全性考虑,默认只使用POST
- 客户端使用代理的方法没有任何变化
- 参数将使用Query String进行传递
- 性能较HTTP POST方法略有提高
- 一些特性略有改变(缓存的基础等,HTTP GET是没有缓存的)
一个使用HTTP GET访问WebService方法的示例
首先创建一个名为UserHttpGetService的WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
///UserHttpGetService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class UseHttpGetService : System.Web.Services.WebService
{
[WebMethod]
public int GetRandom()
{
return new Random(DateTime.Now.Millisecond).Next();
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public int GetRangeRandom(int minValue, int maxValue)
{
return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
}
}
然后创建页面,代码如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Demo03/UseHttpGetService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<input type="button" value="Get Random" onclick="getRandom()" />
<input type="button" value="Get Range Random" onclick="getRandom(50,100)" />
<script language="javascript" type="text/javascript">
function getRandom(minValue, maxValue) {
if (arguments.length != 2) {
UseHttpGetService.GetRandom(onSucceeded);
}
else {
UseHttpGetService.GetRangeRandom(minValue, maxValue, onSucceeded);
}
}
function onSucceeded(result) {
alert(result);
}
</script>
</form>
</body>
</html>
这样我们就完成了这个示例,我们会发现页面代码和以前访问WebService是一摸一样的,但是我们使用HttpWatch查看就可以发现,他们的访问方式是不一样的,而且我们打开页面源代码也可以发现以下代码
this._invoke(this._get_path(), 'GetRandom',false,{},succeededCallback,failedCallback,userContext);
this._invoke(this._get_path(), 'GetRangeRandom',true,{minValue:minValue,maxValue:maxValue},succeededCallback,failedCallback,userContext); }}
这里的false和true就表示是不是使用HTTP GET