I am building a plugin-type system with each plugin represented as a DLL. I would like to be able to reload them without stopping the main application. This means that they must be loaded at runtime, without pre-built links between them (do a filesearch for dlls and load them). I have this set up using Assembly.LoadFile(filename), however, when I try to use File.Copy to replace the DLL it throws an exception, saying something akin to 'file in use'. I have tried using AppDomain, loading all plugins through this secondary domain, and unloading it before reloading, but this throws the same exception.
My current code:
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
}
Generally you need to unload the AppDomain for the communication.
If you want to prevent the mentioned error you simply can load your dll by using Assembly.Load(Byte[]).
You can also use the Managed Extensibility Framework which will save you a lot of work.
Assembly.Load issue solution
Assembly loaded using Assembly.LoadFrom() on remote machine causes SecurityException
Loading plugin DLLs into another AppDomain is the only solution - so you are on the right path. Watch out for leaking object from second app domain to main one. You need to have all communication with plugins to be happening in plugin's AppDomain.
I.e. returning plugin's object to main code will likely leak plugin's Assembly usage to main AppDomain.
Start with very simple code completely in plugin's AppDomain like "load assembly and create class, but don't return anything to main Domain". Than expand usage when you get more understanding on communication between AppDomains.
Note: unless you doing it for educational purposes using existing system (i.e. MEF) is better.
You could do something like this ...
if (pluginAppDomain != null)
{
AppDomain.Unload(pluginAppDomain);
}
//for each plugin
pluginAppDomain = AppDomain.CreateDomain("Plugins Domain");
x = pluginAppDomain.CreateInstanceFromAndUnwrap("Plugin1.dll", "Namespace.Type");
You should not reference the plugins in your main app directly. Put them in separate project/s and reference them through an interface.
Related
Problem
CSharpCodeProvider can be used to compile source .cs files into an assembly.
However, the assembly is automatically loaded into the AppDomain.CurrentDomain by default. In my case, this is a problem because I need to be able to re-compile the assembly again during runtime, and since it's already loaded in the CurrentDomain, I can't unload that, so I'm stuck.
I have looked through the docs and there seems to be no way to set the target app domain. I have also tried searching it on Google and only found answers where Assembly.Load was used, which I don't think I can use because I need to compile from raw source code, not a .dll
How would one go about doing this? Are there any alternatives or workarounds?
Main program
using (var provider = new CSharpCodeProvider())
{
param.OutputAssembly = "myCompiledMod"
var classFileNames = new DirectoryInfo("C:/sourceCode").GetFiles("*.cs", SearchOption.AllDirectories).Select(fi => fi.FullName).ToArray();
CompilerResults result = provider.CompileAssemblyFromFile(param, classFileNames);
Assembly newAssembly = result.CompiledAssembly // The assembly is already in AppDomain.CurrentDomain!
// If you try compile again, you'll get an error; that class Test already exists
}
C:/sourceCode/test.cs
public class Test {}
What I tried already
I already tried creating a new AppDomain and loading it in there. What happens is the assembly ends up being loaded in both domains.
// <snip>compile code</snip>
Evidence ev = AppDomain.CurrentDomain.Evidence;
AppDomain domain = AppDomain.CreateDomain("NewDomain", ev);
domain.Load(newAssembly);
The answer was to use CSharpCodeProvider().CreateCompiler() instead of just CSharpCodeProvider, and to set param.GenerateInMemory to false. Now I'm able to see line numbers and no visible assembly .dll files are being created, and especially not being locked. This allows for keeping an assembly in memory and reloading it when needed.
I'm working on a sort of plugin system for a Discord bot. Right now i'm on macOS using Mono as my runtime and I've been looking at using seperate AppDomains to load and manage each of the plugins. The plugins themselves do have dependencies but these are already loaded in the main appdomain. So here lies my problem:
I've attempted to iterate over the loaded assemblies in the current domain and load them into my separate AppDomain so the plugin's dependencies are already loaded in. This results in a FileNotFoundException when trying to load said dependencies (even though I point them to the correct path in the exe directory).
I've tried without doing that and just going through and loading the plugin dll's into a separate AppDomain and seeing if it'll resolve automatically. Still throws a FileNotFoundException.
I tried manually creating an array in order of the dependendies (in my application's case, Newtonsoft.Json -> DSharpPlus -> Bot.CommandManager). I can't even load Newtonsoft.Json due to a missing dependency.
Now, I'm loading the raw bytes into a stream reader and passing that into AppDomain.Load to make sure that the FileNotFoundException comes from a lack of dependency and not the actual file being missing. The file is there, still throws FileNotFoundException on domain.Load
The only way I've been able to successfully load the plugins is just using Assembly.Load and not worrying about AppDomains which works great until I update something and want to unload/reload the plugin. Which is where I need to use AppDomain. I'm kind of lost, here's my current iteration of the code. It's probably extremely unnecessary to have a separate AppDomain for each plugin but I'm rolling with that for now.
private void SetupAppDomain()
{
string path = Directory.GetCurrentDirectory() + "/modules/";
Console.WriteLine(path);
List<string> installedModules = new List<string>();
string[] files = Directory.GetFiles(path);
foreach(string modulePath in files)
{
try
{
AppDomain domain = AppDomain.CreateDomain(Path.GetFileName(modulePath), AppDomain.CurrentDomain.Evidence);
StreamReader reader = new StreamReader(modulePath, System.Text.Encoding.GetEncoding(1252), false);
byte[] b = new byte[reader.BaseStream.Length];
reader.BaseStream.Read(b, 0, System.Convert.ToInt32(reader.BaseStream.Length));
domain.Load(b); //this throws the FileNotFoundException
Assembly[] a = domain.GetAssemblies();
int index = 0;
for (int i = 0; i < a.Length; i++)
{
if(a[i].GetName().Name + ".dll" == Path.GetFileName(modulePath))
{
index = i;
break;
}
}
installedModules.Add(a[index].GetName().Name);
}
catch(Exception ex)
{
throw ex;
}
}
}
Worst case scenario, I can just go back to the old way of doing it and just have to restart the bot when I want to reload assemblies.
I need to check the time amount to run GetTypes() after loading the dll.
The code is as follows.
Assembly assem = Assembly.LoadFrom(file);
sw = Stopwatch.StartNew();
var types1 = assem.GetTypes();
sw.Stop();
double time1 = sw.Elapsed.TotalMilliseconds;
I'd like to unload and reload the dll to check the time to spend in running GetTypes() again.
How can I unload it? assem = null is good enough?
Is there an explicit way to call garbage collector to reclaim the resource allocated to assem?
Can you use another AppDomain?
AppDomain dom = AppDomain.CreateDomain("some");
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = pathToAssembly;
Assembly assembly = dom.Load(assemblyName);
Type [] types = assembly.GetTypes();
AppDomain.Unload(dom);
Instead of using LoadFrom() or LoadFile() you can use Load with File.ReadAllBytes(). With this it does not use the assembly file but will read it and use read data.
Your code will then look like
Assembly assem = Assembly.Load(File.ReadAllBytes(filePath));
sw = Stopwatch.StartNew();
var types1 = assem.GetTypes();
sw.Stop();
double time1 = sw.Elapsed.TotalMilliseconds;
From here We cannot unload the file unless all the domains contained by it are unloaded.
Hope this helps.:)
Unfortunately you can not unload an assembly once it is loaded. But you can unload an AppDomain. What you can do is to create a new AppDomain (AppDomain.CreateDomain(...) ), load the assembly into this appdomain to work with it, and then unload the AppDomain when needed. When unloading the AppDomain, all assemblies that have been loaded will be unloaded. (See reference)
To call the garbage collector, you can use
GC.Collect(); // collects all unused memory
GC.WaitForPendingFinalizers(); // wait until GC has finished its work
GC.Collect();
GC calls the finalizers in a background thread, that's why you have to wait and call Collect() again to make sure you deleted everything.
You can't unload assembly from the current AppDomain. But you can create new AppDomain, load assemblies into it, execute some code inside new AppDomain and then unload it. Check the following link: MSDN
If you only want to load the initial assembly without any of its dependent assemblies, you can use Assembly.LoadFile in an AppDomain, and then unload the AppDomain when done.
Create a loader class to load and work with the assembly:
class Loader : MarshalByRefObject
{
public void Load(string file)
{
var assembly = Assembly.LoadFile(file);
// Do stuff with the assembly.
}
}
Run the loader in a separate app domain like this:
var domain = AppDomain.CreateDomain(nameof(Loader), AppDomain.CurrentDomain.Evidence, new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(typeof(Loader).Assembly.Location) });
try {
var loader = (Loader)domain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);
loader.Load(myFile);
} finally {
AppDomain.Unload(domain);
}
Assembly cannot be unloaded unfortunately, and moreover - if you use appdomains - then it will prevent you to communicate with api's / assemblies of your main application.
Best description on problem can be found here:
Script Hosting Guideline
http://www.csscript.net/help/Script_hosting_guideline_.html
If you want to run C# code without communication to your main application - then best approach is to integrate C# scripting API:
https://github.com/oleg-shilo/cs-script/tree/master/Source/deployment/samples/Hosting/Legacy%20Samples/CodeDOM/Modifying%20script%20without%20restart
And for integration you will need following packages:
C# script:
http://www.csscript.net/CurrentRelease.html
Visual studio extension:
https://marketplace.visualstudio.com/items?itemName=oleg-shilo.cs-script
If however you want to communicate from your C# script to your application - then using same appDomain with assembly name constantly changing is only way at the moment - but that unfortunately eats ram and disk space.
Code sample how to do it can be done - can be found from here:
https://github.com/tapika/cppscriptcore
CsScriptHotReload.sln
And here is demo video:
https://drive.google.com/open?id=1jOECJj0_UPNdllwF4GWb5OMybWPc0PUV
I am working on some software that will dynamically build menu items for certain dlls so that we can load components in dynamically based on what dlls are available on the users machine. Any dlls that I want to load have been flagged with an Assembly Attribute in the AssemblyInfo.cs file and how I determine whether or not I want to build a menu item for that dll. Here is my method so far:
private void GetReportModules() {
foreach (string fileName in Directory.GetFiles(Directory.GetCurrentDirectory())) {
if (Path.GetExtension(fileName) == ".dll" || Path.GetExtension(fileName) == ".exe") {
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(fileName);
object[] attributes = assembly.GetCustomAttributes(typeof(ReportNameAttribute), false);
if (attributes.Count() > 0) {
ReportNameAttribute reportNameAttribute = attributes[0] as ReportNameAttribute;
Type type = assembly.GetType(reportNameAttribute.BaseType);
MenuItem customReportsMenuItem = new MenuItem();
customReportsMenuItem.Header = reportNameAttribute.ReportName;
ReportsMenuItem.Items.Add(customReportsMenuItem);
customReportsMenuItem.Click += (s, ev) => {
var obj = Activator.CreateInstance(type);
type.InvokeMember("Show", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, obj, null);
};
}
}
}
}
For the most part its working fine, I am getting the dlls that I am expecting back out and am creating my menu items fine. The problem is that in order to check for the attribute I first need to load the assembly using Reflection. Some of the other local dlls are throwing errors when I try to load them about missing dependencies or he module was expected to contain an assembly manifest. Is there a way I can safely check to see if an assembly CAN be loaded before I actually do so? (sounds stupid as I write it out). Any thoughts on the problem I'm running into or better suggestions for how to accomplish what I'm trying here? Feeling a little bit in over my head.
You can create a separate AppDomain, try to load the assemblies there, send the results back, and unload the AppDomain. This way you do not change your current AppDomain with 'garbage' of any loaded assemblies.
One way would be to make use of a try catch block. If it throw's an exception, you're not interested...
EDIT:
MSDN explains clearly the type of exceptions LoadFrom can throw. FileLoadException looks likely in your case.
I'm sure there is code out there that carried on after a catch. For example a logging framework. I would not want my framework to catch an exception and make my executable stop etc, i'd want it to smother the exception. My application should not fail just because a line of log miss fired.
You can try the Unmanaged Metadata API (http://msdn.microsoft.com/en-us/library/ms404384.aspx) or the Common Compiler Infrastructure Metadata API (http://ccimetadata.codeplex.com/) as alternatives to plain reflection.
The Code as below which i'm trying to load a dll dynamically is not working.
AppDomain appDomain = AppDomain.CreateDomain("DllDomain");
Assembly a = appDomain.Load(fileName);
//Assembly a = Assembly.LoadFrom(fileName);
objType = a.GetType(className);
obj = a.CreateInstance(className);
object[] args = new object[1];
args[0]=(object) "test";
object ret = objType.InvokeMember("Perform", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, args);
string output = ret.ToString();
obj = null;
AppDomain.Unload(appDomain);
this is the code i am using inside a WCF service but still it does not work.
Heard that we can acheive using 'Shadow Copying' in AppDomain. But i dont know anything about 'Shadow Copying' and how to implement the same in the above code.
Please provide working code as example for 'Shadow Copying'.
-B.S.
You can load assemblies into an application domain but you cannot unload them from that domain.
However, in one application domain you can create a second application domain and load an assembly into the second application domain. Later you can then choose to unload the second application domain which in turn unloads the assembly that you loaded into the second application domain.
This is the basic principle. In practice you will find a number of obstacles (they changed through the versions of .NET) to be resolved in particular when you set up some form of communication between the application domains.
Providing working code here would probably be too big in size.