学习ajax(四)(从客户端访问WebService)
服务器端释放WebService方法
名词解释
Ajax技术:异步刷新(javascript)
Ajax.Net:基于asp.net框架是一个ajax框架(不是微软体统的框架)
Asp.net Ajax: 微软提供的框架
编写一个普通的Asp.net webservice
-ScriptServiceAttribute
释放WebService方法
- 访问级别为public
-使用WeMethidAttribute
为页面中ScriptManager(Proxy)引入asmx文件
客户端访问WebService
[Namespaces.]ClassName.MethodName
依次传入参数
InlineScript=false 将会调用代理
1<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="1_WebServiceFoundation.aspx.cs"Inherits="_1_WebServiceFoundation"%>
2
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<htmlxmlns="http://www.w3.org/1999/xhtml">
6<headrunat="server">
7<title>UntitledPage</title>
8</head>
9<body>
10<formid="form1"runat="server">
11<asp:ScriptManagerID="ScriptManager1"runat="server"ScriptMode="Debug">
12<Services>
13<asp:ServiceReferencePath="WebServiceFoundation.asmx"InlineScript="true"/>
14</Services>
15</asp:ScriptManager>
16
17<inputtype="button"value="GetRandom"onclick="getRandom()"/>
18<inputtype="button"value="GetRangeRandom"onclick="getRandom(50,100)"/>
19
20<scriptlanguage="javascript"type="text/javascript">
21functiongetRandom(minValue,maxValue)
22{
23if(arguments.length!=2)
24{
25Sample.WebServiceFoundation.GetRandom(getRandomSucceeded);
26}
27else
28{
29Sample.WebServiceFoundation.GetRangeRandom(minValue,maxValue,getRandomSucceeded);
30}
31}
32
33functiongetRandomSucceeded(result)
34{
35alert(result);
36}
37</script>
38</form>
39</body>
40</html>
41
对应的的Webservice
1<%@WebServiceLanguage="C#"Class="Sample.WebServiceFoundation"%>
2
3usingSystem;
4usingSystem.Web;
5usingSystem.Web.Services;
6usingSystem.Web.Services.Protocols;
7usingSystem.Web.Script.Services;
8
9namespaceSample
10{
11[WebService(Namespace="http://tempuri.org/")]
12[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
13[ScriptService]
14publicclassWebServiceFoundation:System.Web.Services.WebService
15{
16[WebMethod]
17publicintGetRandom()
18{
19returnnewRandom(DateTime.Now.Millisecond).Next();
20}
21
22[WebMethod]
23publicintGetRangeRandom(intminValue,intmaxValue)
24{
25returnnewRandom(DateTime.Now.Millisecond).Next(minValue,maxValue);
26}
27}
28}
客户端访问PageMethod
服务器端
只能在aspx页面中定义
只能那个是公开静态方法
使用WebMethodAttribute标记
ScriptManager的EnablePageMethods属性设置为true
客户端
通过PageMethods.MethodName 访问
//得到国际标准时间
DateTime.UtcNow
对应代码2
1<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="2_PageMethods.aspx.cs"Inherits="_2_PageMethods"%>
2
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<htmlxmlns="http://www.w3.org/1999/xhtml">
6<headrunat="server">
7<title>UntitledPage</title>
8</head>
9<body>
10<formid="form1"runat="server">
11<asp:ScriptManagerID="ScriptManager1"runat="server"EnablePageMethods="true"/>
12
13<inputtype="button"value="GetCurrentTime"onclick="getCurrentTime()"/>
14
15<scriptlanguage="javascript"type="text/javascript">
16functiongetCurrentTime()
17{
18PageMethods.GetCurrentTime(getCurrentTimeSucceeded);
19}
20
21functiongetCurrentTimeSucceeded(result)
22{
23alert(result);
24}
25</script>
26</form>
27</body>
28</html>
29
1usingSystem;
2usingSystem.Data;
3usingSystem.Configuration;
4usingSystem.Collections;
5usingSystem.Web;
6usingSystem.Web.Security;
7usingSystem.Web.UI;
8usingSystem.Web.UI.WebControls;
9usingSystem.Web.UI.WebControls.WebParts;
10usingSystem.Web.UI.HtmlControls;
11usingSystem.Web.Services;
12
13publicpartialclass_2_PageMethods:System.Web.UI.Page
14{
15protectedvoidPage_Load(objectsender,EventArgse)
16{
17
18}
19
20[WebMethod]
21publicstaticDateTimeGetCurrentTime()
22{
23returnDateTime.UtcNow;
24}
25}
26
错误处理
包括超时和服务器抛出的异常
超时智能设置在WebService对象上
设置在PageMethods对象上
无法在每个MethodCall时指定
Sys.Net.WebServiceError
复杂数据类型基础
公有属性或公有Field会被释放和接受
容器对象
实现IList接口的对象
实现IDictionary接口的对象
Key必须是String
1<scriptlanguage="javascript"type="text/javascript">
2
3functiondoubleSalary()
4
5{
6
7//注意怎么传入一个复杂的数据的
8
9varemployee=newObject();
10
11employee.FirstName="Jeffrey";
12
13employee.LastName="Zhao";
14
15employee.Salary=1000;
16
17
18
19ComplexType.DoubleSalary(employee,doubleSalarySucceeded);
20
21}
22
23
24
25functiondoubleSalarySucceeded(result)
26
27{
28
29varmessage=String.format(
30
31"FirstName:{0}\nLastName:{1}\nFullName:{2}\nSalary:{3}",
32
33result.FirstName,
34
35result.LastName,
36
37result.FullName,
38
39result.Salary);
40
41
42
43alert(message);
44
45}
46
47
客户端代理使用细节
Invoke(ar1,…,argN,onSucceded,onFailed,userContext)
回调函数完整性
onSucceeded(result,userContext,methodName)
onFailed(result,methodName)
WebService级别默认属性
-timeout
-defaultUserContext
-defaultSucceededCallback
-defaultFailedCallback