How to Create. NET Web Service

Source: Internet
Author: User
Why Web Service

After purchasing a product through the internet, you may be confused about the delivery method. It is often because it takes a lot of time to find a distribution company due to delivery problems. This is not a value-added service for distribution companies.

To solve this problem, the distribution company needs to learn more about delivery information without reducing the security level. However, the security system designed by the security company is very complicated. So can we only use port 80 (Web Server Port) and only provide information through the Web server? Therefore, we have created a brand new web application. Program To obtain data from core commercial applications. Distribution companies will pay for something, and all companies want to focus on their core business applications.

What is Web service?

Web Service is a common model for building applications, and can communicate Operating System . Web service enables the best combination of component-based development and web. It is a component-based object model, such as Distributed Component Object Model (DCOM) and remote Method Invocation (RMI ), and Internet Inter-ORB Protocol (IIOP) have been released for a long time. Unfortunately, these models depend on the special object model protocol. Web Service uses soap and XML Communication To eliminate the obstacles of the special object model.

Web service mainly uses HTTP and soap protocols to enable business data on the Web Transmission , Saop uses HTTP Call Commercial objects are remotely called. Web users can use soap and HTTP to call remote objects through web calling methods.

So how can users in location a understand the meaning of location B's Web service? This question can be answered by a consistent standard. Service Description Language (SDL), Soap contract language (SCL), and network accessible Specification Language (nassl )) both are similar languages for this purpose. However, both IBM and Microsoft agree that Web Service Description Language (WSDL) is the standard language for Web Services.

The structure of web service components is described by Web Service Description Language. wsdl1.1 is an XML document describing the attributes and interfaces of Web Services. The new specification can be obtained at msdn.microsoft.com/xml/general/wsdl.asp.

Current task

The best way to learn is to create a web service. We take a stock quote system as an example. The NASDAQ and Australian Stock trading systems are very famous examples. They all provide an interface, enter the company code and accept the final stock price.

We copy a web service with the same functions.

The input parameter of our Web Service is the stock code. The Web Service obtains the stock price by calling the commercial logic function of the middle layer, and the commercial logic function keeps the smallest part on the web service.

Web service development tools

The Core Component for implementing this application will be Microsoft. net Framework SDK, but he is still a trial version. You can download it from the Microsoft Site. My configuration is Windows 2000 Server, piii300, and MB memory in the operating system.

The preferred integrated development environment (IDE) for creating web services is visual studio.net. However, you can use any text editor (wordpad, notepad, visual studio6.0) to easily create a web service file.
Create a Web Service

I will use C # to create a web service called securitywebservice. The extension of a Web service file is. asmx (just like Asp.net's extension. aspx). The first line of the file is:

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

This statement tells the compiler to run the Web service mode and the C # class name. We also need to access the Web Service namespace, which is also a good practice to reference the system namespace.

Using system;
Using system. Web. Services;

Securitywebservice should inherit the functions of the web service class, so we need to add the following line of code:

Public class securitywebservice: WebService

Now we use the object-oriented Programming TIPS: create a class, C # class and C ++ and Java It is very similar. Using C # To create a class is as simple as walking in a park without any skills.

C #'s basic data types are designed very smartly. Therefore, if we return "int," "float," or "string", they will be automatically converted into standard XML Output . Unfortunately, in most cases, we need to regard the acquired data set as a single entity ). Here is an example.

The securitywebservice stock quote system requires the user to enter the stock code and returns the complete company name and current stock price. Therefore, for a stock, we have three information blocks.

1. company code (string)

2. Company Name (string)

3. Price (double)

When we submit a stock, we need Extract There are several ways to do this for all three types of data. The best way is to bind them to an enumerated data type, we can use "struct" in C # to complete it. The "struct" in C # is similar to the structure in C ++.

Public struct securityinfo
{
Public String code;
Public String companyName;
Public double price;
}

We can use Module The code for creating a Web service 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)
{
// This is where you use your business components.
// Method callon business components are used to populate the data.
// For demonstration purposes, I will add a string to the code and
// Use a random number generator to create the price feed.

Security. Code = code;
Security. companyName = code + "Pty Ltd ";
Random randomnumber = new system. Random ();
Security. Price = double. parse (new system. Random (randomnumber. Next (). nextdouble (). Format ("##. #", null ));
}

[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;
}

}

Remember that all users can access the Web service over HTTP. Maybe you will talk about confidential commercial data in the Code and how to keep confidential data that you don't want others to know. The solution is to protect functional blocks of business logic and only allow access to the presentation layer. in C #, you can use the keyword "[web method]" to achieve this purpose. Let's look at the following code:

[Webmethod (description = "This...", enablesession = false)]
Public securityinfo getsecurityinfo (string code)

This function is displayed to the public. The description mark is used to describe the Web service function, because we cannot Storage Any session data will eliminate the session status.

Private void assignvalues (string code)

This commercial logic function is not known to the public, and we do not want sensitive commercial information to be published on the web. (Note: the public still cannot even see private information. Why ?, This is because the [web method] keyword is not used .)

We can use the business logic in this function to obtain the latest stock quotation. For this purpose, I added a text box in the code to enter the company name. The price is produced by a random function.

Save the file sampleservice. asmx in the IIS directory. I saved it in the virtual directory "/work/aspx". The similarities in Web browsers are as follows:

This web page is composed. net Framework, we did not create this page (this is automatically generated by the system, we did not write any line of code for it, this figure is a by-product of the previous Code ), the functions to be used are suitable for a single web service.

