`
wangyihust
  • 浏览: 433675 次
文章分类
社区版块
存档分类
最新评论

第一个Atlas程序-异步调用Webservice

阅读更多

Creating an 'Atlas' Application

To begin, you will create an 'Atlas' Web application in Visual Studio. When you use the Visual Studio project template to create a new, blank 'Atlas' Web application, Visual Studio creates a normal Web site folder structure with the following additional items:

  • An executable file named Microsoft.Web.Atlas.dll, which resides in the Bin folder and provides server-side functionality.
  • A Web.config file with settings for 'Atlas' applications.

To create a new 'Atlas' Web application in Visual Studio

  1. In the File menu, click New, and then click Web Site.
  2. In the New Web Site dialog box, select the ASP.NET 'Atlas' Web Site template item.
  3. In the Location list, select File System.
  4. Specify a path and language for the application, and then click OK.

Creating a Web Service with a Single Method

Next, you will create an ASP.NET Web service with a single method that can be called by your 'Atlas' client script. Web services that work with 'Atlas' applications are not different from other ASP.NET Web services. 'Atlas' applications can run in any browser and connect to any type of Web-based service; the server does not have to be running ASP.NET or Windows.

To create the new Web service

  1. In Solution Explorer, right-click the name of the site, and then click Add New Item.
  2. In the Add New Item dialog box, under Visual Studio installed templates, select Web Service.
  3. Name the file HelloWorldService.asmx and clear the Place code in separate file check box.
  4. Select the language you want to use.
  5. Click Add.
  6. Add code to create a HelloWorld method in the Web service that returns the current date and time. The code must take a string as an input and return a formatted string with the current time and a message.

    The following example shows a complete Web service that includes the HelloWorld method:

    <script language="JavaScript" type="text/javascript"> function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } </script> <style type="text/css"> td.code { padding-left:10px; padding-right:10px; padding-top:0px; padding-bottom:0px; border-left: 1px solid #B1B1B1; border-bottom: 1px solid #DADADA; border-top: none; border-right: 1px solid #DADADA; } td.tab { text-align:center; font: verdana; width:15%; border-top: 1px solid #B1B1B1; border-bottom: none; border-left: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #F0F0F0; padding: 3px; } td.backtab { text-align: center; font: verdana; width: 15%; border-top: 1px solid #B1B1B1; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #E3E3E3; padding: 3px; } td.space { width:70%; font: x-small verdana; padding: 0px 0px 0px 0px; border-top: none; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; background: white; } </style>
    C# VB  
    		<%@ WebService Language="C#" Class="Samples.AspNet.HelloWorldService" %> using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols; namespace Samples.AspNet {  [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class HelloWorldService  :  System.Web.Services.WebService {    [WebMethod]   public string HelloWorld(String query)   {     string inputString = Server.HtmlEncode(query);     if(!String.IsNullOrEmpty(inputString))     {       return String.Format("Hello, you queried for {0}. The "         + "current time is {1}", inputString, DateTime.Now);     }     else     {       return "The query string was null or empty";     }   } }}
    		<%@ WebService Language="VB" Class="Samples.AspNet.HelloWorldService" %> Imports System.WebImports System.Web.ServicesImports System.Web.Services.Protocols Namespace Samples.AspNet<WebService(Namespace:="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ Public Class HelloWorldService   Inherits System.Web.Services.WebService   <WebMethod()> _   Public Function HelloWorld(ByVal query As String) As String     Dim inputString As String = Server.HtmlEncode(query)     If Not String.IsNullOrEmpty(inputString) Then       Return String.Format("Hello, you queried for {0}. The " _         & "current time is {1}", inputString, DateTime.Now)     Else       Return "The query string was null or  empty"     End If   End Function  End ClassEnd Namespace 

  7. Save and close the file.

Creating a Web Page with a Remote Method Call

In this part of the walkthrough, you will create an ASP.NET page and add the client script needed to call the Web service you created earlier.

To create a Web page

  1. Add a new ASP.NET page to your project and name it AtlasScript.aspx.

    Note  Be sure that you clear the Place code in separate file check box. For this walkthrough, you must create a single-file ASP.NET Web page.

  2. Switch to Source view.
  3. In the @ Page directive, set the Title attribute to Atlas Script Walkthrough as shown in the following example:

    <script language="JavaScript" type="text/javascript"> function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } </script> <style type="text/css"> td.code { padding-left:10px; padding-right:10px; padding-top:0px; padding-bottom:0px; border-left: 1px solid #B1B1B1; border-bottom: 1px solid #DADADA; border-top: none; border-right: 1px solid #DADADA; } td.tab { text-align:center; font: verdana; width:15%; border-top: 1px solid #B1B1B1; border-bottom: none; border-left: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #F0F0F0; padding: 3px; } td.backtab { text-align: center; font: verdana; width: 15%; border-top: 1px solid #B1B1B1; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #E3E3E3; padding: 3px; } td.space { width:70%; font: x-small verdana; padding: 0px 0px 0px 0px; border-top: none; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; background: white; } </style>
    C# VB  
    		<%@ Page Language="C#" Title="Atlas Script Walkthrough"  %>
    		<%@ Page Language="VB" Title="Atlas Script Walkthrough"  %>


  4. Copy the following markup and paste it into the file beneath the @Page directive.

    Note the addition of the <atlas:ScriptManager> element, which automatically adds the references to the required JavaScript files that provide 'Atlas' functionality. In this case, the element also adds a reference to the Web service file.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">     <head id="Head1" runat="server">   <atlas:ScriptManager runat="server" ID="scriptManager">     <services>       <atlas:servicereference path="~/HelloWorldService.asmx" />     </services>   </atlas:ScriptManager>   <style type="text/css">     body { font: 11pt Trebuchet MS;        font-color: #000000;        padding-top: 72px;          text-align: center }        .text { font: 8pt Trebuchet MS }   </style>     </head>  <body>  <form runat="server">   <div>     Search for     <input id="SearchKey" type="text" />     <input id="SearchButton" type="button" value="Search"       onclick="DoSearch()" />   </div>  </form>  <hr style="width: 300px" />  <div>  <span id="Results"></span>  </div> </body></html>   
  5. Save the file, but do not close it yet.

Now you will add client-side script that calls the remote Web service you created. The 'Atlas' client library provides the background services and support to make the remote call work.

To write scripts to call the Web service

  1. In the AtlasScript.aspx page, following the <div> element created in the previous task, paste the following code to call the Web service and display the results in the page:
    <script type="text/javascript">  function DoSearch()
       {
         var SrchElem = document.getElementById("SearchKey");
         //异步调用Web服务,传递两个事件的回调函数:完成之后(也可以有OnTimeOut处理)
         HelloWorldService.HelloWorld(SrchElem.value, OnRequestComplete);
       }
     
      //得到返回值.这个result是个objext类型的,可以传递一个类的实例过去,比如DataTable。
       function OnRequestComplete(result)
       {
         var RsltElem = document.getElementById("Results");
         RsltElem.innerHTML = result;
       } </script>
  2. The DoSearch function calls the remote method in the Web service, passing the value that the user enters in the text box, and passing the name of a callback function. The callback function is necessary because you are making an asynchronous call to the Web service, and you must provide a mechanism for the Web service to notify the client when the call returns. The name of the local callback function (OnRequestComplete) is arbitrary, but for the remote call to work, it must match the name of the callback function in the page. You can optionally provide a third parameter that contains the name of a function to be called if the server request times out while trying to call the remote method.

    The OnRequestComplete function is called when the asynchronous call is completed. The function takes the result parameter, which is used to pass the return value of the Web service call. In the example, you display the value of the result parameter as the innerHTML property of the <span> element.

  3. Make sure that the code following the @ Page directive looks like the following example, and then save and close the file.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">   <head id="Head1" runat="server">   <atlas:ScriptManager runat="server" ID="scriptManager">     <services>       <atlas:servicereference path="~/HelloWorldService.asmx" />     </services>   </atlas:ScriptManager>   <style type="text/css">     body { font: 11pt Trebuchet MS;        font-color: #000000;        padding-top: 72px;          text-align: center }       .text { font: 8pt Trebuchet MS }   </style>   </head> <body>  <form runat="server">   <div>     Search for     <input id="SearchKey" type="text" />     <input id="SearchButton" type="button"        value="Search"        onclick="DoSearch()" />   </div>  </form>  <hr style="width: 300px" />  <div>   <span id="Results"></span>  </div>  <script type="text/javascript">     function DoSearch()   {     var SrchElem = document.getElementById("SearchKey");     Samples.AspNet.HelloWorldService.HelloWorld(SrchElem.value,       OnRequestComplete);   }     function OnRequestComplete(result)   {     var RsltElem = document.getElementById("Results");     RsltElem.innerHTML = result;   }    </script> </body></html>

You can now test your page.

To test the page

  1. Run the AtlasScript.aspx page.
  2. Type some text into the search box, and then click the Search button. The text is returned from the HelloWorld method in the Web service and displayed in the page.

来源:http://atlas.asp.net/docs/Walkthroughs/GetStarted/Basic.aspx

分享到:
评论

相关推荐

    WebSerices异步调用方法总结

    一、异步调用方式 WebService 异步调用可以通过多种方式实现,包括: 1. Begin/End 方法:使用 Begin 方法启动异步调用,并使用 End 方法获取异步调用结果。 2. AsyncCallback 方法:使用 AsyncCallback 方法处理...

    webservice8 异步调用webservice

    在IT领域,Web服务(WebService)是一种通过网络进行通信的标准协议,它允许不同系统间的应用程序相互交互。当我们谈论“webservice8 异步调用webservice”时,这通常指的是在Axis2框架下,使用RPCServiceClient类的...

    使用CXF和camel-cxf调用webservice

    1. **创建服务客户端**:首先,你需要有一个服务的WSDL(Web Service Description Language)文件,这是定义Web服务接口和消息格式的标准XML文档。CXF可以基于WSDL自动生成Java客户端代码,这使得调用服务变得简单。...

    CXF入门教程(4) -- webService异步调用模式

    CXF入门教程(4) -- webService异步调用模式文章配套代码,文中涉及的异步调用客户端的代码放在com.neareast.test.cxf.asyClient包下。原文地址:http://blog.csdn.net/neareast/article/details/7726503

    jax-ws实现webservice调用

    **Java API for XML Web ...在"HelloServer"这个示例中,我们可以看到一个基本的JAX-WS服务端实现,它演示了如何发布一个简单的"Hello, World"服务。通过学习和实践,你将能够构建更复杂、功能丰富的Web服务应用。

    ETL工具(kettle)使用系列(四)-kettle调用webservice数据插入数据库-真实案例脱密处理.txt

    ETL工具(kettle)使用系列(四)-kettle调用webservice数据插入数据库-真实案例脱密处理

    ajax异步调用webservice实例

    1. 首先,创建一个WebService,定义一个操作方法,例如返回当前日期。在服务器端,可能使用.NET、Java或其他支持WebService的平台来实现。 2. 客户端使用JavaScript编写AJAX调用代码,调用WebService的URL,发送请求...

    maximo6-JAVA调用WEBSERVICE

    在本例中,我们可以看到一个名为`CustPack.java`的类,其中有一个`copySAPPOLINE()`函数,这个函数很可能是用来调用WEBSERVICE的具体操作。 调用代码示例可能如下: ```java import com.example.webservicename.*;...

    Android高级应用源码-Android调用Webservice.zip

    本资源"Android高级应用源码-Android调用Webservice.zip"提供了一个详细的示例,帮助开发者理解如何在Android应用程序中集成和使用Web服务。本文将深入探讨相关知识点。 一、Web Service简介 Web Service是一种基于...

    camel-cxf调用和发布webservice例子

    用camel-cxf调用webservice和发布一个webservice接口例子,首先启动QueryServiceMain主函数启动webservice接口,然后启动测试类TestWebservice。例子主要是实现java代码实现camel调用webservice接口

    WebService同步异步调用示例代码.rar

    总之,这个压缩包提供了一个深入理解WebService同步与异步调用的实例,同时涵盖了C#、SQL Server数据库操作以及可能的跨域通信和数据交换。通过分析和学习这段代码,你可以提升在Web服务开发和分布式系统通信方面的...

    webservice异步调用

    在本例中,我们将展示如何使用 C# 类库创建一个简单的 WebService,并实现异步调用。我们将创建一个名为 EzoneService 的 WebService,它提供了一个名为 HelloWorld 的方法,该方法返回一个字符串 "Hello World"。 ...

    异步调用WebService.zip

    本示例“异步调用WebService.zip”着重展示了如何在ASP.NET环境中实现异步调用Web服务,从而提高应用程序的响应速度和性能。 首先,让我们了解什么是异步调用。在传统的同步调用中,程序会等待一个操作完成后再执行...

    在WinForm程序中调用WebService

    在WinForm应用程序中调用WebService是一项常见的任务,它允许客户端应用程序与远程服务器上的服务进行交互,从而实现数据交换和功能扩展。以下是如何在WinForm中实现这一操作的详细步骤及涉及的相关知识点: 1. **...

    Laravel开发-laravel-webservice

    【Laravel开发-laravel-webservice】是一个专注于在Laravel框架中构建Web服务的项目,它利用了httpful库来提供高效、简洁的API交互。在本文中,我们将深入探讨Laravel框架的基础,Web服务的概念,以及httpful库如何...

    MULE开发实例1---并行调用多个webservice接口

    你可以选择不同的策略来合并这些响应,例如聚合所有的响应或仅返回第一个响应。 7. **设置Error Handling**:确保对可能的错误情况如网络问题或Web服务不可用进行适当的错误处理,可以通过添加异常策略来实现。 8....

    PB9-soap-WEBSERVICE例子.rar

    标题中的“PB9-soap-WEBSERVICE例子.rar”表明这是一个关于PowerBuilder 9(简称PB9)使用SOAP(简单对象访问协议)调用Web服务的示例压缩包。这个例子可能包含完整的代码、配置文件以及相关的说明文档,用于演示...

    异步调用webservice

    简单的查询明日天气,有天气图片,小程序还可以继续优化,主要提供学习参考使用,开发环境是VS2010

    Delphi调用WebService的实例(非常经典)[参考].pdf

    在 Delphi 中调用 WebService 是一种非常经典的实例。下面我们将详细介绍 Delphi 调用 WebService 的相关知识点。 首先, Delphi 调用 WebService 需要了解基本概念。WebService 是一种基于 XML 的远程过程调用...

Global site tag (gtag.js) - Google Analytics