Use Visual C # to create a web service step by step

Source: Internet
Author: User
Introduction:

In its. NET strategy, Microsoft publicized the web services it promoted. Currently, Web services are in full swing, and various new technologies are emerging. The development of Web Services is building a bright future in the Internet age. In this article, I will introduce some basic knowledge about Web services and how to use Visual C # To create a simple web service step by step.

1. Web Service Overview:

Web services are new web application branches. They are self-contained, self-describing, and modular applications that can be published, located, and called through the Web. Web services can execute any function from simple requests to complex business processing. After deployment, other Web service applications can discover and call the services deployed by the application. Web services can divide business logic into components one by one and then execute their functions across the Internet. Therefore, it is the latest technology development trend for building distributed and modular applications.

2. Why do we need web services?

In the past, distributed application logic used distributed object models by using basic structures such as DCOM, CORBA, and RMI, developers can still have the rich resources and accuracy provided by using local models, and can place services in remote systems.

Why do we have to worry about the web when we already have a favorite middleware platform (RMI, Jini, CORBA, DCOM, and so on? Middleware does provide powerful service implementation means, but these systems share a common defect that they cannot be extended to the Internet: they require that the service clients and the services provided by the system must be closely coupled, that is, a similar basic structure is required. However, such a system is often very fragile: if the execution mechanism at one end changes, the other end will crash. For example, if the interface of the server application changes, the client will crash. To expand to the Internet, we need a loosely coupled basic structure to solve this problem. In this case, the emergence of Web Services is ushered in.

Iii. Development Environment:

1. Windows 2000 Server OS or Windows xp OS;
2.. NET Framework and visual studio.net development tools.

4. Create a Web Service Project:

Here I will introduce you to a web service instance that converts US dollars to RMB. The functions of this instance are quite simple, and we can know the functions from the name. But this is also a very good example, especially for beginners, can play a very good guiding role. When creating a Web service, we use the C # language. The following are the specific project steps.

First, open vs.net, create a project, select "Visual C # project" on the left panel, and select "ASP. net web service, and name it "webservice1", as shown in the figure below:

Figure 1

After you press the "OK" button, vs.net will start to help you create the project. A dialog box for displaying Internet connections may also appear. After a project is created, the following interface is displayed in the development tool:

Figure 2

Because we want to implement a very simple web service, we need a small amount of functionality and code, therefore, you do not have to consider the two hyperlinks "server resource manager" and "toolbox". Instead, you can directly click the "here" link in to edit the code. After you click it, the code editing box is displayed, the figure is as follows:

Figure 3

In the above code editing box, we first remove the sample code of the original "Hello World" Web Service and replace it with our code. The final result is as follows:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. Web;
Using system. Web. Services;

Namespace webservice1
{
///

/// Summary of service1.
///

Public class service1: system. Web. Services. WebService
{
Public service1 ()
{
// Codegen: This call is required by the ASP. NET web service designer.
Initializecomponent ();
}

# Region component designer generated code

// Required by the Web Service designer
Private icontainer components = NULL;

///

/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///

Private void initializecomponent ()
{
}

///

/// Clear all resources in use.
///

Protected override void dispose (bool disposing)
{
If (disposing & components! = NULL)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

// Web service
// Dollarconverttormb () Service converts USD to RMB
// To test this web service, press F5

[Webmethod]
Public double dollarconverttormb (double dollar)
{
Return (dollar * 8.15 );
}
}
}

In the preceding method dollarconverttormb (), we return a value of the double type-dollar * 8.15, of which 8.15 is self-evident (the exchange rate between USD and RMB ). However, the actual exchange rate is not fixed and changes every day. Therefore, we need to connect to the database to obtain the latest information based on the actual exchange rate of the day. However, as a simple example, we certainly don't need to make it so complicated, so here I just want to assume that the exchange rate is. 15.

At the same time, we also need to note that we use the using system in the code of the web service. web, using system. web. services and other namespaces are obviously indispensable for Web service development. Without these namespaces, we cannot call them. net Framework provides us with methods and functions required for developing web services, so we must not forget.

By now, the Code has been compiled, and the code file is stored in a virtual directory (usually C:/inetpub/wwwroot/webservice1. Save the file as service1.asmx. The asmx file extension is a tag of. NET web services. After saving the file, your web service will be ready to show your face.

5. Test Web Services:

Now you are ready to test the web service. You do not need to compile the entire process explicitly. You only need to save the file in the directory and then call it. To call the newly created Service, open your browser and enter the service path, including the name of the asmx file. If you store the service in the C:/inetpub/wwwroot/webservice1 directory, type http: // localhost/webservice1/service1.asmx in the address bar of your browser. (Of course, when you use vs.net for development, you can use Ctrl + F5 to directly test the web service .)

A webpage containing a large amount of information is displayed when you call the service. In the beginning, this may make you feel a little confused: In any case, you have not created HTML pages for such services. In fact, you don't have to create a test webpage because the. NET Framework is already helping you. When you call a web service directly through a browser, the framework will generate a web page for you, display the Web service information to you, and list all available methods. Is the web page of the web service.

Figure 4

The methods listed in this example are nothing special, because there is only one method (dollarconverttormb ). Click this method to display another webpage, as shown in Figure 5. This page is the test page of the specific method, which includes the text box of each parameter accepted by the corresponding method. Enter "10" in the text box and press "call.

Figure 5

Click the "call" button to open a new browser window, showing some XML code. The XML code is returned by the Web Service, including the results of the service. Shows the returned XML code:

Figure 6

The returned results are some XML code that may seem unfriendly to the user interface, but these results do not have to be in a user-friendly format, Because you usually do not directly call the Web service from the browser. On the contrary, you often call the Web service from an application and properly process the returned XML code. However, it is also easy to see from the code that the Web Service has converted $10 into RMB 81.5.

6. Summary:

The example above is simple. The task is to create a component that can be accessed by anyone anywhere in the world if it is placed on a Web server. Customers do not have to load com or DCOM, or even have to own Windows client programs. Any client program that can create an HTTP connection can call the web service and receive the result. This feature opens up a new area for creating distributed applications and implementing interoperability between platforms. At the same time, it is not difficult to find that using vs.net to develop Web Services is quite easy. Interested readers can try to develop more powerful web services and assign them to practical applications.

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.