Developers
Local Navigation
Exercise
Create a Microsoft Windows application that uses the sample web service operations.
Prerequisites
- Define and implement the web service operations and attributes
- Compile, test and publish the web services
Objectives
- Discover how to create a Microsoft Windows application that uses web service operations.
Setup Tasks
This lab is dependent on the previous two labs. Simply downloading the completed code files from the previous labs and installing them on your IIS Server may not work due to IIS Server settings. To set up a web service project and define web service operations and attributes, see the lab entitled Define and implement the web service operations and attributes for more details. To compile, test and publish the web service see the lab entitled Compile, test and publish the web service.
Tasks
The complete sample code is available at the end of this section.
- In Microsoft Visual Studio .NET, on the File menu, click New > Project.
- In the Project Types tree, click the Visual C# Projects folder.
- In the Templates pane, click Windows Application.
- Change the project location to a local directory, for example, c: empMetricConvWSTest
- Click OK.
- To create a web reference to the Measurement Conversion web service, in the Solution Explorer window, right-click the References tag.
- Click Add Web Reference.
- Type the path to the metric conversion web service at http://localhost/MetricConvWS/Service1.asmx.
- Click Go.
- When the web service is detected, click Add Reference.
- To add a button to test the web service, in the Toolbox pane, drag the Button control to the application form.
- Double-click the button.
- Add the following code to the button1_Click method:
- To run the application, press F5 and click the button. The following test results display in the debug output panel:
1 km = 0.6214 miles
1 mile = 1.6093 kms
private void button1_Click(object sender, System.EventArgs e)
{
localhost.MetricConversionWebServiceinC MyService =
new localhost.MetricConversionWebServiceinC();
System.Console.WriteLine("1 km = " + MyService.km2mile(1) + " miles");
System.Console.WriteLine("1 mile = " + MyService.mile2km(1) + " kms");
}
Tip: Note that localhost is an identifier of the web reference that you created to the web service.
Check Your Work
Download the sample example WebForm1.aspx.cs from here.