Connecting C# Windows Applications to a Web Service
In this tutorial, we will see how to connect a C# Forms application to an XML Web Service and exchange data between the two.
Adverts Blocked Please disable AdBlocking software and allow me to set cookies so that I can continue providing free content and services.
This article relates to an old version of the .Net Framework. While the contents are still relevant, future versions of the framework may change, or improve upon, the contents of this article.
You will need the Web Service from the last tutorial open and running in Visual Web Developer (or Studio) and another instance of Visual Studio / Visual C# running for the Windows Application. Ensure that the web service is running correctly by navigating to its URL (e.g. http://localhost:port/WebService/) If in doubt restart the debugger and copy the URL it opens.
Within Visual C# create a new Windows Forms application. On the Solution Explorer right click on References and select Add Web Reference (or Add Service Reference depending on version). In the address box paste in the URL for the web service, you are connecting to and click GO. In my case, the service is located at http://localhost:49699/WebSite1/Service.asmx.
The Add Server Reference dialogue box will then communicate with the web service and download the WSDL and display the service and any methods it exposes. The service we created is listed under Services->SoapService. Soap is the protocol used to package the XML data for transport across the Internet. You can also see the two operations, toCelsius and toFarrenheit listed. Give the web service a meaningful namespace, such as temperatureConverter, and click OK. Different versions of Visual Studio will show different windows, but all will have the same functionality. Visual Studio will then create proxy classes for the service that allow the forms application to communicate with it.
The next thing we need to do is create a form with some controls on to input and output the data. For this simple web service, we will create a label, text box and a button for Celsius conversion and the same again for Fahrenheit. The final form will look like this:
Double click on the convert from Celsius button to bring up the code editor. We will create an instance of the web service class and invoke the web method:
temperatureConvert.Service tc = new temperatureConvert.Service(); textBox2.Text = tc.toFarrenheit(Int32.Parse(textBox1.Text)).ToString();
The temperatureConvert namespace is the same namespace that you used when adding the web reference. We can then use the proxy class as if it were defined within our project.
The other button will use the opposite values.
temperatureConvert.Service tc = new temperatureConvert.Service(); textBox1.Text = tc.toCelsius(Int32.Parse(textBox2.Text)).ToString();
And that's all there is to it... using the automatically created proxy class you can treat a web service as a local class and access any of the exposed web methods.
Last updated on: Friday 23rd June 2017