一步一步用Visual C#创建Web服务
http://tech.ddvip.com 2007年10月05日 社区交流
内容摘要:在本文中,我将向大家介绍Web服务的一些基本知识以及如何用Visual C#一步一步地创建一个简单的Web服务。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace WebService1
{
///
/// Service1 的摘要说明。
///
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region Component Designer generated code
//Web 服务设计器所必需的
private IContainer components = null;
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB 服务
// DollarConvertToRMB() 服务完成美元到人民币的转换
// 若要测试此 Web 服务,请按 F5 键
[WebMethod]
public double DollarConvertToRMB(double Dollar)
{
return ( Dollar * 8.15);
}
}
}
在上面的方法DollarConvertToRMB()中,我们返回的是一个double类型的值――Dollar*8.15,其中的8.15我想是不言而喻的(就是美元到人民币的汇率)。不过现实的汇率是不固定的,而且每天都要变动,所以要根据当天实际的汇率来计算,那么我们就要连接到数据库获得最新的信息了。不过,这里作为一个简单实例,我们当然不需要搞得那么复杂,所以在这里我就姑且假定汇率为1:8.15。
来源:PConline 作者:王凯明 责编:豆豆技术应用