Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Web| the creation of Microsoft in its. NET strategy, the Web service its main push was trumpeted. Now, the WEB service is developing in full swing, the relevant new technologies are endless. The development of WEB service is building a better tomorrow in the Internet era. In this article, I'll introduce you to some of the basics of Web service and how to build a Web service in C #. Through the article, we will also have a general understanding of WSDL, UDDI, and future Web service.
Why do I need a Web Service?
Previously, distributed application logic required the use of distributed object models, and by using basic structures such as DCOM, CORBA, and RMI, developers could still have the rich resources and accuracy to use the local model, and could place services on remote systems.
Why do we have to worry about the web when there are already desirable middleware platforms (RMI, Jini, CORBA, DCOM, and so on)? Middleware does provide a powerful service implementation, but there is a common flaw in these systems, which is that they cannot be extended to the Internet: they require a tight coupling between the service client and the system-supplied service itself, requiring a homogeneous infrastructure. However, such systems are often very fragile: if one end of the execution mechanism changes, then the other end will collapse. For example, if the interface of the server application changes, the client crashes. In order to expand to the Internet, we need a loosely coupled infrastructure to solve this problem. In this case, the birth of the Web service is ushered in.
What is a Web Service?
Web Service is a new branch of Web application that is self-contained, self-describing, modular, and can be published, positioned, and invoked via the Web. Web service can perform any function from a simple request to a complex business process. Once deployed, other Web service applications can discover and invoke the services that it deploys.
Web Service is an application that utilizes Web networking technology and the essence of component-based development. The standard Internet protocols, such as Hypertext Transfer Protocol (HTTP) and XML, can be used to programmatic function in the Internet and intranet. Component-based object models such as DCOM, RMI, and IIOP have been in vogue for a long time. However, these models are dependent on a particular object Model protocol. WEB Service expands these models to make it possible to eliminate the barriers to a particular object model protocol with Simple Object Access Protocol (easy access Protocol,soap) and XML communication. WebService can be treated as a component on the web. (See Figure 1)
WEB service basically uses Hypertext Transfer Protocol (HTTP) and soap to make business data available online. It exposes business objects (COM objects, Java beans, and so on) to soap calls on HTTP and performs remote function calls. Therefore, consumers of Web service can make method calls on the web on the remote object via SOAP and HTTP.
A SOAP invocation is a class of calls that can cause a Web service component program to execute on location B. The results of the program execution are then returned to the user on location A in the form of an XML document.
In Figure 1, how does a user in position a know something about a user in location B? This is going to involve a common standard. Services Description Language (service Description Language, SDL), SOAP contract language (soap Contract LANGUAGE,SCL) and Network Accessibility specification language (network Accessible Specification Language,nassl) is an XML class language built for this purpose. However, IBM and Microsoft have recently agreed to use the Web Services Description Language (Web Service Description language,wsdl) as a standard for web servicing.
The structure of Web service components is exposed through the Web Services Description language. WSDL1.1 is an XML document that describes the properties and interfaces of a Web service. A new specification is available, which can be obtained on http://msdn.microsoft.com/xml/general/wsdl.asp.
The task facing
The best way to learn Web service is to do a hands-on example. We are all familiar with stock quote services, both Nasdaq and Dow Jones are well-known examples. They all provide an interface to enter the company code and get the latest stock price. In this article, we try to design the same function.
Tools for creating Web service
In this article we implement this program through the Ms.net Framework SDK.
The better Integrated development environment (IDE) for creating Web service is Visual Studio.NET. However, you can also easily create a Web service file with any text editor (Notepad, WordPad, Visual Studio 6.0).
Also, you must be familiar with the following concepts:
NET Platform Basics
Basic knowledge of C #
Basic knowledge of object-oriented concepts
Create a Web Service
Below, we will build a web Service called "Securitywebservice" in C #. A Web service file will contain an extension with the form. asmx. (Just like ASP.net's. aspx file name extension)
<%@ WebService language= "C #" class= "Securitywebservice"%>
This statement tells the compiler that the program will run in Web service mode and the name of the C # class. Also, we want to access the name space of the Web service. Also, it is a good idea to add a reference to the System namespace.
The Securitywebservice class should inherit the functionality of the Web service class. So, we added the following line of code:
public class Securitywebservice:webservice
Now we're going to use our object-oriented techniques to write a C # class. C # classes and C + +, Java classes are very similar, if you have C + + and Java Foundation, this is a piece of cake.
. NET Web service can set some basic data types. Therefore, if we return data types such as "int", "float" or "string", it can automatically convert them to standard XML output. Unfortunately, in most cases, we need a class of data sets of the same entity. Let me give you an example. Our Securitywebservice stock Quote service requires the user to enter a company's code name, and then it gives the company's full names and current stock prices. From this, we need a company of three information:
Company code (data type: string)
Company full Name (data type: string)
Stock price (data type: Double)
We need to break down the data information for a single stock quote. There are a number of ways to do this, and we use the best enumerated data types here. We used "structs" in C #, just like the structs in C + +. The code is as follows:
public struct SECURITYINFO
public string Code;
public string CompanyName;
public double price;
Now we have completed all the modules needed to build the Web service. So, all the code is as follows:
private void Assignvalues (string Code)
Use the business component here
The method call is used to obtain the desired data.
In this program I add a corresponding string to the corresponding code to facilitate the display
At the same time, I used a random number generator to generate stock prices
Security.code = Code;
Security.companyname = Code + "Pty Ltd.";
Random randomnumber = new System.Random ();
Security.price = Double. Parse (New System.Random (Randomnumber.next (1,10)). Nextdouble (). ToString ("##.##"));
[WebMethod (description= "This") call'll get the company name and the price for a given security code. ", EnableSession =false)]
Public Securityinfo GetSecurityInfo (string Code)
Securityinfo securitydetails = new Securityinfo ();
Securitydetails.code = Security.code;
Securitydetails.companyname = Security.companyname;
Securitydetails.price = Security.price;
Keep in mind that this Web service can do anything with HTTP. We may have some very sensitive business data involved in the code, but we don't want it to fall into the hands of others. The solution is to protect some logical functions so that users can access only a few functions to display the data. To achieve this, we used the keyword [Web method]. Here is the sample code:
[WebMethod (description= "This ...", Enablesession=false)]
Public Securityinfo GetSecurityInfo (string Code)
The access type of this function is public. The label "Description" is used to describe the functionality of this Web service. Because we don't have to store any session data, we set the session state to false.
private void Assignvalues (string Code)
This is a function that should be logically protected. Because we don't want our commercially confidential data to be easily accessible on the WEB, so we set the access type of the function to private (note: Here, even if you set the access type of the function to public, the function cannot be accessed publicly, because the keyword [Web method] "Not to be used."
Here, we can use the GetSecurityInfo (string) function to get the latest stock price. Also, for convenience, I added the name of the company to the company code. Also, the price of a stock is randomly generated.
Finally, we save the file in a directory controlled by IIS with the file named "Sampleservice.asmx". The following diagram is running:
The above is a Web page generated by the. Net framework that we did not create (it was generated automatically by the system, so I didn't need to write any code to create the page). This function has reduced our workload relatively much. Similarly, you can use ASP.net's pagelets function or modify the page file to make the page display its contents in different ways. You can get a good example in Http://www.ibuyspy.com/store/InstantOrder.asmx.
How do I use this Web service?
Now we're going to use this Web service. Let's first enter some values to get the stock sample price.
By pressing the Invoke button, we can obtain the following XML document:
In this way, the Web service provides the user with the information it needs. Because it is an XML-formatted document, we need to write the client to extract the XML document. Clients can be of the following categories:
1. A Web page
2. A console or Windows application program.
3. A mobile program described in WML language
4. A palm or win CE program used on a PDA
You can invoke this web Service directly using the HTTP GET method. In this way, the first page will not appear, and the user is not required to click the Invoke button. Specific methods:
The way to invoke our Web service is to:
So far, we already know how to create and use a Web service in C #, but the task is not fully completed. We need to know how to find our Web service on the Internet and whether our web service can also be paid for in a big search engine. To solve this problem, we need to create a "discovery" file.
Create a discovery File
Before you can access an existing Web service, you must first find and integrate the Web service, a process that is the discovery of Web service. Through this discovery process, you will know what service the Web service can provide for you and how you interact with it. The discovery file is a. Disco is an XML file with a name extension. In practice, you do not have to create discovery files for each Web service. Here is an example of a discovery file:
<?xml version= "1.0"?>
<disco:discovery xmlns:disco= "http://schemas.xmlsoap.org/disco/" >
<scl:contractref ref= "Http://localhost/work/aspx/SampleService.asmx?SDL"/>
Let's first name this file "Sampleservice.disco" and save it in the directory of the Web service. If we were to create a Web service under the "/work/aspx" directory, we could use more flexible "dynamic discovery". "Dynamic discovery" automatically detects the "/work/aspx" directory and all the * in subdirectories. Disco file, so it saves us a lot of kung Fu.
<?xml version= "1.0"?>
<dynamicdiscovery xmlns= "URN:SCHEMAS-DYNAMICDISCOVERY:DISCO.2000-03-17" >
You can get a copy of the discovery file that you can use at Http://services3.xmethods.net/dotnet/default.disco. By analyzing the discovery files, we can find the Web Service we need. However, you must know the exact URL of the discovery file before you can get the discovery file. If you can't find the file you want, then of course you can't find the Web service you want. In this way, we are now using a new technology-universal discovery, description and integration (Universal Description,discovery,and Integration,uddi) to advertise existing Web service. UDDI is public, internet-based. The technology is still in its infancy, so it is evolving. You can get a reference to UDDI in http://uddi.microsoft.com/.
Publish this web Service
It is easy to publish a Web service. As with the ASP.net program, you just copy the. asmx file and the. disco file to the appropriate directory, so that if everything works, the Web service will work.
Looking to the future of Web service
The future of WEB service technology is quite bright. In promoting the development of Web service technology, not only Microsoft injected a lot of investment, Sun, IBM, etc. also expressed great interest. There is also a SOAP toolkit for Apache and Java Web Development on the Web. However, a lot of work needs to be done before the Web service starts. Especially in the domestic, the WEB service technology started a step later than abroad, so more to seize the time to meet the challenge.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.