Reprinted from http://www.voidcn.com/article/p-ryejjwwh-ry.html
WebServicesIntroduction
First give a concept of SOA, namely Service Oriented Architecture, Chinese is generally understood as a service-oriented architecture,
Since In terms of an architecture, it is generally believed that SOA includes the operating environment and programming model,
A set of new distributed software system construction methods and environments including architecture style and related methodology,
Cover the entire life cycle of the service. In the architectural style of SOA, service is the core abstraction method.
The services in SOA are built on a series of open standards-based foundations,
Web services define how to achieve a standardized method of communication between heterogeneous systems,
so that Web services can span operating platforms and implementation languages,
At the same time, Web services have become the main technology for realizing services in SOA.
As for SOA, too advanced technology is not discussed here (hehe), this blog post only introduces the technology of WebServices.
Introduction p>
Is there a way to achieve cross-application communication and cross-platform communication?
In other words, is there any way to realize that my application A can communicate with application B?
Or, what about the communication between the application I wrote in Java and the application developed in .Net?
Many times, we must use the above mentioned ones, For example, a cross-application program,
Take Tencent QQ as an example. I guess everyone has used the weather forecast tool on Tencent QQ! ! !
How is the weather forecast function above implemented?
There is a way, that is, Tencent puts a satellite into the sky, and establishes a meteorological department in the company to pay attention to the weather every day ,
Then update this weather forecast information on Tencent QQ every moment,
Indeed, This method really works, but if you really do this, it is estimated that Ma Huateng should be kicked out (how can he be so stupid?),
Then How is the above achieved? Don’t worry, and listen to me slowly~~~
Then, let’s take a look at what the cross-platform stuff is?
Here is mainly to take. Net platform and Java platform to illustrate examples,
If there are two companies, each company has its own project, one company uses .Net development, and one uses Java development,
Well, originally, these two companies were independent of each other, and the floodwater did not cause the river water, but one day, suddenly, the two companies merged,
After the merger, the boss found that if the two projects were combined, they would make a lot of money. What should I do for this?
Because the two projects are to be combined, there should always be communication between the two projects! ! !
But these two projects are based on different platforms, how to communicate? Are you in trouble ! ! !
Then look at a situation, such as a company’s server is Windows Server 2008,
So how does it connect with the UNIX or Linux server of the IT vendor? It’s complicated too ! ! !
WebServicesFeature introduction
WebServicesprovides a platform for building distributed applications, enabling software running on different operating systems and different devices, or using different programming languages and different The software developed by the manufacturer’s software development tools, and all possible developed and deployed software, can use this platform to achieve the purpose of distributed computing. The idea of WebServices is: makes the application also have the loose coupling of the Web distributed programming model.
WebServices Features:
(1), WebServices is self-contained. That is, there is no need to attach any software on the client, as long as the client supports HTTP and XML, it’s OK.
(2), WebServices is self-describing. Neither the client nor the server need to know anything except the format and content of the request and response messages.
(3), WebServices is cross-platform and cross-language. Both the client and the server can be implemented on different platforms and language environments, and at the same time, there is no need to change the existing code to support WebServices.
(4), WebServices is based on openness and standards. XML and HTTP are the main technical foundations of WebServices, and XML and HTTP have long become industry standards.
(5), WebServices is dynamic.
(6), WebServices can be combined. That is, through a WebService to access another WebService to achieve the purpose of composition. By combining WebServices, simple WebServices can be aggregated into complex services that implement more complex functions.
(7), WebServices is loosely coupled. It completely decouples the client and server.
(8), WebServices provides the ability to programmatically access. In other words, you can access Web services by writing programs.
(9), WebServices is built on the proven technology. Such as XML and HTTP.
(10), WebServices provides the ability to package existing applications.
(11), WebServices is published, searched and used via the Internet.
The above features, if you don’t know it now, don’t worry, there will be a detailed explanation later ! ! !
WebServicesWhat exactly is it?
If you put it simply, WebServices It is a set of function libraries, but this is different from the function libraries in our usual concept. The function libraries we usually use are either written by ourselves (write a set of function libraries in our own applications), p>
or call the underlying API (operating system API such as Win32 API), the above These two cases have one thing in common,
that is, the function library is located locally on the client For example, if you call Win32 API, you are calling the function library on the local operating system, and here is the difference between the concept that Web services are also a set of function libraries and the concept of function libraries mentioned above. Because the Web service is regarded as a set of function libraries, then this set of function libraries is not located locally, but on a remote machine (of course, you can Is the local machine In the device).
What is Web service?
is also network service, that is, some functions on the network that do not know that place are regarded as It is a set of services, and then I can use these services through the Internet.
what is Web service, the above statement is The copycat version, a slightly more serious statement is:
Web Service is an object or application component deployed on the Web.
Why WebServices< span style="color:rgb(128,0,255)">?
Why do I need to use WebServices? This must be analyzed according to the characteristics of WebServices and its advantages.
First of all, the above, also said, Web services , Is a set of application components on the network,
In this way, you can use Web services in your application to transfer your The application is upgraded to the service level.
Since it can be regarded as a set of services, it can of course be provided to other (other applications).
For example, I can expose some interfaces through Web services for others to use. As for these, do I need to charge? Then it depends on my mood. The previous example of querying the weather on Tencent QQ, this example, can be explained here,
In China , There should be only one satellite for weather forecasting, right? It’s impossible for Tencent to put a satellite into the sky specifically for weather forecasting, right?
But Tencent QQ can indeed check the weather, here, it can be solved through the Web service.
First of all, the China Meteorological Administration should have a satellite. According to the results returned by the satellite, the Meteorological Administration will release the weather conditions across the country in real time, and use these weather information to The form of the web service is open, and then Tencent QQ can access the weather conditions through this web service, and then feedback these weather conditions to QQ and it is OK.
Then, it is mentioned that the Web service is an application component, Since it is a component, you can use this component repeatedly,
At the same time, the application component can be used as a service through the Web service,
More powerful is that you can combine multiple WebServices into a more powerful WebServices,
< span style="color:black">and through the Internet! ! !
This is also a big advantage,
Then, the most basic WebServices is based on XML and HTTP
(Of course this is the most basic WebServices, for example, WebServices can also communicate through HTTPS or SMTP),
What are the benefits of this? Obviously, XML and HTTP are already standards,
Whether you are on the Java platform or the .Net platform (or you are using Web services ), since I use XML and HTTP, I’m not too lazy about Java or .Net. I don’t care if you are Linux or Windows. All of this has nothing to do with my Web service.
What I pay attention to is only to transmit XML through HTTP protocol and it’s OK,
As for how the XML was developed by the service provider or How these XMLs are used by the service requester has nothing to do with me. Here you can see another advantage of Web services, that is, cross-language and cross-platform (to achieve collaborative work), so you can achieve different through Web services Communication between applications and different platforms.
Web services allow the services to be used independently of the hardware or software platform on which the service is implemented and the programming language used to write the service,
According to the above two points,
It can solve how and how to use the application developed in Java that was proposed at the beginning. Net developed The problem of communication between applications,
At the same time, it can also solve the problem of connection between Linux or UNIX and Windows Server 2008.
Finally, by using different Web services, regardless of which programming language the Web services are implemented in,
We can all access from different platforms and operating systems, which greatly improves the ability of different applications to share data and applications.
and the Web service provides a way to build SOA Must have technical foundation.
From the above, we can see that through WebServices we have solved The question that I dare not even think about, why not make good use of such a powerful thing?
WebServicesarchitecture
In the Web service architecture, three roles are involved,
One is Web service provider, one is Web service intermediary, and the other is Web Service requester,
also involves three types of actions, namely publishing, finding, binding ,
WebService Provider:
Can publish Web service, and respond to requests to use its own services,
Web< /span>The owner of the service, it will wait for other services or applications to access itself.
WebService requester:
that is Web< /span>The user of the service function, it finds the required service through the service registry, which is the web service intermediary,
Reuse SOAP messages to send requests to Web service providers to obtain services.
WebService Intermediary:
also called service agent, used to Register and classify published Web service providers, and provide search services at the same time,
In simple terms, the role of the Web service intermediary is to associate a Web service requester with a suitable Web service provider.
The role of as a manager is generally achieved through UDDI.
发布:
通过发布操作,可以使 Web服务提供者向 Web 服务中介者注册自己的功能以及访问的接口。
发现(查找):
使得 Web 服务请求者可以通过 Web 服务中介者来查找到特点的种类的 Web 服务。
绑定:
这里就是实现让服务请求者能够使用服务提供者提供的服务了。
WebServices三种基本元素之 SOAP
SOAP 即 Simple Object AccessProtocol 也就是简单对象访问协议。
SOAP 呢,其指导理念是“唯一一个没有发明任何新技术的技术”,
是一种用于访问 Web 服务的协议。
因为 SOAP 基于XML 和 HTTP ,其通过XML 来实现消息描述,然后再通过 HTTP 实现消息传输。
SOAP 是用于在应用程序之间进行通信的一种通信协议。
因为是基于 XML 和HTTP 的,所以其独立于语言,独立于平台,并且因为 XML 的扩展性很好,
所以基于 XML 的 SOAP 自然扩展性也不差。
通过 SOAP 可以非常方便的解决互联网中消息互联互通的需求,
其和其他的 Web 服务协议构建起 SOA 应用的技术基础。
SOAP 协议的一个重要特点是它独立于底层传输机制,Web 服务应用程序可以根据需要选择自己的数据传输协议,
可以在发送消息时来确定相应传输机制。
由于 HTTP 协议本身的一些特点和局限性,
使得当 SOAP 使用HTTP 绑定的 Web 服务并不能满足某些企业应用的需求。
比如,HTTP 不是一个可靠传输协议,所以有可能在传输过程中出现问题,
然后 HTTP 协议基于Request/Response 模型,也就是说客户端需要在等待响应消息接收完成后才能继续执行,
而此时如果响应时间过长呢?
基于上面的这些需求,便需要选择合适的传输协议了。
关于这方面的内容的话,有点深奥了,有兴趣的可以去看看 IBM 的一些关于这方面内容的介绍。
还有一点需要提及一下,那就是 SOAP 是可以绕过防火墙的,将来将会作为 W3C 的标准进行发展。
WebServices三种基本元素之 WSDL
WSDL 即Web Services Description Language也就是 Web 服务描述语言。
是基于 XML的用于描述 Web 服务以及如何访问 Web 服务的语言。
服务提供者通过服务描述将所有用于访问 Web服务的规范传送给服务请求者,
要实现 Web服务体系结构的松散耦合,服务描述是一个关键,
不管是请求者还是服务提供者,通过服务描述便可以不必了解对方的底层平台,编程语言等,
服务描述与底层的 SOAP 基础结构相结合,
足以封装服务请求者的应用程序和服务提供者的 Web服务之间的这个细节。
WSDL 描述了 Web服务的三个基本属性:
(1)服务所提供的操作
(2)如何访问服务
(3)服务位于何处(通过 URL 来确定就 OK 了)
WebServices三种基本元素之 UDDI
UDDI 即 Universal Description,Discovery and Integration,也就是通用的描述,发现以及整合。
WSDL 呢,用来描述了访问特定的 Web 服务的一些相关的信息,可以在互联网上,
或者是在企业的不同部门之间,如何来发现我们所需要的 Web 服务呢?
而 Web 服务提供商又如何将自己开发的 Web 服务公布到因特网上,
这就需要使用到 UDDI 了,UDDI的话,是一个跨产业,跨平台的开放性架构,
可以帮助 Web 服务提供商在互联网上发布 Web 服务的信息。
UDDI 呢是一种目录服务,企业可以通过 UDDI 来注册和搜索 Web 服务。
简单来时候话,UDDI 就是一个目录,只不过在这个目录中存放的是一些关于 Web 服务的信息而已。
并且 UDDI 通过SOAP 进行通讯,构建于 . Net 之上。
开发 Web服务的方式
(1)开发阶段:
实现一个 Web 服务,使这个 Web 服务能响应和接收 SOAP 消息,
(这个呢,其实可以通过 Visual Studio 来帮助实现),
定义好逻辑模块(这个 Web 服务总要干点事情吧),
然后再撰写 WSDL 文件(这个呢,开发工具会自动生成的,不需要人工撰写了)
(2)部署阶段:
指定 Web 服务的传输协议,将 Web 服务注册到相应服务描述部署文件(这些也是可以由工具来自动完成的)
(3)发布阶段:
将 Web 服务的接口和调用的地址公开给客户端调用,
常用的发布方式为基于 Web 提供的WSDL 的链接,当然 UDDI 也是一个选择。
总结一下 WebServices的优点
其实呢,前面介绍的都是关于 W ebServices 的优点,在这里就只是浅要的总结一下了。
首先,WebServices 是基于 Internet 和异构平台的应用,
这样便可以方便的实现通过网络来通信,同时可以实现在不同的平台之间共享数据。
然后就是,WebServices 是基于 XML 和HTTP 的,
也就是基于标准和开放的,基于 XML 的话,扩展性自然好,自然跨语言。
基于 HTTP 的话,自然跨平台了。
最后,再回忆一下 WebServices 是一种应用程序组件吧,这样便可以将 WebServices 重复使用了。
谈谈 WebServices 的缺点
首先就是由于 XML 文件的难以解析,所以在使用 Web 服务的时候,会消耗较多的 CPU 和内存资源,
而后,SOAP 是基于XML 的,所以在网络传输中传输的是 XML 文件,
但是由 XML 文件相比于二进制文件来说,要大很多,自然就会消耗更多的网络资源了。
而后,由于通过 WSDL 解耦了Web 服务提供者和请求者,且 SOAP 消息时从发送者向接收者单向传送的,
这在一定程度上造成了 WebServices 是一种无状态服务,
尽管现在在 . Net 中可以通过 Session 来实现在客户端和服务端共享一些数据,
但是单单依靠 Session 来实现客户端和服务端的状态交互也太牵强了吧
WebServices 在数据绑定上也存在一些缺陷,
因为所有的数据在传输中都是使用的 XML 来实现的,
因此,需要在二进制数据和 XML 之间进行一个转换(通过序列化和反序列化来实现),
而在转换过程中有可能出现语义丢失的情况。
最后就是 WebServices 的技术要求相对比较高,
因为涉及到底层的 HTTP 协议以及SOAP ,WSDL 和UDDL 这三大平台元素,
所以学习的曲线也是比较长的,
当然,在 . Net 中可以通过 Visual Studio 非常快速和简单的开发和访问一个 Web 服务,
但这只限于在简单的使用上,而对于本质的东西,是比较难的。
后续
正如题目所言,是 WebServices 简介,既然是简介的话,那么自然就是以简为目标了,
说明一下的是,上面的这篇博文呢,源自前几天做的一个关于 WebServices 的演讲,
演讲的 PPT 还存有,有兴趣要的可以留个邮箱的。
. Net中 WebServices 的实战
下面呢,就来具体看看在 . Net中如何开发一个WebServices 以及如何使用这个 WebServices
开发环境:
Windows 7 下IIS 7.5
Visual Studio TeamSystem 2008
Sql Server 2008
首先来看看如何开发一个 WebServices
先建立一个 ASP.NET 应用程序项目,然后再在项目中添加一个 WebServices 服务,
然后就是在这个 WebServiceTest中编写业务逻辑了,
本次实例的业务逻辑呢就是从数据库“图书馆管理系统”中取出所有的读者的信息。
WebServiceTest.asmx中的代码如下
usingSystem.Web.Services;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Web.Configuration;
namespace WebServiceDemo
{
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public classWebServiceTest :System.Web.Services.WebService
{
[WebMethod]
public DataSet GetAllReader()
{
DataSet ds = newDataSet();
string connStr =
WebConfigurationManager.ConnectionStrings[“DBConnString”].ConnectionString;
string sqlStr = “SELECT[ReaderID],[ReaderIDType],[ReaderName],” +
“[ReaderSex],[ReaderBirth]” +
“FROM [图书馆管理系统].[dbo].[Reader]”;
using (SqlConnection sqlCon = new SqlConnection(connStr))
{
using (SqlCommand sqlCom =sqlCon.CreateCommand())
{
sqlCom.CommandType = CommandType.Text;
sqlCom.CommandText = sqlStr;
using (SqlDataAdapter sqlDa = newSqlDataAdapter(sqlCom))
{
sqlDa.Fill(ds);
}
}
}
return ds;
}
}
}
然后我再在这一个项目 WebServiceDemo中添加一个页面 Test . aspx
来实现访问自身应用程序中的 WebServices
(Test. aspx和 WebServiceTest . asmx 位于同一应用程序中)。
这个 Test . aspx 呢非常简单,仅仅在上面放了一个 GridView ,然后稍微写点 Code-Behind 就 OK 了,
其代码如下:
using System;
namespace WebService Demo
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
WebServiceTesttest= new WebServiceTest();
GridView1.DataSource = test.GetAllReader();
GridView1.DataBind();
}
}
}
}
再来浏览一下 Test . aspx页面
可以看出已经达到了预定的效果,也就是从数据库中通过 WebServices取出了数据。
而从上面的代码可以看出,仅仅是将 WebServices看做是一个类了,
将其作为一个类来进行使用(实质上也就是一个类而已)。
下面我们还需要看一种情况,
那就是,实现在另外的一个应用程序中访问上面建立的 WebServices。
其实这种情况呢,就是和访问网络上的 WebServices 一样了,
比如腾讯 QQ 就是使用这种方式来实现的,
为了模拟这种实现,我首先将上面建立的这个 ASP.NET 应用程序 部署到 IIS 上面,
且指定了一个端口为 81
然后我再建立一个项目 TestWebServices
并且在这个项目里面也添加一个页面 Test . aspx
在Test . aspx 上也只放一个 GridView 控件。
然后就要给这个项目添加 Web服务的引用了(右键TestWebService 点击“添加 Web 引用”)
如果您要访问的是互联网上的 Web服务,比如查询天气,
那么就需要在上面的 URL中写入 Web 服务所在的地址,然后“前往”就 OK 了,
由于本次的演示,我只是把我的 Web服务放在了本地的 IIS 上面,
所以在此处呢选择“本地计算机上的 Web服务”,
从上面的截图中就可以看出,在 81号端口上面我有一个 Web 服务,
就是前面的 Demo中建立的 Web 服务 WebServiceTest
然后我选择这个 Web 服务,单击它即可,
上面的这幅截图中便可以看出我在 Web 服务WebServiceTest 中公开的接口了,
由于我只在其中写了一个接口 GetAllReader ,所以在这里便只显示了一个了。
在这一步中,您便可以添加这个 Web 引用了,不过要注意的是,
如果在这一步添加 Web 引用的话,那么这个 Web 服务中所有被公开的方法都会被添加到您的项目中,
比如,如果我在上面的 Web 服务中还有一个 GetAllName 的方法的话,
那么在这一步添加 Web 引用的话,就会将 GetAllReader 和 GetAllName 全部添加到您的项目当中,
但是有时候,这样会太浪费了,因为我可能根本就不需要使用 GetAllName 而只需要 GetAllReader,
此时,可以单击上面的 GetAllReader 进入下一步,
在这一步中添加 Web 引用的话,那么就只会在项目中添加 GetAllReader 这个方法的引用了,
我们在这里使用这种方法来添加 GetAllReader 的引用。
单击“添加引用”,此时可以看到在项目中生成一些文件,
(这里呢,其实就是代理模式来实现了)
既然在项目中引用了这个 Web服务了,
那么下一步就是在 Test . aspx中使用这个 Web 服务了。
看看 Test . aspx的 Code-Behind 吧
using System;
namespace TestWebService
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
// WebServiceTest.GetAllReader 这一段是我引用后的服务名
WebServiceTest.GetAllReader.WebServiceTesttest =
new WebServiceTest.GetAllReader.WebServiceTest();
GridView1.DataSource = test.GetAllReader();
GridView1.DataBind();
}
}
}
}
下面就来看一看效果了
从上面的效果便可以看出,我们已经成功在另外的应用程序中访问了 Web 服务,
也可以得出 Web 服务实现了在不同应用程序之间数据的共享。
如果,读者对通过网络 URL 来访问WebServices 有疑问的话,
可以参考一下笔者的另外一篇稍微带有 WebServices 性质的博文,
在其中实现了一个访问互联网上提供的天气查询 Web 服务。
http://www.cnblogs.com/QinBaoBei/archive/2010/03/30/1700898.html
上面呢,通过几个 Demo 对WebServices 在 .Net 中的实战进行了一个简单的应用了。
在这里一切似乎都和前面所提到的 SOAP ,WSDL,UDDI 均扯不上关系,
其实不然,只不过这些全部都由别个(工具)给你完成了,而你只是简单的开发一下逻辑就 OK 了,
不过呢,简单归简单,理解前面的一些原理还是很有必要的。
这里还点一下,就是上面呢,我通过 WebServices 来查询数据库并且返回一个 DataSet,
不过,大家有没有想过,如果这个 DataSet 中的数据量有很大呢,比如 100万条,甚至更多,
这样的话,在网络传输中那不慢的要死啊,其实有一种比较好的解决办法,那就是压缩,
关于压缩呢,又有好几种方法,比如 GZIP 可以将其压缩到原来的一半以上,
这几种方法各有所长,一个晚上又过咯,很晚啦,以后有时间的话,可能会写一篇这方面的博文吧 ! ! !
关于WebServices 的简介就到这里了 ! ! !