To create a Web service in Visual C # step-by-Step

Source: Internet
Author: User
Tags web services visual studio
Visual|web|web Services | Create quotes:

Microsoft in its. NET strategy, the Web services of its main push are trumpeted. Now, Web services are booming and new technologies are emerging. The development of Web services is building a better tomorrow in the Internet era. In this article, I'll introduce you to some of the basics of Web services and how to create a simple Web service step-by-step with Visual C #.

A Web Services Overview:

Web services are a new branch of Web applications that are self-contained, self-describing, modular, and can be published, positioned, and invoked via the Web. Web services can perform any function from simple requests to complex business processes. Once deployed, other Web service applications can discover and invoke the services that it deploys. Web services can divide business logic into one component and perform its functions across the Internet. Therefore, it is the latest technology development trend to construct distributed and modular applications.

Two 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.

Three Development environment:

1. Windows server operating system or Windows XP operating system;
2..Net framework and Visual Studio.NET development tools.

Four To create a Web service project:

I am here to introduce you to a dollar to renminbi conversion of the Web Services instance, the completion of the function is quite simple, from the name we can know the function. But this is also a very good example, especially for beginners, can play a very good guiding role. In the process of creating a Web service, we used the C # language. The following is the specific project steps.

First, open vs.net, create a new item, select "Visual C # project" In the left panel, select "ASP.net Web Service" in the right panel, and name "WebService1" as shown below:



Figure 1

When you press the OK button, Vs.net begins to help you create the project, and a dialog box may appear to display the Internet connection. After you have created the project, the following interface appears in the development tool:



Figure 2

Because we're going to implement a very simple Web service, so we need the functionality and our code is very small, so we do not have to consider the figure in the "Server Explorer" and "toolbox" two hyperlinks, but you can directly click on the "Here" link in the map to edit the code, Click to open the Code edit box, as shown below:



Figure 3

In the Code edit box above, we first remove the original "Hello World" Web service sample code and replace it with our code, which 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 description of the 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;

///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
private void InitializeComponent ()
{
}

///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing && components!= null)
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

#endregion

WEB Services
DOLLARCONVERTTORMB () service completes the conversion of USD to RMB
To test this WEB service, press the F5 key

[WebMethod]
Public double DOLLARCONVERTTORMB (double Dollar)
{
Return (Dollar * 8.15);
}
}
}

In the above method DOLLARCONVERTTORMB (), we return a double value ――dollar*8.15, 8.15 of which I think is self-evident (that is, the exchange rate of the dollar to the renminbi). But the real exchange rate is not fixed, and every day to change, so based on the actual exchange rate of the day to calculate, then we will connect to the database to get the latest information. However, as a simple example, we certainly do not need to be so complicated, so here I assume the exchange rate is 1:8.15.

Also, we need to note that in the code of the WEB service we use the using system.web, using System.Web.Services and other namespaces, as WEB services development, these namespaces are obviously indispensable, without these, we can not invoke the. NET Framework to provide us with the necessary methods and functions to develop WEB services, so we must not forget.

So far, the code is written, and the code file is stored under a virtual directory (usually C:\Inetpub\wwwroot\WebService1). Save the file as a service1.asmx. The asmx file name extension is the markup for the. NET Web Service. After you save the file, your Web service is ready to show up.

Five. Test the Web service:

Now you are ready to test the Web service. You do not have to explicitly compile the entire process, only need to save the file in the directory and then call it. To invoke the newly created service, open your browser and enter the service path, including the name of the ASMX file. If you put the service in the C:\Inetpub\wwwroot\WebService1 directory, then you need to type http://localhost/WebService1/Service1.asmx on the browser's address bar. (Of course, when you use vs.net development, you can also test Web services directly through CTRL+F5.) )

A Web page that contains a large amount of information is displayed when the service is invoked. At first this may make you feel a bit confused: Anyway, you haven't created HTML pages for this service. In fact, you don't have to create a test page at all, because. NET Framework has helped you with this. When you invoke a Web service directly through the browser, the framework creates a Web page for you and displays information about the Web service through it, listing all available methods. The following figure is a Web service page.



Figure 4

The methods listed in this example are nothing special, because there is only one method (DOLLARCONVERTTORMB). This method of clicking the mouse will display another page, as shown in Figure 5. This page is the test page for that particular method, which includes a text box for each parameter that the corresponding method accepts. Now, enter "10" in the text box and press the "Call" button.



Figure 5

Clicking the call button opens a new browser window with some XML code displayed. The XML code is returned by the Web service, which includes the results of the service. The XML code returned is as shown in the following illustration:



Figure 6

The result is some XML code, and the user interface may seem less user-friendly, but these results do not necessarily have to be in a user-friendly format because you typically do not invoke Web services directly from the browser. Instead, you often invoke the Web service from the application and handle the returned XML code appropriately. However, it is also easy to see from the code above that Web services have converted 10 of dollars into 81.5 yuan.

Six Summarize:

The example above is simple, and it accomplishes the task of creating 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, nor do they even have Windows clients. Any client that can create an HTTP connection can invoke the Web service and receive the results. This functionality opens up new areas 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 a fairly easy task. Interested readers can try to develop a more powerful Web service and give it a practical application.




Related Article

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.