服务器端释放WebService方法
名词解释
Ajax技术:异步刷新(javascript)
Ajax.Net:基于asp.net框架是一个ajax框架(不是微软体统的框架)
Asp.net Ajax: 微软提供的框架
编写一个普通的Asp.net webservice
为WebSrvice类添加自定义属性标记
-ScriptServiceAttribute
释放WebService方法
- 访问级别为public
-使用WeMethidAttribute
为页面中ScriptManager(Proxy)引入asmx文件
客户端访问WebService
[Namespaces.]ClassName.MethodName
依次传入参数
传入一个方法作为成功后的回调函数
即使没有返回值也会调用回调函数
InlineScript=false 将会调用代理
页面的代码
Code
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1_WebServiceFoundation.aspx.cs" Inherits="_1_WebServiceFoundation" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
12 <Services>
13 <asp:ServiceReference Path="WebServiceFoundation.asmx" InlineScript="true" />
14 </Services>
15 </asp:ScriptManager>
16
17 <input type="button" value="Get Random" onclick="getRandom()" />
18 <input type="button" value="Get Range Random" onclick="getRandom(50, 100)" />
19
20 <script language="javascript" type="text/javascript">
21 function getRandom(minValue, maxValue)
22 {
23 if (arguments.length != 2)
24 {
25 Sample.WebServiceFoundation.GetRandom(getRandomSucceeded);
26 }
27 else
28 {
29 Sample.WebServiceFoundation.GetRangeRandom(minValue, maxValue, getRandomSucceeded);
30 }
31 }
32
33 function getRandomSucceeded(result)
34 {
35 alert(result);
36 }
37 </script>
38 </form>
39</body>
40</html>
41
对应的的Webservice
Code
1<%@ WebService Language="C#" Class="Sample.WebServiceFoundation" %>
2
3using System;
4using System.Web;
5using System.Web.Services;
6using System.Web.Services.Protocols;
7using System.Web.Script.Services;
8
9namespace Sample
10{
11 [WebService(Namespace = "http://tempuri.org/")]
12 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13 [ScriptService]
14 public class WebServiceFoundation : System.Web.Services.WebService
15 {
16 [WebMethod]
17 public int GetRandom()
18 {
19 return new Random(DateTime.Now.Millisecond).Next();
20 }
21
22 [WebMethod]
23 public int GetRangeRandom(int minValue, int maxValue)
24 {
25 return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
26 }
27 }
28}
客户端访问PageMethod
服务器端
只能在aspx页面中定义
只能那个是公开静态方法
使用WebMethodAttribute标记
ScriptManager的EnablePageMethods属性设置为true
客户端
通过PageMethods.MethodName 访问
//得到国际标准时间
DateTime.UtcNow
对应代码2
Code
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2_PageMethods.aspx.cs" Inherits="_2_PageMethods" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
12
13 <input type="button" value="Get Current Time" onclick="getCurrentTime()" />
14
15 <script language="javascript" type="text/javascript">
16 function getCurrentTime()
17 {
18 PageMethods.GetCurrentTime(getCurrentTimeSucceeded);
19 }
20
21 function getCurrentTimeSucceeded(result)
22 {
23 alert(result);
24 }
25 </script>
26 </form>
27</body>
28</html>
29
Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Web.Services;
12
13public partial class _2_PageMethods : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 [WebMethod]
21 public static DateTime GetCurrentTime()
22 {
23 return DateTime.UtcNow;
24 }
25}
26
错误处理
调用时可以提供一个额外的错误回调函数
包括超时和服务器抛出的异常
超时智能设置在WebService对象上
设置在PageMethods对象上
无法在每个MethodCall时指定
Sys.Net.WebServiceError
复杂数据类型基础
公有属性或公有Field会被释放和接受
容器对象
实现IList接口的对象
实现IDictionary接口的对象
Key必须是String
Code
1<script language="javascript" type="text/javascript">
2
3 function doubleSalary()
4
5 {
6
7 //注意怎么传入一个复杂的数据的
8
9 var employee = new Object();
10
11 employee.FirstName = "Jeffrey";
12
13 employee.LastName = "Zhao";
14
15 employee.Salary = 1000;
16
17
18
19 ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
20
21 }
22
23
24
25 function doubleSalarySucceeded(result)
26
27 {
28
29 var message = String.format(
30
31 "First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
32
33 result.FirstName,
34
35 result.LastName,
36
37 result.FullName,
38
39 result.Salary);
40
41
42
43 alert(message);
44
45 }
46
47
客户端代理使用细节
函数调用完整性
Invoke(ar1,…,argN,onSucceded,onFailed,userContext)
回调函数完整性
onSucceeded(result,userContext,methodName)
onFailed(result,userContext,methodName)
WebService级别默认属性
-timeout
-defaultUserContext
-defaultSucceededCallback
-defaultFailedCallback