微软.NET战略的一个比较重要的部分就是webservice,利用webservice我们可以创建真正有效的分布式应用程序。
下面,我们对webservice做一些说明。
假设A是客户端,B是webservice服务端,用户通过http协议向服务器发送soap请求,webservice返回客户端XML格式的数据。
现在我们看一看创建一个webservice的大致过程:
服务端的webservice是必须要建的。中间的soap,xml我们不用去关心,在客户端这边,比较重要的是如何从webservice取得对象?答案是用的是proxy对象。客户端由代理对象(proxy)负责
webservice的通信。所以在客户端使用webservice,完全和使用一个本地对象是一样的。
我们现在以一个简单的实例来说明。
打开vs.net,新建工程(asp.net web服务),在位置中键入http://localhost/webserver,其中webserver就是工程的名字。确定后,出现一个Service1.asmx.cx,双击,出现代码窗口,
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace webserver
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
(1)
public class Service1 : System.Web.Services.WebService
{
{
public Service1()
{
//CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region Component Designer generated code
{
//CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region Component Designer generated code
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
{
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
{
if(disposing && components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion
// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键
// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }
}
}
// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键
// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }
}
}
下面在(1)处加入
[WebService(Namespace=http://localhost/webserver/)]
这是因为soap是基于http协议上的,客户端无法知道webservice位于那个服务器上。在实际应用中,比如http://www.ourfly.com上放置这个webservice,则Namespace改
NET企业战略
http://news.newhua.com/html/Deve_lan/2007-3/15/0731511150372181_64_2.shtml