•每个Service类对应客户端一个代理类
•每个代理类为Sys.Net.WebServiceProxy类的子类
•代理类的每个方法均对应一个类的静态方法
•最终由Sys.Net.WebServiceProxy.invoke方法发出请求
aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug" />
<input type="button" value="Get Range Random" onclick="getRandom(50, 100)" />
<script language="javascript" type="text/javascript">
function getRandom(minValue, maxValue)
{
Sys.Net.WebServiceProxy.invoke(
"Services/UseHttpGetService.asmx",
"GetRangeRandom",
true,
{ "minValue" : minValue,
"maxValue" : maxValue},
onSucceeded,
null,
null,
-1);
}
function onSucceeded(result)
{
alert(result);
}
</script>
</form>
没有将ScriptManager的InlineScript="true",不是不使用客户端代理.直接使用Sys.Net.WebServiceProxy.invoke方法调用WebService。<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug" />
<input type="button" value="Get Range Random" onclick="getRandom(50, 100)" />
<script language="javascript" type="text/javascript">
function getRandom(minValue, maxValue)
{
Sys.Net.WebServiceProxy.invoke(
"Services/UseHttpGetService.asmx",
"GetRangeRandom",
true,
{ "minValue" : minValue,
"maxValue" : maxValue},
onSucceeded,
null,
null,
-1);
}
function onSucceeded(result)
{
alert(result);
}
</script>
</form>
UseHttpGetService.asmx
<%@ WebService Language="C#" Class="UseHttpGetService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[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);
}
}
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[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);
}
}