Step by step with Visual C # Create a web service

xiaoxiao2021-03-06  103

INTRODUCTION: Microsoft has done a big promotion in its .NET strategy. Now, the web service is in full swing, and the relevant new technologies are endless. The development of Web services is building a beautiful tomorrow in the Internet era. In this article, I will introduce some of the basic knowledge of Web services and how to create a simple web service in one step in step by step by step.

One. Web service overview:

Web services are a new web application branch that is self-contained, self-description, modular applications, can be posted, locating, via web calls. Web services can perform any feature from simple requests to complex business processing. Once the deployment, other Web service applications can discover and call their deployment services. The web service can divide the business logic one component and then execute its function over the range of the Internet. Therefore, it is the latest technology development trend of constructing distributed and modular applications.

two. Why do I need a web service?

Previously, distributed application logic requires the use of distributed object models. By using the basic structure such as DCOM, CORBA, RMI, developers can still have the rich resources and accuracy provided by the local model, and will serve Placed in a remote system.

When there is already a middle-aged middleware platform (RMI, Jini, Corba, DCOM, etc., why should we worry about Web? The middleware does provide a powerful service implementation, but these systems have a common defect, that is, they cannot extend to the Internet: they require the service client to be closely coupled between the services provided by the system itself, that is, the requirements A similar basic structure. However, such a system is often very fragile: if the execution mechanism of one end changes, the other end will crash. For example, if the server application is changed, the client will crash. In order to scale to the Internet, we need a loose coupling basic structure to solve this problem. In this case, the birth of the web service is ushered in.

three. Development environment:

1. Windows 2000 Server operating system or Windows XP operating system;

2. .NET Framework and Visual Studio.NET development tools.

four. Create a web service project:

I am here to introduce you to a Web service instance to the renminbi-converted web service. The function of the instance is quite simple. From the name, we can know the functions. But this is also a very good example, especially for beginners, can play a good guidance. In the process of creating a Web service, we use the C # language. Below is the specific project steps.

First, open vs.net, create a project, select "Visual C # item" in the panel on the left, select "ASP.NET Web Service" in the right side, and name "WebService1", the illustration is as follows:

figure 1

After pressing the "OK" button, VS.NET starts to help you create the project, and a dialog for displaying the Internet connection can occur during the period. After the new project is created, the interface as shown below is displayed in the development tool:

figure 2

Because we want to implement a very simple web service, we need the features and our code quantity, so you don't have to consider "Server Explorer" and "Toolbox" in the figure. Link, and you can directly click here "This" link for code editing. After clicking, the code edit box will be opened, the illustration is as follows:

image 3

In the above code editing box, let's remove the original "Hello World" web service, replace it into our code, eventually 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

{

///

/// service1's summary description. ///

Public class service1: system.web.services.Webservice

{

Public service1 ()

{

// Codegen: This call is necessary for the ASP.NET Web service designer.

InitializationComponent ();

}

#Region Component Designer Generated Code

/ / Web service designer necessary

Private icontainer Components = NULL;

///

/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///

Private vidinitiRizeComponent ()

{

}

///

/// Clean all the resources being used. ///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing && Components! = NULL)

{

Components.dispose ();

}

Base.dispose (Disposing);

}

#ndregion

// Web service

// DollarConvertTORMB () Service Completes the conversion of the US dollar to RMB

/ / To test this web service, press F5 key

[WebMethod]

Public Double DollarconvertTORMB (Double Dollar)

{

Return (Dollar * 8.15);

}

}

}

In the above method DollarConverttormb (), we return a Double type value - Dollar * 8.15, 8.15 I think is self-evident (the exchange rate of the US dollar to RMB). However, the real exchange rate is not fixed, and every day is changed, so we must calculate based on the actual exchange rate on the day, then we have to connect to the database to get the latest information. However, here as a simple example, we certainly don't need to make it so complicated, so I have aunt here and assume that the exchange rate is 1: 8.15.

At the same time, we also need to pay attention to the names such as using system.web, using system.web.services in this web service, which is obviously unable to be missing, and there is no such thing as a web service. We can't call the .NET framework for the methods and functions necessary for us to develop Web services, so you must not forget.

So far, the code is written, and then store the code file in a virtual directory (usually C: / INETPUB / WWWWROOT / WebService1). Save the file as Service1.asmx. The ASMX file extension is the tag of the .NET web service. After saving the file, your web service is ready to appear.

V. Test Web Services:

Now prepare the test web service. During the period, you don't have to use the explicit compilation process, just save the file in the directory and then call it. To call the latest 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 want to type http://localhost/webservice1/service1.asmx on the browser address bar. (Of course, when you use VS.Net development, you can also test the Web service directly through Ctrl F5.) A web page containing a lot of information is displayed when calling the service. At this point, this may make you feel a bit confused: No matter what to say, you have not created HTML web pages for this service. In fact, you don't have to create a test page at all, because .NET Framework has been helped you. When you call a web service directly through your browser, the framework will generate a web page and display the information of the web service through it, and list all available methods. The picture below is a web page for web services.

Figure 4

There is nothing special in this example, because there is only one method (DollarConverttormb). The mouse click this method to display another web page, as shown in Figure 5. This page is the test page of the particular method, including the text box of each parameter accepted by the corresponding method. Now enter "10" in the text box and press the "Call" button.

Figure 5

Click the "Call" button to open a new browser window, which shows some XML code. These XML code is returned by the web service, including the results of the service. The returned XML code is shown below:

Figure 6

The result of returning is some XML code, and the user interface is not so friendly, but these results do not necessarily have to take the user-friendly format, because you usually don't call the web service directly from the browser. Instead, you tend to process the returned XML code as appropriate from the application calling Web service. However, it is also easy to see that the web service has been converted to 81.5 yuan from the code.

six. to sum up:

This example above is very simple. Its task it complete is: Create a component, if the component is placed on the web server, you can be accessed by anyone in the world. Customers don't have to load COM or DCOM; or even have a Windows client. Any client that creates an HTTP connection can call the web service and receive the result. This function has opened up a new field of creating a distributed application to implement interoperability between platforms. At the same time, we are not difficult to find a considerable thing to develop web services with vs.net. Interested readers can try to develop a more powerful web service and give it a practical application.

转载请注明原文地址:https://www.9cbs.com/read-123732.html

New Post(0)