Prehistory
I was testing a web application and it appeared that all unit tests have passed on my dev machine, but on the TeamCity build machine some tests would fail due to
System.IO.FileLoadException: Could not load file or assembly 'NHibernate,
Version=3.0.0.2001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4'
or one of its dependencies. The located assembly's manifest definition does
not match the assembly reference.
I checked references within the projects and all of them use a newer version of NHibernate. The problem was a bit more latent. Convention over configuration brought me to utilising FluentNHibernate, which is a small library that can configure NH. As it turned out, FNH also depends on NH internally. So I end up referencing and using NH twice: (1) from the project and (2) from FNH.
Solution
To resolve the problem I had to put a binding redirect in the config file. Such operation will take some time if performed manually, however NuGet does it in a matter of few clicks. I just need to run Add-BindingRedirects command.
PM> Add-BindingRedirects
Name : NHibernate
Culture : neutral
PublicKeyToken : aa95f207798dfdb4
ProcessorArchitecture :
NewVersion : 3.0.0.4000
OldVersion : 0.0.0.0-3.0.0.4000
CodeBaseHref :
CodeBaseVersion :
PublisherPolicy :
It will automatically update app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.4000" newVersion="3.0.0.4000"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
And after this operation all my tests are running successfully in the build machine environment.
If you enjoyed this post, make sure you subscribe to my RSS feed!
7a9451c6-b37b-432d-b425-36186f748963|0|.0
NuGet package manager is a free open source package management system that allows developers to add, remove and update references to 3rd party libraries in .NET projects. It also can update .config files to reflect changes in references. One usually needs to be connected to the internet, as NuGet is linked to Microsoft online centralized package storage. A package is a set of data and metadata files that are needed for 3rd party library or tool integration.
NuGet uses Powershell console and Microsoft Visual Studio 2010 (Powershell needs to be installed separately, here is the link).
Okay, that’s basic theory. Let’s start doing some work! As a sample, I will be adding references to a CHB.Container, which is a small Inversion of Control project that I am using within my solution. The base IoC container I will be using is Castle Windsor. This is due to a number of reasons that go beyond the topic of this post. So, theoretically, without NuGet I will be doing the following steps:
- Download the latest version of Castle Windsor from sourceforge
- Unzip the files
- Do the security checks
- Find libraries that target proper .NET
- Copy files to some resource folder in the solution
- In the CHB.Container project add references to Castle.Core.dll and Castle.Windsor.dll from location defined in step 5
Seems to be easy, erm? Let’s see how I can make a shortcut.
To kick off with NuGet I go to Tools > Extension Manager in MS VS 2010. Then, I search for NuGet in the online gallery, download and install it. After the VS restart, I can begin useing the package manager.
So far so good. Now, let's add references to CHB.Container. I will perform these steps:
- Open Package Manager Console from View -> Other Windows
- Select CHB.Container as a default project
- Type install-package Castle.Windsor in the console window
Hint: IntelliSense can be used with TAB key. Try pressing TAB after install-package and see the whole list of available packages.
The output will look something like this:
PM > Install-Package Castle.Windsor
'Castle.Core (= 2.5.2)' not installed.
Attempting to retrieve dependency from source...
Done
Successfully installed 'Castle.Core 2.5.2'
Successfully installed 'Castle.Windsor 2.5.2'
Successfully added 'Castle.Core 2.5.2' to CHB.Container
Successfully added 'Castle.Windsor 2.5.2' to CHB.Container
All the needed references were added and the project is ready to rock!
Hint: to uninstall the package use uninstall-package command.
Lastly, a few considerations:
- Screencasts can be found in the Getting Started section on the official website.
- NuGet downloads the whole package, whereas just a few files are used. In my sample solution I am using NHibernate, FluentNHibernate, log4net and Castle Windsor and the total package folder weight climbs just a few inches above 61 MB, which is, of course, a lot to be sent to a repository.
- It is a new project, do expect to catch some small bugs here and there
Summary
NuGet is a package management system that simplifies developer’s life. Even though it has some drawbacks, it really saves much time. Use it!
If you enjoyed this post, make sure you subscribe to my RSS feed!
1e8cb564-4387-4e71-8138-eba0dc28cb75|0|.0