Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Updating Assembly Redirects with NuGet


:P
On this page:

Here’s a little NuGet gem that  I didn’t know and just found out about today: You can get NuGet to explicitly re-write your reassembly redirects in your .config files based on the installed NuGet Packages in the project.

You can use the following command from the Package Manager console:

PM> Get-Project –All | Add-BindingRedirect

This recreates all those assembly redirects that are defined in your web.config or app.config file for a solution and updates them to match the versions from the various packages that are installed in each project. IOW it refreshes the assembly redirects to the actually installed packages of each project.

Right on! This is something I run into quite frequently and this simple command fixes the problem easily!  If you want this to work for an individual project just remove the –all flag.

Thanks to @maartenballiauw and @tsimbalar who pointed out this command to me when I was griping on Twitter about mismatched assemblies after an update to the latest ASP.NET WebAPI and MVC packages in a couple of projects. If you get “Could not load file or assembly '' or one of its dependencies.” errors when you know you have the package and assembly referenced in your project, you’re likely running into a problem related to assembly versioning and need assembly redirects. NuGet automatically creates redirects for you, but versions can get out of sync when projects with cyclical dependencies are upgraded in a single solution.

Maarten also wrote up a blog post on this and I don’t want to take away from Maartens’s post here, and instead just link you to that for a good deal more information:

Could not load file or assembly… NuGet Assembly Redirects

I thought it was important enough to repost this here, since it’s a little known command that probably can benefit many people. I know it’s definitely a problem I run into a lot because I have a few component libraries that take dependencies on high level framework libraries that rev frequently, so it’s easy for things to get out of sync. This will help me tremendously in making sure that libraries are properly redirected.

Posted in .NET  NuGet  ASP.NET  

The Voices of Reason


 

Jon Atkinson
November 29, 2014

# re: Updating Assembly Redirects with NuGet

Hi Rick,

I also had this problem and spent a few hours resolving it.

This nuget command would've sorted it in no time. And, now I know how ;-)

Thanks for posting.

B. Clay Shannon
December 01, 2014

# re: Updating Assembly Redirects with NuGet

So why isn't this just automatically done? Is there any reason why you wouldn't want this?

Rick Strahl
December 02, 2014

# re: Updating Assembly Redirects with NuGet

@Clay - NuGet only works off a single package and updates redirects only for that single package, it doesn't look at all of the dependencies when it installs individual packages. This command looks at the whole project/solution and looks at all assembly references. I think the reason it doesn't happen on everything is that this can also potentially break stuff if you explicitly set assembly redirects or if you want to stick with a particular verions for a given assembly.

bloumoord
July 06, 2016

# re: Updating Assembly Redirects with NuGet

Is there a command line version of this?
I use this command to update packages in my solution, but found that binding redirects in web.config / app.config files are not updated as it would be when updating in VS Package Manager Console
"nuget update mySolution.sln"

Simon Hughes
March 27, 2017

# re: Updating Assembly Redirects with NuGet

To update all projects use an asterisk Add-BindingRedirect *


Chris Nash
April 19, 2017

# re: Updating Assembly Redirects with NuGet

Echoing @bloumoord. Is there a way to do this outside the package manager console? It would fix a lot of issues for us if we could run this as part of our CI (Jenkins pipeline).


Ed Gillett
June 28, 2017

# re: Updating Assembly Redirects with NuGet

I actually love you Rick. Thank you for this. Still very handy in 2017 to fix the rats nest of dependency mess my web.config had built up.


Raul
July 24, 2017

# re: Updating Assembly Redirects with NuGet

Any way to add in assemblies to app.config that are not already present?


Sphynx
July 27, 2018

# re: Updating Assembly Redirects with NuGet

I'm guessing this only works for existing dependentAssembly elements in the runtime section of the web.config. The problem is: I have no idea how those dependentAssembly elements end up there as I've started a fresh MVC project, it has some already in there (presumably in the project type template) but then adding a new NuGet package which does have redirectability (e.g. iTextSharp) does nothing to the file, neither does "Restore NuGet Packages", neither does a complete rebuild and neither does this Package Manager script. Where on earth do these elements come from?

I was expecting to see this appear in the runtime section when installing iTextSharp and disappear when uninstalling it (or running the above script since that doesn't work).

<dependentAssembly>
	<assemblyIdentity name="itextsharp" publicKeyToken="8354ae6d2174ddca" culture="neutral" />
	<bindingRedirect oldVersion="0.0.0.0-5.5.9.0" newVersion="5.5.9.0" />
</dependentAssembly>

I've seen it in another project which has worked and no one in our team has written it.


Nick
June 11, 2020

# re: Updating Assembly Redirects with NuGet

I've reference this page and used this command a lot. Thanks so much for posting this and keep it up!


Cameron Wilby
July 27, 2021

# re: Updating Assembly Redirects with NuGet

You know, I've probably stumbled on your site about 3 times over the last 10 years, and each time has been a huge value add.

Thank you for writing!


mark
November 15, 2023

# re: Updating Assembly Redirects with NuGet

Running this gave me this error:

Add-BindingRedirect : Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401) At line:1 char:20

Get-Project -all | Add-BindingRedirect
CategoryInfo : NotSpecified: (:) [Add-BindingRedirect], FileLoadException
FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.AddBindingRedirectCommand

To solve this:

I was facing the same issue when attempting to run this command, but managed to run it by creating the DWORD variable LoaderOptimization with 1 for value under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework, then restarting Visual Studio and running the command in the Package Manager Console again.

source: https://stackoverflow.com/questions/76049195/add-bindingredirect-loading-this-assembly-would-produce-a-different-grant-set

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024