在网上也找了一些,发现要么题目和内容不对应,明明是在Winform下异步调用,却写成在Asp.net异步调用WebService,有的调用方式在.NET3.5下不能通过,.NET3.5下取消了BeginXXXX,EndXXXX,奇怪,而用了以下方式。
建立一个WebService和一个WebApplication如图所示:
WebService代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
以下为同步调用WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//同步调用WebService
Response.Write("aaa<br>");
localhost.Service1 n = new localhost.Service1();
Response.Write(n.HelloWorld().ToString()+"<br/>");
Response.Write("bbb<br>");
}
}
}
输出结果:
aaa
Hello World
bbb
以下为异步调用WebService
1、需要在Default.aspx页的Page里加入Async="true"
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" Async="true" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
2、在Default.aspx.cs里代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//异步调用WebService
Response.Write("aaa<br>");
localhost.Service1 n = new localhost.Service1();
n.HelloWorldCompleted += delegate(object sender2, localhost.HelloWorldCompletedEventArgs e2)
{
//e.Result获取处理结果
Response.Write(e2.Result.ToString()+"<br/>");
};
n.HelloWorldAsync();
Response.Write("bbb<br>");
}
}
}
运行结果:
aaa
bbb
Hello World
试验结果:发现Hello World时在输出aaa,bbb之后输出Hello World的。
使用总结:
1、在ASPX页面设置一个允许异步调用的属性 Async="true"这样就可以在这个页面里进行异步调用了
2、同步调用的方法和异步调用的方法不一样,异步调用的方法是WebServic的方法名+Async()作为方法名,同步调用的方法就是WebService的方法名。
以上采用了匿名方法。将方法体的代码和委托对象相关联,如果要单独定义一个方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//异步调用WebService
Response.Write("aaa<br>");
localhost.Service1 n = new localhost.Service1();
n.HelloWorldCompleted += new localhost.HelloWorldCompletedEventHandler(HelloWorldCompleted);
n.HelloWorldAsync();
Response.Write("bbb<br>");
}
//完成事件处理方法
void HelloWorldCompleted(object sender, localhost.HelloWorldCompletedEventArgs e)
{
if (e.Error != null)
throw e.Error;
Response.Write(e.Result.ToString() + "<br>"); //e.Result获取处理结果
}
}
}
分享到:
相关推荐
这个是利用异步调用WebService的一个示例,是用asp.net开发的。供大家学习,谢谢!
1.异步调用aspx.cs方法 2.异步调用WebService方法 3.异步调用一般处理程序方法 4.异步调用动态网页方法 7.返回对象集合参数 8.传递对象集合参数
使用Asp.net 2.0新增特性异步页功能处理过程 简单介绍了客户端异步实现原理和实例里面涉及多线程的例子 当未使用异步页时,一个线程只能为同一个页面的请求服务. 即使页面请求过程中处理其它的I/O等操作 时,此线程也...
体验.net 2.0 的优雅(1) -- 异步WebService调用 ASP.NET 2.0页面框架的几点新功能 ASP.NET 2.0 中收集的小功能点 asp.net2.0中的webpart使用小记 2.0问题、错误解决办法 ASP.NET 2.0使用Web Part创建应用程序之二...
以一个ShoppingAssistant案例的ASP.NET Web服务为例,构建新的基于事件的编程模型,利用XmlSerializer类的XML串行化功能,将对象的内容串行化到XML文件中,在Web应用程序中有效地利用异步Web服务调用功能,利用...
14.2.5 异步调用webservice 274 14.3 wcf开发 276 14.3.1 定义wcf服务契约和引入 14.3.1 wcf服务契约 277 14.3.2 构架wcf服务,运行wcf 14.3.1 服务 281 14.3.3 生成wcf服务代理的 14.3.1 代码文件 283 ...
本文的客户端应用程序不包括 ...异步调用相关 Web Method 实现的! 1. updownload.asmx ,位于 IIS 的某个 Web 共享目录,代码如下,只有一句话: <%@ WebService Language=”c#” Codebehind=”UpDow
书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合有一些基础的ASP.NET初级程序员以及准备迁移到ASP.NET 2.0的编程老手。该书与《ASP.NET 2.0入门经典(第4版)》...
Web Service中的异步调用,状态管理 事务处理,安全性,自定义身份验证 Visual Studio Team System 中的单元测试与Web测试 案例分析:基于.NET2.0的大型电子商务系统 五 Windows 应用(Windows Forms) 创建...
书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合有一些基础的ASP.NET初级程序员以及准备迁移到ASP.NET 2.0的编程老手。该书与《ASP.NET 2.0入门经典(第4版)》...
1. ASP.NET AJAX 中,如何用 JavaScript 调用服务器端的方法? 这里不是指调用简单的 PageMethod,因为静态方法是不能操作当前页面的控件的,所以静态的 PageMethod 作用就跟普通的 WebService 一样,比较局限。 ...
3)xml加载后异步属性设置; 4)命名空间处理等问题; 下面上代码: ========ASPX前台代码======== 代码如下: <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs
1. ASP.NET AJAX 中,如何用 JavaScript 调用服务器端的方法? 这里不是指调用简单的 PageMethod,因为静态方法是不能操作当前页面的控件的,所以静态的 PageMethod 作用就跟普通的 WebService 一样,比较局限。 ...
》针对.NET Framework 4.0和多核编程进行了全面更新和修订,是帮助读者深入探索和掌握公共语言运行时、C#和.NET开发的重要参考,同时也是帮助开发人员构建任何一种应用程序(如Microsoft Silverlight、ASP.NET、...
客户端浏览器能够轻松通过Ajax实现REST资源的异步调用处理,同时也可以有效地减少应用服务器地压力 通过提供开放的RESTAPI,能够轻松实现与第三方的集成 平台服务 平台服务层的调用是通过RESTAPI进行的,...
111 <br>0184 如何在ASP.NET中获取文件的扩展名 111 <br>0185 如何在ASP.NET中用URL在页面之间传值 112 <br>0186 如何使用IsPostBack实现ASP.NET页面加载 112 <br>0187 如何利用输出缓存技术缓存...