Talking about the practical web Service

Source: Internet
Author: User
Tags definition soap connect web services wsdl advantage
The Web provides a brief introduction to SOA and personal understanding of webservice. Some issues needing attention in the implementation process under the introduction of a specific project


Quote article:
(1) http://webservices.xml.com/pub/a/ws/2003/09/30/soa.html
(2) Ms-help://ms. Vscc.2003/ms. Msdnqtr.2003feb.2052/cpguide/html/cpconanatomyofsoapwebservicelifetime.htm
(3) Ms-help://ms. Vscc.2003/ms. Msdnqtr.2003feb.2052/cpguide/html/cpconcustomizingsoapinaspnetwebserviceswebserviceclients.htm






"In the words of the world, a long time must be combined, a long time must be divided." ----The software industry is the same?



Now the software industry has a wide range of technology platforms, architecture, but due to the different aspects of the platform concerned, each has its own characteristics, the platform is difficult to communicate, thus forming an isolated information island. So how do you connect these islands of information together.? The industry has put forward a lot of solutions, a very famous program is serviced-oriented (service-oriented). What is service oriented? This is the definition of it: "SOA is a architectural style whose goal is to achieve L Oose coupling among interacting software agents. A service is a unit of work do by the A service provider to achieve desired end results for a service consumer. Both provider and consumer are roles played by software agents on behalf of their owners. (1). Service-oriented is a collection of services. Each service can communicate with each other (including simple data transfer and multiple services to participate in a single activity), and it achieves the goal of integrating each system by providing some external interface for each system. The industry also has some corresponding architectures, such as DCOM. Corba. Java EE. But it is not possible to fully integrate across systems. The reason for personal feeling is as follows:

1. The interface definition language cannot be received by different systems. Both Dcom.cobra and EE have their own interface Definition language (binary). None of them can be accepted by other systems. And the interface is difficult to change once it is set. And the user's needs are changing.

2. The use of binary network protocols for data communication. It is difficult to cross the firewall, and the protocols used are not widely received, which is particularly critical to the use of the Internet.

A new architecture emerged, which is XML Web services, which Microsoft defines as "XML Web Services is a programmable entity that provides specific functional elements such as application logic, and any number of, possibly disparate, systems can be used in a common Internet standards, such as XML and HTTP, access it. Its core feature is the high degree of abstraction that exists between the implementation and use of the service. "Web services are being quickly accepted by the various platforms." Compared to other architectures, it has the advantage of:

1. Interface Definition language. It uses WSDL as the interface definition language. This is a document based on XML format, and text can be understood by various systems and platforms.

2. Data communication using other widely accepted protocols such as HTTP,SOAP,SMTP. HTTP is one of the basic protocols of the Internet



How do you get an in-depth understanding of Web services? Personally, I think we should start with the following:

I. Purpose

It is a way to implement SOA in order to connect different systems and computing devices. Implementing system and data interoperability

Simply to be able to access data from different systems and computing devices, without having to worry about how the data is stored in their systems and devices. It can be said that you don't have to worry about how data is encapsulated (like the black box in OO).

Two. Define

XML Web service as the name implies is the use of XML to provide WEB services. In fact, strictly speaking, the web can not be, is the XML Service, because not all Web service need to webserver. Service is to provide the user with my features, that is, to provide users with an interface, this is the Web service. So Web service is definitely not a new distributed object. And DCOM, CORBA,J2EE is essentially a distributed object.

Three. Composition

1. Some components that can handle XML.

First, the XML Web service (or Web service) has to be able to handle XML, and how the components that handle XML are designed. Different languages, platforms have different ways. can be oo (object-oriented). It can be other ways. NET is through. Net Some of the classes provided by the Framework implement the



2. XML Document

As we said earlier, one of the advantages of Web Services is an xml-based document with Interface Definition language (IDL), because Web service is an SOA (service oriented), and the goal of SOA is to create a loose coupling between systems, Therefore, the service and consumer service can not use object as the link of data communication or wedge (Contract), it must be used as an XML document for wedge. That is to enable both service providers and consumers to understand the implications of Contract. Web Service uses WSDL to describe an XML document. That is to describe the external interface. Using XML schemas to describe the data in a document

3. The carrier of XML document

With an XML document, you need a protocol that hosts it. The WEB service uses SOAP as a download. SOAP: Simple Object Access Protocol, which is strictly the wrong name, because it is not used to access objects. Ms gives it the definition of "SOAP is a simple lightweight protocol that is xml-based and is used to exchange structured and type information on the Web." It hosts the XML document in an envelope manner. The envelope is divided into two parts: the head of the envelope and the body of the envelope. Headers are typically used to hold auxiliary information, such as security (signature and encrypted data) and routing information, which is used to Pau the interface description of the service and the specific data

4. Address of the service

Used to tell the service consumer where to access the service, where the service is located, and

Microsoft provides UDDI (common description, Discovery, and integration).



