Resolving Assembly Bindings in Powershell

After configuring PowerShell to use an assembly’s config file and successfully loading assemblies and their dependencies, I found that assemblies referenced by the assembly’s config file were not found by the loader, even though the same referenced assemblies were already loaded by the original assembly.

To understand what is going on during Assembly.Load and Assembly.LoadFrom, read this MSDN article explaining the different Load Contexts that .Net uses for the various Assembly.Load*** methods.

This answer on StackOverflow provides C# code implementing the AssemblyResolve event for the current AppDomain to retrieve assemblies that the runtime does not find directly.

The original code stores a list of assembly names and their actual paths, and the AssemblyResolver class loads an assembly if it is found in this list.

I added code to handle binding assemblies in pre-defined paths, and posted the resulting code under the AssemblyResolver package here.

To use this assembly resolver in PowerShell, first load the assembly containing the assembly resolver, define known assemblies by adding assembly filenames and/or assembly paths, and then load the assemblies you originally wanted to load.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: