Address: http://www.cnblogs.com/denylau/archive/2010/07/23/1783530.html
1.1 Basic concepts of Web Service
Web service, also known as XML Web Service WebService, is a lightweight independent communication technology that can receive requests from other systems on the Internet or intranet. Yes: The software services provided on the Web through soap are described using the WSDL file and registered through UDDI.
XML :( Extensible Markup Language) Extended Markup Language. Short-term-oriented temporary data processing and hichina network are the basis of soap.
Soap: (Simple Object Access Protocol) Simple Object Access Protocol. Is the communication protocol of XML Web Service. After you find your WSDL Description document through UDDI, you can use soap to call one or more operations in your web service. Soap is a standard for calling methods in XML format. It supports different underlying interfaces, such as HTTP (s) or SMTP.
WSDL :( Web Services Description Language) the WSDL file is an XML document that describes a group of soap messages and how to exchange them. In most cases, it is automatically generated and used by the software.
UDDI (Universal Description, discovery, and integration) is a new project mainly for Web service providers and users. Before you can call a web service, you must determine the business methods contained in the service, find the called interface definition, and compile software on the server, UDDI is a mechanism that guides the system to find the corresponding service based on the description document. UDDI uses the SOAP message mechanism (standard XML/HTTP) to publish, edit, browse, and find registration information. It uses XML format to encapsulate different types of data and sends the data to the Registration Center or the Registration Center to return the required data.
1.2 XML Web Service Features
The main goal of Web Service is cross-platform interoperability. To achieve this goal, Web services are fully based on standards independent of the platform and software vendors, such as XML (Extensible Markup Language) and XSD (XML Schema, is a new platform for creating interoperable and distributed applications. Therefore, using Web Service has many advantages:
1. Cross-firewall communication
If the application has thousands of users distributed all over the world, communication between the client and the server will be a tough problem. Because there is usually a firewall or proxy server between the client and the server. The traditional approach is to use a browser as the client, write a lot of ASP pages, and expose the middle layer of the application to the end user. The result is that development is difficult and programs are difficult to maintain. If the client code is no longer so dependent on HTML forms, the client programming will be much simpler. If the intermediate layer component is changed to Web service, you can directly call the intermediate layer component from the user interface, thus saving the step for creating an ASP page. To call a web service, you can directly use a soap client such as Microsoft soap toolkit or. NET, or use a self-developed soap client to connect it to the application. This not only shortens the development cycle, but also reduces Code complexity and enhances the maintainability of applications. At the same time, the application does not need to jump to the corresponding "result page" every time it calls the middle layer components ".
2. Application Integration
Enterprise-level application developers know that enterprises often need to integrate various programs written in different languages and run on different platforms, this integration will take a lot of development effort. Applications often need to obtain data from programs on a running host, or send data to the host or other platform applications. Even on the same platform, various software products produced by different software vendors often need to be integrated. Through web service, applications can use standard methods to "expose" functions and data for other applications.
XML Web Services provides the ability to exchange messages using standard protocols (HTTP, XML, SOAP, and WSDL) in a loosely coupled environment. Messages can be structured, typed, or loosely defined.
3. B2B Integration
B2B refers to business, as in businesses doing business with other businesses, the e-commerce of merchants (generally referred to as enterprises, that is, products, services, and information exchange between enterprises over the Internet. The popular saying means that both the supply and demand sides of e-commerce transactions are merchants (or enterprises or companies). They use Internet technology or various business network platforms to complete the business transaction process.
Web Service is the key to successful B2B integration. With Web service, the company only needs to expose key business applications to specified suppliers and customers, and the Web service runs on the Internet, it can be easily implemented anywhere in the world, and its operation cost is relatively low. Web Service is only a key part of B2B integration, and many other parts are required for integration. The biggest benefit of implementing B2B integration with Web services is that it can easily implement interoperability. As long as the business logic is exposed and becomes a web service, any designated partner can call these business logic, regardless of the platform on which their system runs, the development language used. This greatly reduces the time and cost spent on B2B integration.
4. software and data Reuse
Web service allows code reuse while reusing the data behind the code. When using Web Services, you no longer have to purchase and install software components from a third party, as before, and then call these components from the application; you only need to directly call the remote web service. Another case of software reuse is to integrate the functions of several applications and expose them through Web Services, you can easily integrate all these functions into your portal to provide users with a unified and friendly interface. You can use the functions provided by third-party web services in applications, or provide your own application functions to others through Web Services. In both cases, you can reuse the data behind the code and code.
From the above discussion, we can see that Web services are most useful when performing interoperability or remote calls through the Web. However, in some cases, Web Services cannot bring any benefits at all. Web services have the following Disadvantages:
1. standalone applications
Currently, enterprises and individuals still use many desktop applications. Some of them only need to communicate with other programs on the local machine. In this case, it is best not to use web services, as long as you use local APIs. Com is very suitable for working in this case because it is small and fast. The same is true for the software running on the same server. Of course, Web services can also be used in these scenarios, but that will not only consume too much, but will not bring any benefits.
2. Lan applications
In many applications, all programs use Com on the Windows platform and run on the same LAN. In these programs, using DCOM is much more effective than soap/HTTP. Similarly, if A. Net Program is to connect to another. Net program on the LAN, it should use. Net remoting. In fact, in. Net remoting, you can also specify the use of SOAP/HTTP for Web service calls. However, it is better to call RPC directly through TCP, which will be much more effective.
1.3 XML Web Service applications
1. The original XML Web Service is usually a source of information that can be easily incorporated into applications, such as stock prices, weather forecasts, and sports scores.
2. Provide existing applications in XML Web Service Mode, build new and more powerful applications, and use XML Web Service as the construction block.
For example, a user can develop a purchase application to automatically obtain price information from different suppliers, so that the user can select a supplier, submit an order, and then track the transportation of the goods until the goods are received. In addition to providing services on the web, supplier applications can also use XML Web Service to check the customer's credit, collect the payment, and handle the freight formalities with the freight company.
2. Web Service Development
The. NET platform has built-in support for Web Services, including the construction and use of web services. Unlike other development platforms, you can use the. NET platform to develop Web services without other tools or sdks .. Net Framework itself fully supports Web services, including server-side request processors and support for sending and receiving soap messages to clients. Next, we will use Microsoft Visual Studio. NET 20058 (vs. net 2008) to create and use a simple web service.
2.1 create a simple Web Service
First, open vs2005, open "file-New-Website", and select "ASP. NET web service"
Check the service. CS code and you will find that vs. NET 2005 has already set up a default framework for the Web service file. The original code is:
View plaincopy to clipboardprint?
Using system;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. scriptservice]
Public class service: system. Web. Services. WebService
{
Public Service (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod]
Public String helloworld ()
{
Return "Hello World ";
}
}
Using system;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. scriptservice]
Public class service: system. Web. Services. WebService
{
Public Service (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod]
Public String helloworld ()
{
Return "Hello World ";
}
}
There is already a hello World method in the default project. Run it directly to see the effect,
Click the "helloworld" hyperlink on the displayed page to jump to the next page:
Click the "call" button to view the Web service results returned in XML format. It indicates that our web service environment is correct, and we have initially come into contact with the simplest web service.
2.2 create a simple and functional Web Service
We have learned about WebService at a macro level. In fact, WebService is an external interface, which contains functions that can be called by external customers (Note: there are also functions that cannot be called by customers ). if we are a server, we have written a WebService and then gave it to the customer (and we also gave them the call rules ), the customer can be in a relatively transparent State when obtaining information from the server. that is, the customer does not understand (or need to) the process, and they only obtain data. in the code file, if we want this function to become an externally callable interface function after writing a function, we must add a line of code [webmethod (description = "function description")] to the function. If your function does not have this Declaration, it cannot be referenced by users. next, let's write a simple web service example.
First, comment out the default helloworld method, and simply write the four methods for finding addition, subtraction, multiplication, division, and so on;
View plaincopy to clipboardprint?
Using system;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. scriptservice]
Public class service: system. Web. Services. WebService
{
Public Service (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
// [Webmethod]
// Public String helloworld ()
//{
// Return "Hello World ";
//}
[Webmethod (description = "summation method")]
Public double addition (double I, Double J)
{
Return I + J;
}
[Webmethod (description = "difference calculation method")]
Public double subtract (double I, Double J)
{
Return I-j;
}
[Webmethod (description = "Method for product calculation")]
Public double multiplication (double I, Double J)
{
Return I * J;
}
[Webmethod (description = "method of quotient calculation")]
Public double division (double I, Double J)
{
If (J! = 0)
Return I/J;
Else
Return 0;
}
}
Using system;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. scriptservice]
Public class service: system. Web. Services. WebService
{
Public Service (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
// [Webmethod]
// Public String helloworld ()
//{
// Return "Hello World ";
//}
[Webmethod (description = "summation method")]
Public double addition (double I, Double J)
{
Return I + J;
}
[Webmethod (description = "difference calculation method")]
Public double subtract (double I, Double J)
{
Return I-j;
}
[Webmethod (description = "Method for product calculation")]
Public double multiplication (double I, Double J)
{
Return I * J;
}
[Webmethod (description = "method of quotient calculation")]
Public double division (double I, Double J)
{
If (J! = 0)
Return I/J;
Else
Return 0;
}
}
Run the command to view the methods we have written that can be called, such:
Similarly, click the addition method to go to the addition method call page.
Enter the parameters I = 3 and J = 3. For example, click call to view the Web Service result returned in XML format (the result of adding I and j)
Here, we will find that WebService is not so mysterious. It is just an interface. For us, the focus is on writing interface functions.
2.3. use ASP. NET to call Web Service
First, open vs2005, open "file-New-Website", and select "ASP. NET Website ".
Select the storage location and click "OK" to go to the default page. Then add a web reference to bring the WebService to the current project. The method is as follows: Right-click the resource manager and choose add web reference to bring up the dialog box:
Fill in the URL. Enter the address displayed in the browser after the WebService is run (that is, the access address after the WebService is published), and click "go", as shown in, the method that can be called in the referenced WebService is displayed. Click "add reference" to reference WebService to the current project, as shown in, the introduced WebService file appears in the solution.
Here we will practice calling WebService's four methods. For a simple call example, add several controls on the front-end of the website. The Code is as follows:
View plaincopy to clipboardprint?
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "num1" runat = "server"> </ASP: textbox>
<Select id = "selectoper" runat = "server">
<Option> + </option>
<Option>-</option>
<Option> * </option>
<Option>/</option>
</SELECT>
<Asp: textbox id = "num2" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "=" onclick = "button#click"/>
<Asp: textbox id = "result" runat = "server"> </ASP: textbox>
</Div>
</Form>
</Body>
</Html>
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "num1" runat = "server"> </ASP: textbox>
<Select id = "selectoper" runat = "server">
<Option> + </option>
<Option>-</option>
<Option> * </option>
<Option>/</option>
</SELECT>
<Asp: textbox id = "num2" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "=" onclick = "button#click"/>
<Asp: textbox id = "result" runat = "server"> </ASP: textbox>
</Div>
</Form>
</Body>
</Html>
Then write the calling code in the background. before calling the code, just like using other objects, instantiate the code first. The instantiation method is localhost. service A = new localhost. service (); then you can use a to access the methods provided in WebService. In this example, a button control is dynamically created to trigger WebService calls. The background code is as follows:
After running, you can see the effect, as shown in. Enter two operands in the first two Textbox, select the operator from the drop-down list in the middle, and click "=, output the calculated result to the third textbox.
However, the entire computation is not performed locally. The computation is performed on the Web server and the results are returned to the caller through XML. Therefore, when running the program, the WebService program must also be started; otherwise, an exception will be reported that the remote server cannot be connected, for example:
The background code is as follows:
View plaincopy to clipboardprint?
Using system;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
String selectflag = selectoper. value;
Localhost. Service web = new localhost. Service ();
If (selectflag. Equals ("+ "))
{
Result. Text = (Web. addition (double. parse (num1.text), double. parse (num2.text). tostring ();
}
Else if (selectflag. Equals ("-"))
{
Result. Text = (Web. Subtract (double. parse (num1.text), double. parse (num2.text). tostring ();
}
Else if (selectflag. Equals ("*"))
{
Result. Text = (Web. Multiplication (double. parse (num1.text), double. parse (num2.text). tostring ();
}
Else if (selectflag. Equals ("/"))
{
Result. Text = (Web. Division (double. parse (num1.text), double. parse (num2.text). tostring ();
}
}
}
Using system;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
String selectflag = selectoper. value;
Localhost. Service web = new localhost. Service ();
If (selectflag. Equals ("+ "))
{
Result. Text = (Web. addition (double. parse (num1.text), double. parse (num2.text). tostring ();
}
Else if (selectflag. Equals ("-"))
{
Result. Text = (Web. Subtract (double. parse (num1.text), double. parse (num2.text). tostring ();
}
Else if (selectflag. Equals ("*"))
{
Result. Text = (Web. Multiplication (double. parse (num1.text), double. parse (num2.text). tostring ();
}
Else if (selectflag. Equals ("/"))
{
Result. Text = (Web. Division (double. parse (num1.text), double. parse (num2.text). tostring ();
}
}
}
By now, the development and calling of a simple WebService have been completed. In actual applications, you can write powerful and complex WebServices according to your own needs. No matter how complicated, the whole process is like this.