简体   繁体   中英

Dynamic Assembly Loading in .Net 4.0

My problem begins with moving a .Net 2.0 application to .Net 4.0. The reason I had to do this was that Windows 8 does not enable the earlier .Net versions by default and my application cannot ask the user to enable it.

The application is a NPAPI plugin which uses .Net components via UnmanagedExports . I designed it as a low integrity application and therefore it has to reside in the users 'LocalLow' directory.

In my application I used a dynamic assembly loading mechanism to load several assemblies at runtime. I used the following method to load an assembly,

MyInterface Instance;

Assembly assembly = Assembly.LoadFrom(AssemblyFile);
Type type = assembly.GetType(Identifier); // Identifier is implementing the MyInterface 
Instance = Activator.CreateInstance(type) as MyInterface;

// Do something with the Instance

After modifying the project to .Net 4.0, I noticed that the plugin crashes when the binaries are placed inside the LocalLow directory ( It works in other places ). My next step was to create a minimalistic plugin with least possible code to figure out the issue. I noticed that the dynamic assembly loading failed with the following exception,

System.IO.FileLoadException: Could not load file or assembly '<assemblyPath>' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) --->

System.NotSupportedException: 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=131738 for more information.

I tried the following approaches to create a separate domain and load the assemblies but with no luck,

Adding the configuration 'loadFromRemoteSources' did not work either. It seems that the .Net component does not load .dll.config files. ( Could be because of UnmanagedExporting )

My questions are,

  • Is it possible to dynamically load an assembly from LocalLow?
  • Does the new CAS policy in CLR 4.0 apply to LocalLow as well? From what I understood so far it should affect only assemblies loaded over the network
  • Is there any other way to overcome this issue?

虽然它没有具体解决您的LocalLow问题,但如果您能够从目录中“读取文件”,您可以使用此处详述的“解决方法”: 如何让LabView停止锁定我的.NET DLL?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM