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.

 

Adding a Web Reference in Visual Studio 2010

There is a new wrinkle in working with Web services in Visual Studio 2010. In previous versions of the product, you could access a Web service by creating a Web Reference to it. All you did was right click the project and choose to add a Web Reference from the context menu. After entering a few bits of information, the IDE created a class you could use to access the Web Service for you. The process was quite simple because all you needed to do then is create an instance of the class and use the methods it provided to access the Web service as if the information was local to the machine.

The default operation for newer versions of Visual Studio is to create a Service Reference, which works differently from the Web Reference you used in the past. Service references are actually quite flexible, but they require a different technique to use. For example, I show how to use a Service Reference to access a Web Service using LINQ in Chapter 13 (page 278) of LINQ for Dummies. LINQ for Dummies is unique, however. Most of my books, including C# Design and Development, rely on the older Web Reference technique, as do all of my articles. This post provides a quick overview of the differences between the two techniques and examines how to create a Web Reference in Visual Studio 2010 (including all of the Express products).

 

Even though the source code in this post relies on C#, the same principles hold for Visual Basic.NET. In fact, the process for adding and using the Web Reference is almost precisely the same. If you’d like a Visual Basic.NET version of this example, please contact me at John@JohnMuellerBooks.com and I’ll provide another post.

Let’s examine the Service Reference technique first. For this example, I use an extremely simple Web service that’s free and works equally well using either technique. You can find the description of this Web service at http://www.deeptraining.com/webservices/weather.asmx. To begin, you right click the project entry in Solution Explorer and choose Add Service Reference from the context menu. After you type http://www.deeptraining.com/webservices/weather.asmx?WSDL in the Address field and click Go, you’ll see the information shown here (expanded so you can see everything).

WebService01

Notice that I’ve also typed WeatherService in the Namespace field to give the Service Reference an easy name to remember. The Web Service offers a single method, GetWeather(), that requires a String containing the name of the city as input. The output is a String containing a single word weather forecast for the city in question. This example also has two textboxes, txtCity (contains the input string) and txtWeather (contains the output string), and a button, btnTest. Given this setup, here’s the code you’ll use to access the Web service using a Service Reference:

private void btnTest_Click(object sender, EventArgs e)
{
    // Create an instance of the Web service.
    WeatherService.WeatherSoapClient Client =
        new WeatherService.WeatherSoapClient();
 
    // Query the weather information.
    var Output = from ThisData
                 in Client.GetWeather(txtCity.Text)
                 select ThisData;
 
    // Clear the current information and output the
    // new information.
    txtWeather.Text = "";
    foreach (Char Letter in Output)
        txtWeather.Text = txtWeather.Text + Letter;
}

As you can see, the example begins by creating an instance of the Web service client. It uses this instance, Client, in a LINQ statement that calls on GetWeather() to obtain the weather for the city specified by txtCity.Text. The odd thing is that the Output actually receives a Char array that contains the weather information, so you have to use some method for turning that array into a string. The example shows just one method for performing this task.

Now, let’s say you need to use a Web Reference instead of a Service Reference. For example, a lot of people are encountering problems using this approach due to encoding problems. A number of Web sites use ISO-8859-1, which won’t work with this technique unless you build a custom encoder (see the article at WCF: Text Message Encoding and ISO-8859-1 Encoding for a discussion of this solution).

In this case, you start by right clicking the project entry in Solution Explorer and choosing Add Service Reference as you did before. However, this time you click Advanced at the bottom of the dialog box, which displays the Service Reference Settings dialog box shown here.

WebService02

Notice the Add Web Reference button in the lower left corner. Click this button and you’ll see the Add Web Reference dialog box shown here. (I’ve already included the WSDL file URL and provided the name of the Web reference that you should use if you’re following along.)

WebService03

Click Add Reference and Visual Studio will add the new reference for you. However, this reference appears in the Web References folder, not the Service References folder, as shown here.

WebService04

I added another button to the application to test this Web Reference, btnTest2. The code is quite a bit different from the LINQ technique used for a Web Service as shown here:

private void btnTest2_Click(object sender, EventArgs e)
{
    // Create an instance of the Web service.
    WeatherService2.Weather Client =
        new WeatherService2.Weather();
 
    // Call the Web service method required to obtain the data.
    txtWeather.Text = Client.GetWeather(txtCity.Text);
}

Both event handlers produce the same result, using the same essential call, Client.GetWeather(txtCity.Text). The difference is that one uses a Service Reference with LINQ and the other uses a Web Reference with procedural code. So far I haven’t found a decided speed advantage for either approach. In this particular example, you type less by using the second approach, but that might not always be the case, especially as the Web service gains in complexity. The point is that both methods are available.

If you’re working with one of my books, you’ll probably need to use this second approach in most cases because it’s the approach that Visual Studio relied upon until recently. As always, let me know if you have any questions about this post or my books at John@JohnMuellerBooks.com.