Web Service 基于SOAP协议,而SOAP 【Simple Object Access Protocol】本身符合XML【Extensive Markup Language】 语法规范的
SOAP协议的全称是简单对象访问协议,SOAP致力于以XML形式提供一个简单、轻量的用于在分散或分布环境中交换结构化和类型信息的机制。SOAP只规范对象访问的方式**,而不限制具体实现的技术环境,这意味着SOAP协议是一种跨平台的协议**:一个.NET客户端程序可以按照SOAP协议访问一个基于JavaEE技术体系结构的Web Service。SOAP访问仍然基于HTTP协议,同时其内容又以XML形式展现。
SOAP规范由四部分组成:
① SOAP信封(SOAP envelop)
② SOAP编码规则(SOAP encoding rules)
③ SOAP RPC表示(SOAP RPC representation)
④ SOAP绑定(SOAP binding)
顺便回忆一下:
TCP/IP 4层
第一层:物理层,TCP/IP 里无对应;
第二层:数据链路层,对应 TCP/IP 的链接层;
第三层:网络层,对应 TCP/IP 的网络层;
第四层:传输层,对应 TCP/IP 的传输层;
第五、六、七层:统一对应到 TCP/IP 的应用层。
(1)WSDL介绍
WSDL(Web Service Description Language)是Web服务描述语言,它是一种由微软、IBM、Intel等大型供应商提出的语言规范,目的就是为了描述Web服务器所提供的服务,以供使用者参考。WSDL是一种复合XML语法规范的语言,它的设计完全基于SOAP协议,当一个Web Service服务器期望为使用者提供服务说明时,WSDL是最好的选择之一。
web service 提供的方法
string GetSumString(int para1, int para2)
对应的wsdl文件
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
//方法的名字
<s:element name="GetSumString">
//参数数量、每个参数的类型:
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="para1" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="para2" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
// 以及返回参数的类型:
<s:element name="GetSumStringResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetSumStringResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="GetSumStringSoapIn">
<wsdl:part name="parameters" element="tns:GetSumString" />
</wsdl:message>
<wsdl:message name="GetSumStringSoapOut">
<wsdl:part name="parameters" element="tns:GetSumStringResponse" />
</wsdl:message>
<!-- 这里省略其他定义 -->
</wsdl:definitions>
(2)获取和使用WSDL
当Web Service服务器提供WSDL时,就可以通过特定的工具获得WSDL文件。最直接的方式就是在URL中直接添加WSDL参数,来发送得到WSDL文件的请求,如下所示:
http://localhost:6105/MySimpleService.asmx?wsdl
WebMethod特性包含哪些属性
WebMethod特性在Web Service中被用来申明一个公开方法,了解其使用方法是在正确编写Web Service的基础。在WebMethod特性中,一共包含了6个属性,这6个属性对WebMethod的使用非常重要。
public class MySimpleService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public string WithSession()
{
return TryGetSession();
}
[WebMethod(EnableSession = false)]
public string WithoutSession()
{
return TryGetSession();
}
private string TryGetSession()
{
if (Session == null)
{
return "Session is Forbidden";
}
if (Session["number"] == null)
{
Session["number"] = 0;
}
Session["number"] = (int)Session["number"] + 1;
return Session["number"].ToString();
}
}
(3)CacheDuration属性
该属性指示启用对Web Service方法结果的缓存。服务端将会缓存每个唯一参数集的结果,该属性的值指定服务器端应该对结果进行多少秒的缓存处理。如果该值为0,则禁用对结果进行缓存;如果不为零,则启用缓存,单位为秒,意为设置多少秒的缓存时间。默认该值被设为0。
该属性指示启用对Web Service方法结果的缓存。服务端将会缓存每个唯一参数集的结果,该属性的值指定服务器端应该对结果进行多少秒的缓存处理。如果该值为0,则禁用对结果进行缓存;如果不为零,则启用缓存,单位为秒,意为设置多少秒的缓存时间。默认该值被设为0。
[WebMethod(CacheDuration = 10, EnableSession = true)]
public string WithCache()
{
if (Session[“number”] == null)
{
Session[“number”] = 0;
}
Session["number"] = (int)Session["number"] + 1;
return Session["number"].ToString();
}
**
WS 代理
**
Web Service的异常机制