Web Design that taps into the haromny and vision of your dreams.

Connecting C# Windows Applications to a Web Service

How to access a Web Service from a forms application

Written By on in C#

458 words, estimated reading time 3 minutes.

In this tutorial, we will see how to connect a C# Forms application to an XML Web Service and exchange data between the two.

Introduction to Web Services Series
  1. What are XML Web Services?
  2. Creating a Simple XML Web Service
  3. Connecting C# Windows Applications to a Web Service

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.

XML Web Services Add Web Reference
XML Web Services Add Web Reference

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:

Connecting Windows Applications to a Web Service
Connecting Windows Applications to a Web Service

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

 

Comments
Helmut Scheiss

Helmut Scheiss

Best friggin' example I've ever seen. Just in case some people had trouble completing the service reference in VS 2010 :
The Add Server Reference dialog box will then communicate with the web service and download the WSDL and display the service and any methods it exposes. The serice we created is listed under Services->SoapService.

Click the "Advanced Button", then click on the "Add Web Reference" button and continue from there.

Reply to Helmut Scheiss
Peter

Peter

Thanks a lot, you're a star!
Used quite some time to figure this out and your tutorial just made it so easy that I'm almost crying :-)

Reply to Peter
Balachandran

Balachandran

It's work perfectly. Thanks..
Thanks..Thanks..Thanks..Thanks..Thanks..Thanks..

Reply to Balachandran

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me