Trying to load incorrect version of dll – bindingRedirect bites

I got an error saying that application tries to load Npgsql, v. but it can’t find it. Whoops, missing dll. Or that’s what I thought. Checked output folder and references in project – reference is there, it’s copied. So what’s the problem? Then I noticed the version does not match. I have referenced, cause that was the version other dependencies were build against. So where does this newer version loading code coming from?

Sadly it took me quite some time to figure it out. First I was thinking there is some mismatch in dependencies, that I’ve messed something up when rebuilding Simple.Data.PostgreSql. Checked it, double checked it – all was perfectly fine. Then I used my favorite tool – search. There was just one line in whole solution referencing

  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
      <bindingRedirect oldVersion="" newVersion="" />

bindingRedirect – a way to tell that we should load newer library even if strongly named reference requires older version. What a bummer – how did it get there? I didn’t put it there myself, that’s for sure.

Then I remembered – first I installed Npgsql from Nuget and I picked latest version. And installation from Nuget modified my web.config file. But removing this dependency from Nuget did not remove config entry!

So lesson learned – Nuget is great tool, but pay attention to the changes it makes to your code or you may end up with some stupid errors. Easy to fix, but sometimes hard to pin point.