Tutorials to .com

Tutorials to .com » Dotnet » Webservice » Use C # to create a. Net of Web Service

Use C # to create a. Net of Web Service

Print View , by: iSee ,Total views: 13 ,Word Count: 2935 ,Date: Sun, 24 May 2009 Time: 2:08 PM

Microsoft in its. Net strategy, its main push of the Web Service has done much to promote the. Now, Web Service development is in full swing, and related to the emerging new technologies. Web Service development is the age of the Internet to build a better tomorrow. In this article, I will introduce a number of Web Service basic knowledge of how to use c # to build a Web Service. Through the article, we will of WSDL, UDDI and the future of Web Service have a general understanding.

Why do we need Web Service?
Previously, the logic of distributed applications need to use the distributed object model, through the use of DCOM, CORBA, RMI, such as the basic structure, developers still have to use the model provided by the local rich resources and accuracy, and service placed in the remote system.
When China and Italy have been the middleware platform (RMI, Jini, CORBA, DCOM, etc.), when, why do we have to worry about it for the Web? Middleware does provide a powerful means of services, but these systems have a common flaw, that is, they can not be extended to the Internet: they require service client and service provided by the system itself must be carried out between the close-coupled, which would require the a similar basic structure. However, such systems are often very fragile: if one end of changes in the implementation of the mechanism, then the other side will collapse. For example, if the server application's interface changes, then the client will collapse. In order to expand the use of the Internet, we need a basic structure of loose coupling to solve the problem. Circumstances so ushered in the birth of the Web Service.

What is a Web Service?
Web Service is a new branch of Web applications, they are self-contained, self-describing, modular applications, can be published, positioning, through the Web calling. Web Service can run from simple to complex business deal with the request of any function. Once deployed, the other Web Service applications can be found and call it the deployment of services.
Web Service is an application that the use of the Web network technology and the essence of component-based development component. Can use the standard Internet protocol, such as Hypertext Transfer Protocol (HTTP) and XML, will be reflected in the programmatic functions of the Internet and corporate Intranet. Such as DCOM, RMI, IIOP, such as component-based object model has been popular for some time longer. However, these models are dependent on a specific object model agreement. Web Service to expand the model to enable it and the simple object access protocol (Simple Object Access Protocol, SOAP) and xml communication to a specific object model to eradicate obstacles to bring about agreement. WebService can be regarded as the components of Web programming. (See Figure 1)
Web Service is basically the use of Hypertext Transfer Protocol (HTTP) and SOAP to make business data can be obtained online. It will target commercial (COM objects, java Beans, etc.) exposed to the HTTP on the implementation of the SOAP call and the remote function calls. Thus, Web Service users can be adopted in the remote object in the SOAP and HTTP method invocation on the Web.


Figure 1

SOAP is a kind of call can cause the location of B in the Web Service component of the calling procedure. After the implementation of the outcome of the procedure on the form of xml documents returned to the position A on the user.
In Figure 1, in the location of the user A to know how the location of B in some cases the user of this? This should be related to a common standard. Services Description Language (Service Description Language, SDL), SOAP Contract Language (SOAP Contract Language, SCL) and network accessibility Language Specification (Network Accessible Specification Language, NASSL) are established for this purpose the XML-type language. However, IBM and Microsoft recently agreed to Web Services Description Language (Web Service Description Language, WSDL) as a Web Service standards.
Web Service component of the structure is through the Web Services Description Language to the show. WSDL1.1 is a Web Service used to describe the attributes of the xml document as well as the interface. Now has a new specifications, the specifications can be obtained http://msdn.microsoft.com/xml/general/wsdl.asp.

Society is facing the task of Web Service is the best way to make a do-it-yourself examples. We are all familiar with the stock quote service, Nasdaq, Dow Jones is very well-known example. They all provide a code and enter the company's stock price to obtain the latest interface. In this paper, we try to design the same function.

