Reputation: 40032
I have the below code in a .Net 4 Winforms app which loads an assembly. All files are on a C:. There are numerous DLL's which work fine but two error with the following:
An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
This only seems to be a problem on some PCs
Here is the code:
strDLLs = Directory.GetFileSystemEntries(strPath, "*.dll")
For intIndex = 0 To strDLLs.Length - 1
Try
objDLL = [Assembly].LoadFrom(strDLLs(intIndex))
ExamineAssembly(objDLL, strInterface, Plugins)
Catch e As Exception
' MsgBox("Error whilst loading Library: " & strDLLs(intIndex) & ". Reported Error was:" & vbCrLf & e.ToString)
End Try
Next
Upvotes: 8
Views: 19134
Reputation: 36
Thanks for the great explanation. I want to add some cross-reference information, how this issue arises on VS2015, where the helpful message is not shown. I use a compiled assembly which itself includs an assembly. The included assembly has the internet flag set. The result is, that the application closes without notice. When using VS 2022, I get the helpful message of this initial post. When using VS 2015, there is the following error (translated from German):
Exception of type "System.IO.FileLoadException" happened in an unknown module
The resulting stack trace is as follows:
clr.dll!RaiseTheExceptionInternalOnly(class Object *,int,int) Unbekannt
clr.dll!UnwindAndContinueRethrowHelperAfterCatch(class Frame *,class Exception *) Unbekannt
clr.dll!isValidTokenForTryResolveToken(class CEEInfo *,struct CORINFO_RESOLVED_TOKEN *) Unbekannt
clrjit.dll!Compiler::impResolveToken(const unsigned char * addr, CORINFO_RESOLVED_TOKEN * pResolvedToken, CorInfoTokenKind kind) Zeile 282 C++
clrjit.dll!Compiler::impImportBlockCode(BasicBlock * block) Zeile 10497 C++
clrjit.dll!Compiler::impImportBlock(BasicBlock * block) Zeile 13246 C++
clrjit.dll!Compiler::impImport(BasicBlock * method) Zeile 14195 C++
[Inlineframe] clrjit.dll!Compiler::fgImport() Zeile 6060 C++
clrjit.dll!Compiler::compCompile(void * * methodCodePtr, unsigned long * methodCodeSize, unsigned int compileFlags) Zeile 2491 C++
clrjit.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned long * methodCodeSize, unsigned int compileFlags, CorInfoInstantiationVerification instVerInfo) Zeile 3615 C++
clrjit.dll!Compiler::compCompile(CORINFO_METHOD_STRUCT_ * methodHnd, CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned long * methodCodeSize, unsigned int compileFlags) Zeile 3086 C++
clrjit.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd, CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, void * * methodCodePtr, unsigned long * methodCodeSize, unsigned int compileFlags, void * inlineInfoPtr) Zeile 4057 C++
clrjit.dll!CILJit::compileMethod(ICorJitInfo * compHnd, CORINFO_METHOD_INFO * methodInfo, unsigned int flags, unsigned char * * entryAddress, unsigned long * nativeSizeOfCode) Zeile 180 C++
clr.dll!invokeCompileMethodHelper(class EEJitManager *,class CEEInfo *,struct CORINFO_METHOD_INFO *,class CORJIT_FLAGS,unsigned char * *,unsigned long *) Unbekannt
clr.dll!invokeCompileMethod(class EEJitManager *,class CEEInfo *,struct CORINFO_METHOD_INFO *,class CORJIT_FLAGS,unsigned char * *,unsigned long *) Unbekannt
clr.dll!CallCompileMethodWithSEHWrapper(class EEJitManager *,class CEEInfo *,struct CORINFO_METHOD_INFO *,class CORJIT_FLAGS,unsigned char * *,unsigned long *,class MethodDesc *) Unbekannt
clr.dll!UnsafeJitFunction(class MethodDesc *,class COR_ILMETHOD_DECODER *,class CORJIT_FLAGS) Unbekannt
clr.dll!MethodDesc::MakeJitWorker(class COR_ILMETHOD_DECODER *,class CORJIT_FLAGS) Unbekannt
clr.dll!MethodDesc::DoPrestub(class MethodTable *) Unbekannt
clr.dll!_PreStubWorker@8() Unbekannt
clr.dll!_ThePreStub@0() Unbekannt
03f0d320() Unbekannt
[Die unten aufgeführten Frames sind möglicherweise nicht korrekt und/oder fehlen.]
[Externer Code]
Upvotes: 0
Reputation: 5692
Piggybacking on Jon, I had this problem but with lots of assemblies in many different folders. I downloaded Streams from Sysinternals to unblock the files en masse. I found a good discussion on Super User about this topic.
Streams from Sysinternals Super User discussion
Upvotes: 1
Reputation: 11029
This is how I managed to get it to work, without resorting to any clicking on client side:
var appDomain = AppDomain.CreateDomain(assemblyName);
var assembly = appDomain.Load(File.ReadAllBytes(assemblyName));
Keep in mind if you CreateDomain with Evidence parameter, you will get the 'This method uses CAS policy, which has been obsoleted by the .NET Framework.' message.
Alternatively, you can set up a proper sandbox:
http://msdn.microsoft.com/en-us/library/bb763046.aspx http://blogs.msdn.com/b/shawnfa/archive/2005/08/08/449050.aspx
Upvotes: 2
Reputation: 40032
Well turns out the issue is because the file was possibly downloaded from the internet.
To fix Right Click -> Properties -> Unblock
Upvotes: 25