Use Visual Basic or C # to create a Web Service
The following describes how to create a web service that converts the Fahrenheit temperature to the Celsius temperature. It uses Visual Basic or C #. All controlledCodeAll are compiled into "Intermediate Language" (msil: Microsoft intermediate language) and then executed in the general language runtime environment.
Note that the process of creating a Web service using C # Or Visual Basic is almost the same. Each code sample is displayed in C # and Visual Basic.
We will do the following:
AB creates a web service to convert a standard Fahrenheit temperature to a standard Celsius temperature.
AB uses the Web service description page to test the Web service function.
To complete the following steps, you must provide:
AB is a machine that meets the needs of creating a web project.
this article includes the following content:
using AB to create a web service project
using AB to implement web service
N new method and compile Web Service
using AB to test and deploy Web Service
create a web service project
you can use the template provided by Visual Studio to start a project and its necessary files for the web service, you can choose to use Visual Basic or C #.
1. Go to the File menu, locate new, and click Project.
2. In the new project dialog box, select one of the Visual Basic projects or Visual C # projects folders.
3. Click the Web Service icon.
4. Change the project name to tempconvert1.
5. If necessary, enter a web server address that you can use to develop Web Services.
note that this server is called your development server. By default, the development server and Visual Studio are on the same machine. The project is developed and compiled on the development server.
6. Click OK to create the project.
Visual Studio automatically creates necessary files and contains necessary references to Support Web Services.
Implement Web Service
In this step, we will compile code that provides real services for Web Service customers. These codes are placed in the code behind file created for us by Visual Studio, which is associated with the. asmx file of web service. The Code-behind file can be. CS (C #) or. VB (Visual Basic), depending on the language we chose when creating the web service.
Add method and compile Web Service
1. In Solution Explorer, expand the references folder to check whether there is a reference to system. xml. serialization. If not, add it to enable soap section 5 encoding.
2. Select service1.asmx (Visual Basic) or webservice1.asmx (C #) in Solution Explorer and press F7 to view the code-behind file. Alternatively, click code in the View menu.
Note that the code-behind file is hidden by default. On the Solution Explorer toolbar, click the "show all files" icon to display hidden files. Alternatively, Click Show All files in the project menu.
Note by default, Web Services implemented using Visual Basic or C # Do not use the optional soap encoding format described in section 5 of the soap 1.1 specification. To create a controlled code web service using section 5 encoding format, it is necessary to add the soapservice attribute to the class Implementation of Web Service and specify an RPC soapservice format. For more information, see the readme.htm file in the Visual Studio. NET Beta 1 CD-ROM disc 1root directory.
3. Locate the Web Service Sample Code (Hello world) and enter the code that matches the language you selected:
'Visual basic
Public Function <webmethod ()> converttemperature (byval dfahrenheit as double) as double
Converttemperature = (dfahrenheit-32) * 5)/9
End Function
// C #
[Webmethod]
Public double converttemperature (double dfahrenheit ){
Return (dfahrenheit-32) * 5)/9;
}
Note: Web Services supports a majority Programming Language All supported data type subsets. When creating web services, make sure that the parameter and return value type you specified are supported.
Attaches the webmethod property to the method, indicating that it will be used as part of the Web service to expose. In addition, classes and methods must be marked as public.
4. In Solution Explorer, select service1.asmx (Visual Basic) or webservice1.asmx (C #).
5. Go to the project menu, locate the WEB Project, and click set as start page.
6. Save solution.
7. Go to the build menu and click build.
Test and deploy Web Services
When a. asmx file is opened in a browser, a default description page is displayed, which provides information about the web service. The link labeled SDL contract on this page will take you to a service description document containing this web service.
1. Select service1.asmx (Visual Basic) or webservice1.asmx (C #) in Solution Explorer #).
2. Go to the File menu and click View in browser.
3. In the dfahrenheit text box, enter the number 212 and click the invoke button.
The Web service returns the converted value response in an XML document, as shown in the following figure:
<? XML version = "1.0"?>
<Double xmlns = "http://tempuri.org/"> 100 </Double>
4. Deploy the project
To make your web service effective to others, you need to deploy it on a web server that can connect to the user you want to support.
For more information about deployment, see.