Calling A Web Service With C#

Calling a web service with C# and Visual Studio isn’t that difficult as it maybe seems in the first place. In this post I will show how to add a service reference and use it when there is a Web Service Description Language aka WSDL available.

With the WSDL file Visual Studio automatically generates all necessary method calls for the WebService. You can later change the web service endpoint in the configuration file (e.g. web.config) if you have different development and production endpoints.

1. Add Web Service Reference in Visual Studio

Adding Service Reference in Visual Studio

 

Add Service and set Namespace

2. Accessing the Web Service with C#

After Adding the web service reference in Visual Studio you can access the web service through the generate client which is named by the web service and client at the end.

In this example the Web Service returns the given city name and random values for the today’s and tommorrow’s weather.

3. Result

Output of the program

If you are wondering where this web service is available – I just created one on my local machine for this tutorial :-). Probably I will create a tutorial for this too – so stay tuned.

 

You can also watch a very good YouTube Tutorial on this topic here:

[quads id=1]
Close ×

One Reply to “Calling A Web Service With C#”

  1. //Code from Video:

    WeatherService.GlobalWeatherSoapClient client = new WeatherService.GlobalWeatherSoapClient();
    var response = client.GetWeather(txtBxCity.Text, txtBxCountry.Text);
    lblResult.Text = response;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.