Manually adding third-party library references

In this chapter we will describe the most common way a developer finds a library and adds it to a solution. For the sake of simplicity, let us create a console application that will retrieve and parse an JSON result from a web service, for example, Google query search.

Let us start by creating a new console application:

In Visual Studio 2012, navigate to File | New Project. In the New Project dialog, choose the Console Application Visual C# template. Let us name our solution CH1_01_GoogleApiSearcher , and start writing some code as shown: class Program { static void Main() { var request = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=nuget"; using (var webClient = new WebClient()) { var result = webClient.DownloadString(request); Console.WriteLine(result); } } }

In the first line of the Main method, we store a HTTP request in a string, and it is used to query Google's Search API for search results for the nuget query. In the second line of this method, we are creating an instance of WebClient , making the web request, and then storing the result in a new string variable. The last line outputs the raw result to the console, in the form of a large unformatted JSON string.

Great, we have a JSON result. We now need a JSON library to parse our result into a meaningful form. When dealing with unknown libraries, the best choice is to type some relevant keywords into our favorite search engine.

For example, let's write json for .net into Google's search engine. The first hit on Google is Json.NET - Home, which sounds about right. As we all know, the first hit is not necessarily the best choice when searching for third-party libraries, but in this case we got lucky. Following the first hit, we are forwarded to http://json.codeplex.com/. After reading the description, we are sure that this is the appropriate library for our problem. So, we will find a download button and transfer the archived files to the computer. After opening the archived file, we will see two folders: Bin and Source . We decide that we will not be needing the library's source code; let us just choose the binary assembly and reference it from our project. In the Bin folder, we see another subset of folders with platform names as shown in the following screenshot; so again, we have to choose what to use in our code:

Since we are writing our application in .NET Framework 4.5, files in the Net45 folder should be the right ones. But where should we put these files? There are no clear guidelines about how and where external references should be stored. In our root solution folder we should create a folder that will contain our external references. Let's name our newly created folder ExternalReferences and place the assemblies there.

Now we are set to add a reference in our project:

Choose Add Reference from the context menu in Solution Explorer on the References node. Use the Browse button to select our assembly file from the file dialog box. The assembly file is located in the ExternalReferences folder in our project's root folder. Click on the OK button to confirm the selection and add the assembly to the project.

Now we are ready to use the Newtonsoft.Json library for JSON manipulations. By changing our source code, we are able to get more useful information from our web service result. Let's make changes to our Main method, so it will look as shown:

static void Main(string[] args) { var request = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=nuget"; string result; using (var webClient = new WebClient()) { result = webClient.DownloadString(request); } // Deserializing JSON string to object dynamic data = JsonConvert.DeserializeObject(result); // Looping throughout results foreach (var item in data.responseData.results) { Console.WriteLine("{0} {1}", item.titleNoFormatting, item.url); } }

Tip Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

We used a method from our recently-added JsonConvert.DeserializeObject library. This code is written in .Net 4.5 and we can use the dynamic type for our result.

Tip The dynamic keyword is also available in .Net 4.0; more about the dynamic keyword is available at http://bit.ly/csdynmc.

In the foreach loop, we are looping through results and displaying the titleNoFormatting and url properties of results, which are our search results. Running the application outputs the following to the console window:

NuGet Gallery | Home http://nuget.org/ NuGet - Home http://nuget.codeplex.com/ NuGet - Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/NuGet NuGet Package Manager extension - Visual Studio Gallery - Microsoft http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c

This library did not require editing of the configuration file, which is usually the most time-consuming step when using third-party libraries. Now, we have added a reference manually and successfully used it in our project. In the next chapter, we will learn how to set up NuGet in a Visual Studio environment; then we will take a look at how to add references using the NuGet Package Management.