学习asp.net ajax(四)(从客户端访问WebService)

服务器端释放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 将会调用代理

 页面的代码

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1_WebServiceFoundation.aspx.cs" Inherits="_1_WebServiceFoundation" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4None.gif
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
 6None.gif<head runat="server">
 7None.gif    <title>Untitled Page</title>
 8None.gif</head>
 9None.gif<body>
10None.gif    <form id="form1" runat="server">
11None.gif        <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
12None.gif            <Services>
13None.gif                <asp:ServiceReference Path="WebServiceFoundation.asmx" InlineScript="true" />
14None.gif            </Services>
15None.gif        </asp:ScriptManager>
16None.gif        
17None.gif        <input type="button" value="Get Random" onclick="getRandom()" />
18None.gif        <input type="button" value="Get Range Random" onclick="getRandom(50, 100)" />
19None.gif        
20None.gif        <script language="javascript" type="text/javascript">
21None.gif            function getRandom(minValue, maxValue)
22ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
23InBlock.gif                if (arguments.length != 2)
24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
25InBlock.gif                    Sample.WebServiceFoundation.GetRandom(getRandomSucceeded);
26ExpandedSubBlockEnd.gif                }

27InBlock.gif                else
28ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
29InBlock.gif                    Sample.WebServiceFoundation.GetRangeRandom(minValue, maxValue, getRandomSucceeded);
30ExpandedSubBlockEnd.gif                }

31ExpandedBlockEnd.gif            }

32None.gif            
33None.gif            function getRandomSucceeded(result)
34ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
35InBlock.gif                alert(result);
36ExpandedBlockEnd.gif            }

37None.gif        </script>
38None.gif    </form>
39None.gif</body>
40None.gif</html>
41None.gif

对应的的Webservice

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif<%@ WebService Language="C#" Class="Sample.WebServiceFoundation" %>
 2None.gif
 3None.gifusing System;
 4None.gifusing System.Web;
 5None.gifusing System.Web.Services;
 6None.gifusing System.Web.Services.Protocols;
 7None.gifusing System.Web.Script.Services;
 8None.gif
 9None.gifnamespace Sample
10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
11InBlock.gif    [WebService(Namespace = "http://tempuri.org/")]
12InBlock.gif    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13InBlock.gif    [ScriptService]
14InBlock.gif    public class WebServiceFoundation : System.Web.Services.WebService
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        [WebMethod]
17InBlock.gif        public int GetRandom()
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            return new Random(DateTime.Now.Millisecond).Next();
20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22InBlock.gif        [WebMethod]
23InBlock.gif        public int GetRangeRandom(int minValue, int maxValue)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28ExpandedBlockEnd.gif}

客户端访问PageMethod

服务器端

只能在aspx页面中定义

只能那个是公开静态方法

使用WebMethodAttribute标记

ScriptManager的EnablePageMethods属性设置为true

客户端

通过PageMethods.MethodName 访问

 

//得到国际标准时间

DateTime.UtcNow

对应代码2

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="2_PageMethods.aspx.cs" Inherits="_2_PageMethods" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4None.gif
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
 6None.gif<head runat="server">
 7None.gif    <title>Untitled Page</title>
 8None.gif</head>
 9None.gif<body>
10None.gif    <form id="form1" runat="server">
11None.gif        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
12None.gif        
13None.gif        <input type="button" value="Get Current Time" onclick="getCurrentTime()" />
14None.gif    
15None.gif        <script language="javascript" type="text/javascript">
16None.gif            function getCurrentTime()
17ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
18InBlock.gif                PageMethods.GetCurrentTime(getCurrentTimeSucceeded);
19ExpandedBlockEnd.gif            }

20None.gif            
21None.gif            function getCurrentTimeSucceeded(result)
22ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
23InBlock.gif                alert(result);
24ExpandedBlockEnd.gif            }

25None.gif        </script>
26None.gif    </form>
27None.gif</body>
28None.gif</html>
29None.gif
ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gifusing System.Web.Services;
12None.gif
13None.gifpublic partial class _2_PageMethods : System.Web.UI.Page
14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif
18ExpandedSubBlockEnd.gif    }

19InBlock.gif
20InBlock.gif    [WebMethod]
21InBlock.gif    public static DateTime GetCurrentTime()
22ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
23InBlock.gif        return DateTime.UtcNow;
24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}

26None.gif

错误处理

调用时可以提供一个额外的错误回调函数

包括超时和服务器抛出的异常

超时智能设置在WebService对象上

   设置在PageMethods对象上

   无法在每个MethodCall时指定

Sys.Net.WebServiceError

 

复杂数据类型基础

公有属性或公有Field会被释放和接受

 

容器对象

   实现IList接口的对象

   实现IDictionary接口的对象

     Key必须是String

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif<script language="javascript" type="text/javascript">
 2None.gif
 3None.gif                 function doubleSalary()
 4None.gif
 5ExpandedBlockStart.gifContractedBlock.gif                 dot.gif{
 6InBlock.gif
 7InBlock.gif           //注意怎么传入一个复杂的数据的
 8InBlock.gif
 9InBlock.gif                      var employee = new Object();
10InBlock.gif
11InBlock.gif                      employee.FirstName = "Jeffrey";
12InBlock.gif
13InBlock.gif                      employee.LastName = "Zhao";
14InBlock.gif
15InBlock.gif                      employee.Salary = 1000;
16InBlock.gif
17InBlock.gif                      
18InBlock.gif
19InBlock.gif                      ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
20InBlock.gif
21ExpandedBlockEnd.gif                 }

22None.gif
23None.gif                 
24None.gif
25None.gif                 function doubleSalarySucceeded(result)
26None.gif
27ExpandedBlockStart.gifContractedBlock.gif                 dot.gif{
28InBlock.gif
29InBlock.gif                      var message = String.format(
30InBlock.gif
31InBlock.gif                            "First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
32InBlock.gif
33InBlock.gif                            result.FirstName,
34InBlock.gif
35InBlock.gif                            result.LastName,
36InBlock.gif
37InBlock.gif                            result.FullName,
38InBlock.gif
39InBlock.gif                            result.Salary);
40InBlock.gif
41InBlock.gif                            
42InBlock.gif
43InBlock.gif                      alert(message);
44InBlock.gif
45ExpandedBlockEnd.gif                 }

46None.gif
47None.gif

客户端代理使用细节

函数调用完整性

Invoke(ar1,…,argN,onSucceded,onFailed,userContext)

回调函数完整性

 onSucceeded(result,userContext,methodName)

 onFailed(result,userContext,methodName)

WebService级别默认属性

-timeout

-defaultUserContext

-defaultSucceededCallback

-defaultFailedCallback

转载于:https://www.cnblogs.com/couhujia/archive/2009/10/24/1589024.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值