Net to implement Web service is through asp.net. It encapsulates a lot of detail and makes it easy for developers to develop webservice, but the result is that understanding is inherently difficult. (MS's consistent style).

L Invoke Web Service in the form of Web methods.

Previously, the exchange of Web service is actually an XML document, considering that many programmers

It's not customary to manipulate XML directly (like manipulating objects and their methods), which will be received in. Net

The XML document is converted to an object or a value as a parameter of a method, while the resulting value or

Like deserialization into an XML document sent back to the service consumer, the service consumer can use the traditional

Call the method of an object to send a request to a service provider to obtain the desired number

According The process from the consumer's request for service to the corresponding result is as follows (2)-:





From this flowchart (2) You can see. The client wants to invoke the Web service to send the message first.

And the server is informed by the message that there is a consumer to call it. So, in a sense. Web

Service is a message based architecture, except that the message is an xml-based document

L The generation of proxy class

The role of the proxy class is to facilitate communication with Web services. In. Net can be automatic and hand

Dynamic, both of which are generated from the WSDL file of the service. As mentioned above, for convenience,

The idiom of the programmer. NET deserializes the XML document into objects, as well as in the proxy class.

This gives us the illusion that the client has been regenerated from the server. For example: a

Web method test Returns a objecta to the customer

public class Objecta

{

public string Fielda

public string Fieldb

}

Then the proxy class will also generate a objecta. Its structure and service-side objecta is a modular one

Kind of. But in fact it's just. NET is artificially generated for us to use Web service (which is also

Many people think that WebService is one of the reasons for remote object access. For Web service customers

End-to-end, only XML documents are received, and the data in the document is described in XML schemas. As for

How to use it is the individual characteristics of each Web client, and for. NET it will make some complex customizations

XML Schema type is converted to object. Some other clients are not converted to object. For example

Soap Tookit. It is converted into an XML node object





The following is a practical project to illustrate how to implement a Web Service.



I. Project Overview

A company has an existing product producta. (PA), it needs to get some data for business processing. The data are varied by region and time, and the data is determined by the corresponding government agencies, and the data of government agencies in different regions are different, and each change needs to be communicated to the product user in the form of an official letter, or the company will organize itself and tell the user to update it. This results in different areas of different versions, user upgrades difficult, and the data between government agencies is difficult to communicate and compare

Two. The solution

Use the functionality of the Web service to expose all government agency data (J of course permissions). Product (PA) as a WebService client can access different data in real time, so that between the product and the data, between the data and the data, the user and the data are all connected seamlessly.




Three. Specific implementation

Two participants with services: providers of services and consumers of services

For the product (PA) It is the consumer of the service. So it's just a Web Service Client. can be any

What type of program. The existing PA is a VB version of the program, you can through the proxy class to access different

of Service.

For data (A.B.C ...). They are both service providers and service consumers. Because the user

Need to be able to directly query some data, so for users, we make a website for each data to

Easy user inquiries (using asp.net to establish)

We said that the service is to expose the existing functions and data through the interface without the need for care function and

How the data is implemented in the respective systems, so the function and the data itself can still be used

of the technology. Implement the feature and data section or use a three-tier architecture, the Web service as a separate

Layer (façade layer). The frame diagram is as follows:





. NET provides an ASMX file as a concrete implementation of the file. There are several issues to be aware of in the implementation process:



L SOAP Format

SOAP is the carrier of service wedge, it also has a certain format. For envelopes in soap. There are two forms of document and RPC. There are two formats for data in a SOAP request and response: encoded and literal.

The Document style refers to the BODY element being formatted as one or more contiguous message parts (3) under the BODY element. Using the XSD schema

RPC style is the formatting of the Body element (3) According to the SOAP specification for RPC using the content of the soap (that is, the 7th section of the commonly called SOAP specification). It is to encapsulate all elements in an XML element without using an XSD Schema

The literal style is an XML document that hosts parameters in the BODY element and encodes it as self-contained. Using the XSD Schema

The encoded style is formatted using the encoding rules in the SOAP specification. It does not require an XSD Schema

Document/literal is the default setting in ASP.net. Literal only under the document

Can be used, while encoded can be used simultaneously under document and RPC. We can customize

The style of soap ... NET provides properties to accomplish this function:

SoapDocumentMethod

SoapDocumentService

SoapRpcMethod

SoapRpcService

Please refer to MSDN for specific use



The design of L-Entity (Entity)

I didn't use a dataset in the project. A custom class is used. The main consideration is

A. Easy to operate

B. The dataset is a Coarse-grained object. Include some properties and objects that we don't need. Increased access time

If the Web method takes an entity as an argument, the validity of the entity needs to be judged. For example, whether the property content in the entity is empty, the length of the value, and so on. Because. NET automatically deserializes the received XML message into an object, and the value of the object is set by the client and we cannot control it. Then it is necessary to judge the validity of the data. There are generally three ways.

A. Validation at the business logic level.

Such a disadvantage when the amount of code is more, especially when the property is more often, the change is inconvenient

B. Validating through an entity-corresponding XSD file

C. Validation by setting attributes on entity attributes

B and C are used in the reflection technology to achieve, the advantage is to reduce the amount of code, changes more convenient, recommended use. I used the attribute method in the project.



L Safety of WebService

Security is a very important aspect of Web Service.

Security mainly includes two aspects of authorization and signature. Microsoft and other companies have jointly presented a

Toolkit to solve this problem: Web Service enhancment (WSE)-. Now the latest

The version is 2.0. Implemented a number of important service specifications.



The realization of L exception

In Web service, exceptions are usually published in the form of soap errors. There is a fault element in the SOAP message that is specifically used to hold this information. In a Web service created with ASP.net, the exception is serialized and then placed in the fault element. The error is typically encapsulated as SoapException to the client, but a soapheaderexception is thrown if an error in the header part of soap is encountered. For SoapException, the error message is placed in the detail attribute, which contains the details of the error. In the actual application process these details are not friendly to the customer, at this time we need to reinstall the next SoapException. The method is to add a child element to the Detail property (serialized as a detail element) on the server that throws the exception to hold some friendly error messages, and then display the error message to the user at the receiving end under this child element








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.