Sunday, March 27, 2011

NuGet Packages and Dependencies

I admit that I've always liked the Ruby package management system, RubyGems. Until a few months ago I never even thought about a similar mechanism for the .Net Developer then along came NuGet. NuGet has the potential to really help push open source on the .NET platform and at the same time make the rigor of managing packages somewhat easier.

I am not saying NuGet isn't with it's pain points at times, but overall it's a fantastic tool.

One pet peeve I do have from time to time is dependencies within a package.

For instance Fluent NHibernate when installed through NuGet references NHibernate, Version=3.0.0.2001. I always try to use the latest and greatest versions of Libraries I work with. Whenever I build a web project referencing FluentNHibernate I get the following error bubbled up from StructureMap

StructureMap configuration failures:
Error: 170
Source: Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.2.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
Unable to find the exported Type's in assembly BasilBee.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. One or more of the assembly's dependencies may be missing.

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. (Exception from HRESULT: 0x80131040)
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. (Exception from HRESULT: 0x80131040)
File name: 'NHibernate, Version=3.0.0.2001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4'
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at StructureMap.Graph.TypePool.c__DisplayClass2.b__0(Assembly assembly) in c:\code\structuremap\Source\StructureMap\Graph\AssemblyScanner.cs:line 24


Luckily NuGet has a command for just this scenario which I discovered at David Ebbo's blog.

First you need to build your project and from the package manager console type:
PM> Add-BindingRedirect


This will add the minimal set of binding redirects to config to get everything working.

Once I ran the command my project starting running as expected.

Personally I think this is a great addition to the NuGet commands. Package Managers certainly can't be always up to date with the latest package references.[UPDATE]No longer relevant with the release of version 1.2

2 comments:

  1. We're going to make that automatic in the next version of nuget :).

    ReplyDelete
  2. David: That's good to hear. As I said overall it's a great tool for developers.

    ReplyDelete