I create a system with plugins with each plugin presented as a DLL. I would like to be able to restart them without stopping the main application. This means that they must be loaded at runtime, without pre-built links between them (search for files for dll and load them). I set this with Assembly.LoadFile(filename)
, however, when I try to use File.Copy
to replace the DLL, it throws an exception saying something similar to "used file". I tried using AppDomain
by loading all plugins through this secondary domain and unloading it before rebooting, but this throws the same exception.
My current code is:
if (pluginAppDomain != null) AppDomain.Unload(pluginAppDomain); foreach (string s in Directory.GetFiles(path_to_new_DLLs)) { string name = s.Substring(s.LastIndexOf('\\') + 1); Console.WriteLine("Copying " + name); File.Copy(s, Path.Combine(current_directory, name), true); // Throws exception here } AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = Environment.CurrentDirectory; setup.ShadowCopyFiles = "true"; // I think this is where the problem is, maybe I'm forgetting to set something pluginAppDomain = AppDomain.CreateDomain("KhybotPlugin", null, setup); foreach (String s in Directory.GetFiles(Environment.CurrentDirectory, "*.dll")) { int pos = s.LastIndexOf('\\') + 1; Assembly dll = pluginAppDomain.Load(s.Substring(pos, s.Length - pos - 4)); // Elided... Load types from DLL, etc, etc }
source share