So I was fixing a few bug’s in an application that supports auto updating. Everything was perfect until I suddenly noticed that non of the machines with the application was updating anymore. so I log on to one of the machines and see the service isn't running on the machine either (the application is a windows service). Weird, no errors in the evenlog from the service either, but all my services can also be run from command so I give that a go.
Unhandled Exception: System.BadImageFormatException: Could not load file or assembly XXXXX.exe' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
File name: 'XXXXX.exe'
WTF ? hmm, looks like my app.config is screwed so I create a new one and try again.
that’s weird, I was 100% sure my application was set to Target Framework v3.5. I triede every single combination from 2.0, 3.0, 3.5, 3.5 client profile to 4.0. No matter what I did, it only worked when set to 4.0 AND .net 4.0 was installed on the client. But I don’t want 4.0 on all machines.
After ton of time on google, I realised something. When changing target framework the path to the dll’s in the project references keept pointing to C:\Windows\Microsoft.NET\Framework\v4.0.30319 or C:\Windows\Microsoft.NET\Framework64\v4.0.30319 depending on my target CPU type.
Even if I manuly removed all references and added them pointing to C:\Windows\Microsoft.NET\Framework\v2.0.50727 or C:\Windows\Microsoft.NET\Framework64\v2.0.50727 visual studio 2010 would accept it, but chance the path back to the v4.0.30319 directory.
THIS WAS DRIVING ME NUTS!
Then .. and gods know how I found it .. came a cross this page
and BAM it hit me, I had the exact same issue. I too had googled the problem with
Add-PSSnapin : Cannot load Windows PowerShell snap-in XXXXX because of the following error: Coul not load file or assembly 'file:///C:\XXXXX.dll' or one of its dependencies. This assembly i built by a runtime newer than the currently loaded runtime and cannot be loaded.
At line:1 char:13
+ Add-PSSnapin <<<< XXXXX
+ CategoryInfo : InvalidArgument: (XXXXX:String) [Add-PSSnapin], PSSnapInException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
and the all around solution you find to this problem is adding 1 or both of these 2 registry keys
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ Microsoft\.NET Framework:OnlyUseLatestCLR
Except, everywhere you find it they also warn about doing it, but noone seemed to have a solution. but there is on and its simple. if you just want to fix it for you own local powershell, just go to $pshome and create a file named powershell.exe.config and add this text to it
if you want to chance it for all users and all versions (32 bit and 64bit) create the above file in C:\windows\System32\WindowsPowerShell\v1.0 and C:\Windows\SysWOW64\WindowsPowerShell\v1.0