If you’ve worked with NuGet on a large solution, you may have run into problems where you end up with inconsistent binding redirects, pointing to different versions of the same assembly. This can cause weird runtime errors with FileNotFound exceptions for old versions of an assembly.

So here’s a quick LINQ script I knocked up in LINQPad to help discover if you’ve got any version mismatches. Just replace your source folder and the name of the NuGet package you want to check for. In this example, I’m looking for binding redirects for Newtonsoft.Json

from p in Directory.EnumerateFiles(@"c:\code\myproject", "*.config", SearchOption.AllDirectories)
let xn = (XNamespace)"urn:schemas-microsoft-com:asm.v1"
let xdoc = XDocument.Load(p)
from da in xdoc.Descendants(xn + "dependentAssembly")
let name = da.Element(xn + "assemblyIdentity").Attribute("name").Value
let oldVersion = da.Element(xn + "bindingRedirect")?.Attribute("oldVersion").Value
let newVersion = da.Element(xn + "bindingRedirect")?.Attribute("newVersion").Value
let info = new { p, name, oldVersion, newVersion }
where name.Contains("Newtonsoft")
group info by $"{info.name}|{info.newVersion}"

By the way, this is a great example of when the LINQ Query expression syntax is a lot easier to use than chained extension methods.

Want to learn more about LINQ? Be sure to check out my Pluralsight course More Effective LINQ.
Vote on HN
comments powered by Disqus