I have a ClickOnce package I am trying to deploy that loads dll's on the fly, like plugins. I took advice from another blog post and put them in separate app domains. It runs great on my dev box, but once I publish and try to run, I get a SecurityException that says:
Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
I have given the app full trust for ClickOnce security settings. Here is the code I am calling to setup the AppDomain:
private static AppDomain CreateAppDomain(string domainName, string fileName, string configFileName)
{
var setup = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
ApplicationName = Path.GetFileName(fileName),
CachePath = AppDomain.CurrentDomain.BaseDirectory,
ConfigurationFile = configFileName,
PrivateBinPath = Path.GetDirectoryName(fileName),
ShadowCopyDirectories = Path.GetDirectoryName(fileName),
ShadowCopyFiles = "true"
};
return AppDomain.CreateDomain(domainName, AppDomain.CurrentDomain.Evidence, setup);
}
public static object ExecuteModule(string moduleName, object[] parameters)
{
var exePath = Assembly.GetExecutingAssembly().Location;
var filePath = string.Format(@"{0}\Modules\Module.{1}.dll",
exePath.Substring(0, exePath.LastIndexOf(@"\")),
moduleName);
var domain = CreateAppDomain(moduleName, filePath, string.Empty);
var moduleManager =
(ModuleManager)
domain.CreateInstanceAndUnwrap(
AssemblyName.GetAssemblyName(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"ModuleLoader.dll")).FullName,
typeof(ModuleManager).FullName);
domain.AssemblyResolve += ((sender, args) =>
{
var appDomain = sender as AppDomain;
var moduleFolder = appDomain.RelativeSearchPath;
var path = Path.Combine(moduleFolder, string.Format("{0}.dll", args.Name.Split(',')[0]));
return appDomain.Load(AssemblyName.GetAssemblyName(path));
});
return moduleManager.FetchModuleEntryPoint(filePath, domain, parameters);
}
Any ideas?