Adding a Web Reference in Visual Studio 2010 (Part 2)

Some time ago I provided some step-by-step instructions for creating both a Web Reference and a Service Reference in Visual Studio 2010 in the Adding a Web Reference in Visual Studio 2010 post. In addition, the post explains the difference between the two to make it easier to understand when to use one over the other. The post has proven popular and a number of people have commented on it.

There are a number of questions about the post though and I wanted to answer them in this follow up post. The biggest question is where the WeatherSoapClient class comes from. The WeatherServiceClient() part of the code comes from the way in which Visual Studio interacts with the WSDL. If you look at:

http://www.deeptraining.com/webservices/weather.asmx?WSDL

you find that the WSDL doesn’t contain the word Client either. The WeatherServiceClient class is generated by Visual Studio in response to the WSDL it finds on that site.

 

Another complaint about that original post is that it relies on C#. Just to make things different, this post uses Visual Basic instead. Creating the Service Reference works precisely the same as it does with C#.


To see how this works, go ahead and create a Service Reference as specified in the original post. When you get done creating just the Service Reference, choose View | Class View in Visual Studio. You’ll see a new Class View window open up. Now, drill down into your project. If you create your example using Visual Basic, you’ll see something very similar to this:

WebService011

(The C# view of the dialog box is almost precisely the same.) What you’re seeing here is the result of creating the WeatherService Service Reference. I didn’t do anything else at all to this project. Highlighted in the upper window is the WeatherSoapClient referenced in my article. In the lower window you see the methods associated with that class.

Once you get done, you can recreate the example in Visual Basic. Just add two textboxes (txtCity and txtWeather) and one button (btnTest) to your application. Create an event handler for btnTest. Here’s the code you need to make it work:

Private Sub btnTest_Click(sender As System.Object, _
                          e As System.EventArgs) _
                       Handles btnTest.Click
 
   ' Create an instance of the Web service.
   Dim Client As WeatherService.WeatherSoapClient = _
      New WeatherService.WeatherSoapClient()
 
   ' Query the weather information.
   Dim Output = From ThisData _
      In Client.GetWeather(txtCity.Text) _
      Select ThisData
 
   ' Clear the current information and
   ' output the new information.
   txtWeather.Text = ""
   For Each Letter In Output
      txtWeather.Text = txtWeather.Text + Letter
   Next
End Sub

As you can see, the code is similar to the C# version I provided in the previous post. The point is that you really do need to use the Class View at times to determine how to interact with a Web service after you create either a Web Reference or a Service Reference. My book, LINQ for Dummies, provides a lot more in the way of helpful information on using Web services effectively for queries. If you want a simpler view of Web services using the C# language, check out C# Design and Development instead. Now you know that the names used by other authors don’t come out of thin air either, even though it might seem that way at times. Please let me know if you have any other questions about this example at John@JohnMuellerBooks.com.