How to load an assembly under a different appdomain

I have a plugin like system for my C# application. I am running into the issue where plugins won't load from the /plugins directory but they will if the plugins are in the same area as the executable. I am also trying to sandbox these plugins as my app (shouldn't be, but may be) ran as an admin and I don't want people to be able to write malicious plugins.

foreach (var l_Pair in s_TypesAssembly) { var l_Type = l_Pair.Key; var l_AssemblyPath = l_Pair.Value; var s_AdSetup = new AppDomainSetup(); s_AdSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"plugins\"; s_AdSetup.PrivateBinPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"plugins\"; var s_PermissionSet = new PermissionSet(PermissionState.None); s_PermissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); s_PermissionSet.AddPermission(new FileIOPermission(PermissionState.Unrestricted)); var s_FullTrustAssembly = typeof(PluginManager).Assembly.Evidence.GetHostEvidence<StrongName>(); var s_SandboxDomain = AppDomain.CreateDomain("Sandbox", null, s_AdSetup, s_PermissionSet, s_FullTrustAssembly); var s_AssemblyName = AssemblyName.GetAssemblyName(l_AssemblyPath); var l_Assembly = s_SandboxDomain.Load(s_AssemblyName); var l_Plugin = (IPlugin)Activator.CreateInstance(s_SandboxDomain, l_Assembly.GetName().Name, l_Type.FullName); } 

This throws a FileNotFound exception, unless the module is also in the root directory where the executable is (not in /plugins) Otherwise it works fine.

by kiwidog via /r/csharp

Leave a Reply