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.

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 99 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/. When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.