1. Introduction to Web Service and webservice
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 "expose" functions and data in a standard way for other applications to use.
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. 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.
II,Web ServiceDevelopment
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 2005 (VS. Net 2005) 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:
1 using System;
2 using System. Web;
3 using System. Web. Services;
4 using System. Web. Services. Protocols
5 [WebService (Namespace = "http://tempuri.org/")]
6 [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
7 public class Service: System. Web. Services. WebService
8 {
9 public Service ()
10 // If the designed component is used, uncomment the following line
11 // InitializeComponent ();
12}
13 [WebMethod]
14 public string HelloWorld (){
15 return "Hello World ";
16}
17}
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;
1 using System;
2 using System. Web;
3 using System. Web. Services;
4 using System. Web. Services. Protocols;
5
6 [WebService (Namespace = "http://tempuri.org/")]
7 [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
8 public class Service: System. Web. Services. WebService
9 {
10 public Service (){
11 // if you use the designed component, uncomment the following line
12 // InitializeComponent ();
13}
14 // [WebMethod]
15 // public string HelloWorld (){
16 // return "Hello World ";
17 //}
18 [WebMethod (Description = "summation method")]
19 public double addition (double I, double j)
20 {
21 return I + j;
22}
23 [WebMethod (Description = "difference calculation method")]
24 public double subtract (double I, double j)
25 {
26 return I-j;
27}
28 [WebMethod (Description = "Method for product calculation")]
29 public double multiplication (double I, double j)
30 {
31 return I * j;
32}
33 [WebMethod (Description = "method of quotient")]
34 public double division (double I, double j)
35 {
36 if (j! = 0)
37 return I/j;
38 else
39 return 0;
40}
41}
42
Run the command to view the methods we have written that can be called, such:
Also clickAdditionMethod,EnterAdditionMethod call page.
Enter parameters above the parametersI = 3, j = 3,For example,Click to call,The Web Service result returned in XML format (the result of adding I and j) is displayed)
Here,We will find that,ActuallyWebserviceNot that 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:
Enter in the URL, the address displayed in the browser after the WebService is run, and click "go". For example, the method that can be called in the referenced WebService is displayed, click "add reference" to reference the webservice to the current project. For example, 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:
1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
2
3 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 5 6 <title> Webservice call instance </title>
7 8 <body>
9 <form id = "form1" runat = "server">
10 <div>
11 <asp: TextBox ID = "Num1" runat = "server"> </asp: TextBox>
12 <select id = "selectOper" runat = "server">
13 <option> + </option>
14 <option>-</option>
15 <option> * </option>
16 <option>/</option>
17 </select>
18 <asp: TextBox ID = "Num2" runat = "server"> </asp: TextBox>
19 <span id = E runat = "server"> </span>
20 <asp: TextBox ID = "Result" runat = "server"> </asp: TextBox>
21 </div>
22 </form>
23 </body>
24 25
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. servicea = 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:
1 using System;
2 using System. Data;
3 using System. Configuration;
4 using System. Web;
5 using System. Web. Security;
6 using System. Web. UI;
7 using System. Web. UI. WebControls;
8 using System. Web. UI. WebControls. WebParts;
9 using System. Web. UI. HtmlControls;
10 public partial class _ Default: System. Web. UI. Page
11 {
12 protected void Page_Load (object sender, EventArgs e)
13 {
14 // dynamically create a button during page loading and call Webservice in its event
15 Button btn = new Button ();
16 btn. Width = 20;
17 btn. Text = "= ";
18 btn. Click + = new EventHandler (btn_Click );
19 E. Controls. Add (btn );
20}
21 /// <summary>
22 // defines the Click event for dynamically creating a Button, in which Webservice is called
23 /// </summary>
24 /// <param name = "sender"> </param>
25 /// <param name = "e"> </param>
26 void btn_Click (object sender, EventArgs e)
27 {
28 if (Num1.Text! = "" & Num2.Text! = "")
29 {
30 // instantiate the referenced webservice object
31 localhost. Service WebserviceInstance = new localhost. Service ();
32 int counters = selectOper. SelectedIndex;
33 switch (switches)
34 {
35 // call the webservice exposure method through the instantiated Webservice object
36 case 0:
37 Result. Text = WebserviceInstance. addition (double. Parse (Num1.Text), double. Parse (Num2.Text). ToString ();
38 break;
39 case 1:
40 Result. Text = WebserviceInstance. subtract (double. Parse (Num1.Text), double. Parse (Num2.Text). ToString ();
41 break;
42 case 2:
43 Result. Text = WebserviceInstance. multiplication (double. Parse (Num1.Text), double. Parse (Num2.Text). ToString ();
44 break;
45 case 3:
46 Result. Text = WebserviceInstance. division (double. Parse (Num1.Text), double. Parse (Num2.Text). ToString ();
47 break;
48}
49}
50}
51}
52
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:
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.