C # create a Web Service

Source: Internet
Author: User
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 object-oriented programming techniques to create a class. C # classes are very similar to C ++ and Java. Using C # To create a class is as simple as walking in the park, and no skills are required.

C #'s basic data types are designed very intelligently. Therefore, if we return "int," "float," or "string", they will be automatically converted to 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 to extract all three types of data. There are several ways to do this. The best way is to bind them to a data type that can be enumerated, 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 create a web service through the module. 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)
{
// 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. Since we cannot store any session data, we 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 to display a new window and XML document.

 

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

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 will use the http get method to call the XML document directly, 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

Discovering a Web Service is a process of querying and locating the Web service description. It is a preparation process for accessing the web service. The client obtains the existence, size, and, how to interact with him, the "discovery" file is a file 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.