Updating Assembly Redirects with NuGet
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 '
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.
Other Posts you might also like
The Voices of Reason
# re: Updating Assembly Redirects with NuGet
# re: Updating Assembly Redirects with NuGet
# re: Updating Assembly Redirects with NuGet
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"
# re: Updating Assembly Redirects with NuGet
To update all projects use an asterisk Add-BindingRedirect *
# 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).
# 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.
# re: Updating Assembly Redirects with NuGet
Any way to add in assemblies to app.config that are not already present?
# 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.
# 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!
# 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!
# 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
# re: Updating Assembly Redirects with NuGet
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.