0

I have a PowerShell module (.NET assembly) that references the Autofac v3.5.2 and Autofac Configuration v3.3.0 libraries. When I load this module in PowerShell:

PowerShell.exe -NoExit -Command "& {Import-Module -Name .\MyModule.dll }

PowerShell opens, but displays the error:

"Could not load file or assembly 'Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified."

However, making the same Autofac references from within a Forms or WPF app does not result in an error - everything loads correctly. Note that both the module and UI apps invoke AutoFac Configuration code - they aren't just making reference to those two libraries.

I double checked all the assembly references and they're all set to "Specific Version=False". What then about PowerShell is telling it to require a specific version of the assembly?

Jester
  • 56,577
  • 4
  • 81
  • 125
Dan
  • 1,215
  • 1
  • 10
  • 22
  • See http://stackoverflow.com/a/29242635/480982 – Thomas Weller Apr 16 '15 at 19:14
  • 1
    Better see [How to enable assembly bind failure logging](http://stackoverflow.com/questions/255669/how-to-enable-assembly-bind-failure-logging-fusion-in-net) instead. – Jester Apr 16 '15 at 19:24
  • Nice tip. It shows attempts to load Autofac 3.5 and 3.3. Still don't understand why. – Dan Apr 16 '15 at 20:22
  • It will attempt to load 3.3 and 3.5 because Autofac Configuration will have a dependency on Autofac 3.3 and probing will look for that first (you can use ILSpy to confirm the dependency). As to why the WinForms equivalent works, I'm less sure. Does your project have a policy redirect in App.config to forward 3.3 requests to 3.5? e.g. bindingRedirect olderVersion="x" newVersion="Y"? – ScheuNZ Apr 16 '15 at 20:39
  • This article describes how to apply a binding redirect that the PowerShell session will pick up (if you want to go down that path): http://stackoverflow.com/questions/26652204/powershell-assembly-binding-redirect-not-found-in-application-configuration-fi – ScheuNZ Apr 16 '15 at 20:45
  • Yes ScheuNZ, apparently NuGet? stuck a binding redirect in the app.config. Unfortunately putting that in the module's app.config file doesn't fix the problem, and it is impractical for me to modify the PowerShell.exe.config file, so I guess I'm stuck rolling back to Autofac 3.3. – Dan Apr 16 '15 at 21:45

1 Answers1

1

Based on your most recent comment, you might also have some luck handling AppDomain.CurrentDomain.AssemblyResolve as per the article linked below. I've only ever used this when creating proxies that load an appropriate x86 or x64 assembly at runtime, but it does appear you can use it for version forwarding too.

http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/

ScheuNZ
  • 911
  • 8
  • 19