Tools to create Web Service In this paper, we MS.Net framework SDK to achieve this process.
Comparison of Web Service to create a good integrated development environment (IDE) is a Visual Studio.Net. However, you can easily use any text editor (Notepad, WordPad, Visual Studio 6.0) to create a Web Service file.
In addition, you must be familiar with the following concepts:
.net platform, the basic knowledge
Basic knowledge of C++ # object-oriented basic knowledge of the concept of

Create a Web Service
Next, we will use C # to establish a "SecurityWebService" the Web Service. A Web Service file containing the form. Asmx extension. (Asp.net just called the file extension. Aspx)

<% @ WebService Language = "C #" class = "SecurityWebService"%>
This statement will tell the compiler will run in the Web Service model, as well as the name of C # class. At the same time, we would like to visit the Web Service's name space. Also, the best System to add a reference to the name space.
using System;
using System.Web.Services;
The SecurityWebService class should inherit the functions of Web Service category. Therefore, we add the following lines of code:
public class SecurityWebService: WebService
Now we are to use our skills in preparation for a C # Class. C # classes and C + +, java class is like, if you have C + + and Java basis, this is a piece of cake.
. Net, under the Web Service to set some basic data types. Therefore, if we return to "int", "float" or "string" data type, etc. so, it can automatically transform them into standard XML output. Unfortunately, however in most cases, we need an entity with a class of data sets. Let me first give you an example. Stock quotes SecurityWebService our services require users to input a company's code, and then it will give the company's full name and the current stock price. Because of this, we need three pieces of information a company:
Company code (data type: string)
Company Name (data type: string)
Stock price (data type: double)
We need a single stock quotes disaggregated data. There are many ways to accomplish this work, we here use the best data type enumeration. We are in C # using the "structs", and C + + in the same structs. Code is as follows:
public struct SecurityInfo
(
public string Code;
public string CompanyName;
public double Price;
)

We have now completed all requirements for the establishment of Web Service module of the. Therefore, all the code is as follows:

<% @ WebService Language = "C #" class = "SecurityWebService"%>

using System;
using System.Web.Services;

public struct SecurityInfo
(
public string Code;
public string CompanyName;
public double Price;
)

public class SecurityWebService: WebService
(
private SecurityInfo Security;

public SecurityWebService ()
(
Security.Code = "";
Security.CompanyName = "";
Security.Price = 0;
)

private void AssignValues (string Code)
(
/ / Here the use of commercial components
/ / Method call is used to obtain the necessary data
/ / This process to the appropriate code, I added a string corresponding to facilitate the display
/ / At the same time, I used the random number generator to generate the stock price

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 method call will get the company name and the price for a given security code.", EnableSession = false)]
public SecurityInfo GetSecurityInfo (string Code)
(
AssignValues (Code);
SecurityInfo SecurityDetails = new SecurityInfo ();
SecurityDetails.Code = Security.Code;
SecurityDetails.CompanyName = Security.CompanyName;
SecurityDetails.Price = Security.Price;
return SecurityDetails;
)
)

Keep in mind that this Web Service can use Http to do anything. We may be in the code related to some very sensitive business data, but do not want it falling into the hands of others. That solution is to protect some of the logic function, so that users can only access to some of the functions used to display data. To this end, we use the keyword "[Web Method]". The following is sample code:
[WebMethod (Description = "This ......", EnableSession = false)]
public SecurityInfo GetSecurityInfo (string Code)
The function of the access type is the type of public. Label "Description" is used to describe the Web Service functionality. Because we do not have to store any session data, session state so we will be set to false.
private void AssignValues (string Code)
This is a protection should be a function of logic. Because we do not want our business confidential data in the Web was easy to obtain, therefore, we have access to the type of function is set to private (Note: Here, you will function even if the access type is set to public, or this function can not be public access to, because the keyword "[Web Method]" was not used).
This, we can use GetSecurityInfo (string) function to get the latest stock prices. At the same time, for convenience, I add the code to the company the name of the corresponding company. In addition, stock prices are randomly generated.
Finally, we save the file in a directory controlled by the IIS, the file name is "SampleService.asmx". After running the icons are as follows:


Figure 2

The above is a. Net Framework-generated Web page, we do not create the page (it is automatically generated by the system, so I do not need to write any code to create the page). This feature allows us to reduce the workload of a number of relatively. Similarly, you can also use the Asp.net page Pagelets functions or modify a document so that page shows different ways to its contents. Http://www.ibuyspy.com/store/InstantOrder.asmx you can get a good example.

How to use this Web Service?
Now we come to the use of the Web Service. We enter some values to get the stock price example.


Figure 3

Press the Invoke button, we get the following XML document:


Figure 4

In this way, the Web Service gives users the information they need. Because it is XML format, we need to write the client to extract the XML document. Client for the following categories:
1. A Web page
2. Under a console or Windows application procedures
3. WML language used to describe a cell phone procedures
4. A use of the Palm in the PDA or Win CE program you can use Http Get method to call the Web Service. So will not appear on the first page and does not require users to click the Invoke button. Specific ways:
http://server/webServiceName.asmx/functionName?parameter=parameterValue
Call our Web Service method is:
http://localhost/work/aspx/SampleService.asmx/GetSecurityInfo?Code=IBM

Stop here, we already know how to use C # to create and use a Web Service, but the task is not completed. We need to know how to find in the Internet on our Web Service, our Web Service can also be a big revenue in search engine. To solve this problem, we need to establish a "discovery" documents.

During his visit to the creation of any documents are found an existing Web Service, you must first find and integrate the Web Service, this process is the process of Web Service discovery. Through this discovery process, you know this Web Service can provide you with what kind of services and how do you achieve and its interaction. Found is a document. DISCO for the extension of the XML document. In practical application, you do not have to create a Web Service discovery documents. The following is an example of documents found:
<? xml version = "1.0"?>
<disco:discovery xmlns:disco="http://schemas.xmlsoap.org/disco/">
<scl:contractRef ref="http://localhost/work/aspx/SampleService.asmx?SDL"/>
</ disco: discovery>
We first called the document "SampleService.disco", and save it in the directory of the Web Service. If we are in the "/ work / aspx" directory create a Web Service, we will be able to employ a more flexible "dynamic discovery" of the. "Dynamic discovery" can be detected automatically for us "/ work / aspx" directory and all subdirectories *. DISCO document, so that we put much effort in the province.
<? xml version = "1.0"?>
<dynamicDiscovery xmlns="urn:schemas-dynamicdiscovery:disco.2000-03-17">
</ dynamicDiscovery>
You can use http://services3.xmethods.net/dotnet/default.disco was the discovery of a document. Found by analyzing the document, we can find the Web Service. However, to be found in the document you need to know the exact file that was found in URL. Otherwise you can not find the discovery of documents you want to, then of course you can not find what you want the Web Service. In this way, we now use a new technology - and GM found that the description and integration (Universal Description, Discovery, and Integration, UDDI) to the Web Service has been in existence for the publicity. UDDI is an open, Internet-based. The technology is still in the initial stage, it is constantly developing. You can obtain http://uddi.microsoft.com/ reference UDDI.

Release the Web Service
Published Web Service is very simple. Asp.net procedures and more or less the same as long as you will. Asmx files and. Disco paper copy to the appropriate directory, so if everything goes normal, the Web Service will be able to work.

Web Service's future prospects
Web Service technology, the future is very bright. Web Service technology in promoting the development of the road ahead, not only into a great deal of Microsoft's investment, Sun, IBM and others also expressed great interest. At the same time, the Internet also for the Apache and Java Web Development SOAP Toolkit. However, Web Service starting soon, but also requires a lot of work to do. Particularly in the domestic, Web Service technology startthan abroad, so time to step up and meet the challenge.


.Net WebService Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.