Cannot load dll assembly in windows 10 (Exception from HRESULT: 0x80131515)

2

I was trying to load the compiled dll assembly of JSON.NET. However I get the following error message:

PS C:\Users\tamas\Desktop\garbage> Add-Type -path .\Newtonsoft.Json.dll
Add-Type : Could not load file or assembly 'file:///C:\Users\tamas\Desktop\garbage\Newtonsoft.Json.dll' or one of its d
ependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Add-Type -path .\Newtonsoft.Json.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

On a different computer, under windows 7 I was able to do the same thing without any problem. What could be the cause and the solution?

My .NET version:

PS C:\Users\tamas\Desktop\garbage> [System.Environment]::Version

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      30319  42000

PowerShell version:

PS C:\Users\tamas\Desktop\garbage> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  206
powershell
dll
scripting
asked on Stack Overflow Oct 8, 2016 by ThomasMX • edited Oct 8, 2016 by ThomasMX

2 Answers

3

Thanks to @martin-brandl 's suggeston I was able to figure out the problem (I actually had to modify his code, becuase it did not output anything ($global:error[0].Exception has no LoaderExceptions field.))

PS C:\Users\tamas\Desktop\garbage>
>> try
>> {
>>     Add-Type -Path .\Newtonsoft.Json.dll
>> }
>> catch
>> {
>>         write-host $global:error[0].Exception.InnerException
>> }
System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused th
e assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enabl
e CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please ena
ble the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

It turned out that since the dll file was from an external source, for safety reasons powershell threw an exception. Using th UnsafeLoadFrom(...) method I was able to load the dll assembly:

PS C:\Users\tamas\Desktop\garbage> [System.Reflection.Assembly]::UnsafeLoadFrom("c:\users\tamas\Desktop\garbage\Newtonso
ft.Json.dll") # absolute path required here!!

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Users\tamas\Desktop\garbage\Newtonsoft.Json.dll

More info on this issue can be found on the msdn site here .

answered on Stack Overflow Oct 8, 2016 by ThomasMX
1

I can't tell you which of the dependencies you are missing but I can tell you how to find it out. Just surround your Add-Type within a try-catch cand retrieve the LoaderException:

try
{
    Add-Type -Path .\Newtonsoft.Json.dll
}
catch
{
        $global:error[0].Exception.LoaderExceptions | % { Write-Host $_ }
}
answered on Stack Overflow Oct 8, 2016 by Martin Brandl

User contributions licensed under CC BY-SA 3.0