#分布式应用程序
有服务器有客户端互相数据交互的程序
#Webservice
使用xml数据格式,可以在多端进行数据交互,是一个规定的基于XML的通信标准
##服务器
新建一个Webservice项目,配置好之后返回xml数据。
添加新项->添加WEB服务,建好后其中
WebService.cs
负责逻辑部分,WebService.asmx
提供服务部分。将来引用WebService
服务时将直接引用WebService.asmx
文件来调用WebService
服务.
在WebService.cs
新建服务
/*
Web.Services.cs文件
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
/// <summary>
/// 方法上头的[WebMethod]是声明一个web服务方法,如果你想写个方法能让客户端调用并返回结果就必须在方法上头标注[WebMethod]
/// 如果是只负责逻辑运算或私有方法,并不打算给客户端结果,只给类方法内部调用就无需声明[WebMethod]
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>将运算结果转换成字符串返回</returns>
[WebMethod]
public string HelloWorld(int a, int b) {
int result = a + b;
return result.ToString();
}
}
再使用url,后缀为WebService.asmx
访问,可以看到已经写好的服务列表,点击后可以访问服务查看返回的xml数据。
##客户端
通过url访问后接收到xml数据,再对该数据解析
##应用
比如某银行使用的技术就是Webservice写的服务,把可以共享的数据写到Webservice的服务里,并把url作为接口提供给商家,商家使用该接口访问xml数据并解析出来再进行应用。