You can easily change the page using the Asp.net and config. web files. But pay attention to the link of the SDL specification (even if we use WSDL ,. the Net version still references SDL, which is expected to be corrected in the next version). This is a description file of the Web service to create a proxy object, this basically gives a general introduction to Web services. If you are familiar with this, you can simply look at the "web-only" method, the SDL specification does not describe all private functions and attributes. The SDL specification of the securitywebservice class is shown in routine.
How to Use Web Service

Now we can use this web service. Let's enter a value to get a fake price.

Click The invoke button will display a new Window And XML documents.

This shows how the Web Service publishes information. We need to design a client to display the XML document. The client should be:

1. A web page

2, Console Or Windows Applications Program

3. WML or WMLScript that can interact with mobile phones

4. Palm or Windows CE applications that can be used on PDA

I will explain the process of creating a client later.

You can use the http get method to directly call the web service. In this example, you will not use the preceding webpage or click the invoke button to obtain the XML document. We use the http get method directly. Call For XML documents, the syntax should be as follows:

Http: // server/webservicename. asmx/functionname? Parameter = parametervalue

In this example, the statement is:

Http: // localhost/work/aspx/sampleservice. asmx/getsecurityinfo? Code = IBM

This is the same as clicking the invoke button.

Now we know how to create and use a web service, but our work is only half done. How can the client discover web services? How can I search for a Web Service on the Internet? Is it through a search engine like Yahoo search engine? To solve these problems, we need to create a "discovery" file for the web service.

Create a "discovery" File

The Web Service is queried and Positioning The Web service description process is a preparation process for accessing the web service. The client obtains the existence, size, and interaction of the web service by discovering the Web service process, the "discovery" file is a file with the extension :. disco XML document. You do not have to create a "discovery" file for each web service. The following is an example of the "discovery" file:

<? XML version = "1.0"?>
<Dynamicdiscovery xmlns = "urn: Schemas-
Dynamicdiscovery: disco.2000-03-17 ">
</Dynamicdiscovery>

Configure Web Service

The Web service configuration is very simple. Similar to the Asp.net application file, you can copy the. asmx and. Disco files to the corresponding directory.

The future of Web Service

The future of Web Service is very bright. Now, not only is Microsoft developing Web Service technology, but IBM and Sun are also developing Web Services. Soap toolkits can be used on Apache and Java Web servers, however, I believe that we still need to do some work for the Web service, especially the Web service discovery process.

The Web Service will introduce some new ideas on the web. I believe it is pay-as-you-go. Just like pay-as-you-go TVs, we create web sites and charge users, just like pay-as-you-go TVs, users only need to pay a little fee, which is commercially feasible.
Attached instance

<? XML version = "1.0"?>
<Servicedescription xmlns: S0 = "http://tempuri.org/" name = "securitywebservice" targetnamespace = "http://tempuri.org /"
Xmlns = "urn: Schemas-xmlsoap-org: sdl.2000-01-25">
<Soap xmlns = "urn: Schemas-xmlsoap-org: soap-sdl-2000-01-25">
<Service>
<Addresses>
<Address uri = "http: // localhost/work/aspx/sampleservice. asmx"/>
</Addresses>
<Requestresponse name = "getsecurityinfo" soapaction = "http://tempuri.org/GetSecurityInfo">
<Request ref = "S0: getsecurityinfo"/>
<Response ref = "S0: getsecurityinforesult"/>
<Info> This method call will get the company name and the price for a given security code. </INFO>
</Requestresponse>
</Service>
</Soap>
<Httppost xmlns = "urn: Schemas-xmlsoap-org: post-sdl-2000-01-25">
<Service>
<Requestresponse name = "getsecurityinfo" href = "http: // localhost/work/aspx/sampleservice. asmx/getsecurityinfo">
<Request>
<Form>
<Input name = "code"/>
</Form>
</Request>
<Response>
<Mimexml ref = "S0: securityinfo"/>
</Response>
<Info> This method call will get the company name and the price for a given security code. </INFO>
</Requestresponse>
</Service>
</Httppost>
<Httpget xmlns = "urn: Schemas-xmlsoap-org: get-sdl-2000-01-25">
<Service>
<Requestresponse name = "getsecurityinfo" href = "http: // localhost/work/aspx/sampleservice. asmx/getsecurityinfo">
<Request>
<Param name = "code"/>
</Request>
<Response>
<Mimexml ref = "S0: securityinfo"/>
</Response>
<Info> This method call will get the company name and the price for a given security code. </INFO>
</Requestresponse>
</Service>
</Httpget>
<Schema targetnamespace = "http://tempuri.org/" attributeformdefault = "qualified"
Elementformdefault = "qualified" xmlns = "http://www.w3.org/1999/XmlSchema">
<Element name = "getsecurityinfo">
<Complextype>
<All>
<Element name = "code" xmlns: Q1 = "http://www.w3.org/1999/XmlSchema" type = "Q1: string" nullable = "true"/>
</All>
</Complextype>
</Element>
<Element name = "getsecurityinforesult">
<Complextype>
<All>
<Element name = "result" type = "S0: securityinfo"/>
</All>
</Complextype>
</Element>
<Complextype name = "securityinfo">
<All>
<Element name = "code" xmlns: q2 = "http://www.w3.org/1999/XmlSchema" type = "Q2: string" nullable = "true"/>
<Element name = "companyName" xmlns: Q3 = "http://www.w3.org/1999/XmlSchema" type = "Q3: string" nullable = "true"/>
<Element name = "price" xmlns: Q4 = "http://www.w3.org/1999/XmlSchema" type = "Q4: Double"/>
</All>
</Complextype>
<Element name = "securityinfo" type = "S0: securityinfo"/>
</Schema>
</Servicedescription>

 

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.