Source Code Download Issues

Most of my books include source code. The source code is made available on the publisher’s Web site and you download it from there. I always recommend downloading the source code to reduce the potential for errors. Typos are the number one source of problems for readers who work with the source code in my books. A single mistyped word will cause the application to fail in some way. If the reader is lucky, the compiler will catch the error and provide a clear message saying what’s wrong (such as the fact that the .ResX file is missing), but often that isn’t the case and then the reader becomes frustrated with the unworkable code. Sometimes the error isn’t even the reader’s fault—errors do creep into books (see my Errors in Writing post for details). The point is to use the fully tested downloadable source code whenever possible to save yourself time and effort.

 

There is another good incentive to download the source code. The reason to work through the source code in your debugger is to discover how it works through actual tracing. To help accomplish this task, the source code examples are carefully crafted to provide an optimal learning experience and they include features such as additional comments to help enhance the learning experience (see my Handling Source Code in Books post for details).


However, what happens when the downloaded source doesn’t appear to work either? Some readers have complained that the downloaded example won’t compile. Normally, the problem is one of a missing file. Firewalls and other network security features can strip out files and some organizations are proactive about stripping out anything that even looks binary. Consequently, the downloaded ZIP containing the source code is missing solution (.SLN), project (.CSProj or .VBProj), resource (.ResX), or other files. The project won’t compile with missing files. The best course of action is to try to download the source code from another systemone that isn’t inhibited by the network security features that stripped out the files.

 

Missing .DLL files aren’t normally an indication of a failed download. In most cases, missing .DLL files are part of a library or third party product you must install. The book will always include instructions on where to obtain these required add-ons and how to install them. Make sure you install any required additions as part of your system setup.


Sometimes a reader doesn’t have another way to download the file. Asking me to send the source code to you won’t help because the same features that strip out the files from the publisher site will also strip them out of the e-mail I send containing the files. In most cases, the firewall still downloads the actual source code. Create the project using the instructions from the book, and then copy and paste the source from the downloaded files into this new project. It’s not a perfect solution, but it should still work better (and faster) than typing all of the source code by hand.

A few readers have asked that I create a disk containing the source code and mail it to them. I’m not set up to perform this kind of support and the costs would be prohibitive. Unfortunately, if all else fails, you may have to try typing the source in by hand, but it should be your last option because typos really can ruin the learning experience. I’m always open to suggestions for making your reading experience better. If someone has a magic bullet solution that will help others get the downloaded source code in one piece, please let me know 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.

 

Getting the Right Visual Studio Add-In

Give me the right tool and I can use it to create just about anything in code! It’s a bold statement and I’m sure that I’d have little trouble finding a lot of other developers who believe precisely the same thing. Unfortunately, finding the right tool is akin to finding a needle in a haystack (feel free to substitute any other worn cliche you can think about). How does someone quantify the usability of a tool or decide whether to even download it in the first place. I recently wrote an article for Software Quality Connection entitled, “Techniques for Finding Useful IDE Add-ins” that answers that question. The article proposes some techniques you can use to make the process of finding just the right add-in easier.

Of course, my great add-on is a piece of junk for you. Our needs, goals, programming skills, and programming tasks differ. So, what I’d like to know is how you look for add-ins and how you use them to create better applications. It’s nice to get another perspective on a given topic. Over the years I’ve talked with hundreds (perhaps thousands) of readers about the tools they use because the right tool in the right hands can do amazing things. Most of my books contain a healthy number of links to various tools and I often employ add-ins in my books to make tasks easier. Let me know about your favorite add-in and tell me why you think it’s so great at John@JohnMuellerBooks.com. (Please, no vendor e-mails. I already know your tool is great; I really want to hear from people in the trenches on this topic.)

Part of my reason for asking this sort of information is to improve the quality of my books and articles. Quality is extremely important to me. In fact, it’s the reason I created the beta reader program. A beta reader reviews my manuscript as I write it. You can find out more about the beta reader program in my Errors in Writing post. If you want to become a beta reader, just write me at John@JohnMuellerBooks.com for additional details. In the meantime, try out some new add-ins and have a bit of fun .

Working with Low Level Code

Working with low level code is becoming less necessary as Microsoft continues to improve the .NET Framework, but you sometimes still need to resort to direct Win32 API access using P/Invoke.  My book, “.NET Framework Solutions: In Search of the Lost Win32 API,” is a great place to learn about P/Invoke and the significant number of ways you can use it to access Windows features that Microsoft hasn’t made available in the .NET Framework yet.  For example, you’ll find a thorough discussion of the Windows messaging system in Chapter 4.  However, the discussion is a bit lengthy because there is so much you can do with the Windows messaging system.

One of the questions I get asked quite often is whether there is a quick start sort of guide I can recommend for working with the Windows messaging system.  With that in mind, I wrote a series of four DevSource articles some time ago.  Here’s the complete article list:


These four articles provide quite a bit of information about Windows messages that you might not know from a .NET perspective.  Using these techniques can save you considerable time, especially when you need to interact with other applications.  In fact, the final article reveals secrets you can use to interact with applications when you don’t have the source code; a significant problem for most developers.  So, how do you use P/Invoke?  Have you had to resort to P/Invoke to work with Windows 7 features that haven’t been added to the .NET Framework yet?  Let me know at John@JohnMuellerBooks.com.