-
-
Save alexischr/240cfb8f91d5630fdeb165bef78d29d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
namespace CrossAppDomainServices | |
{ | |
public class PluginDependencies : MarshalByRefObject | |
{ | |
public void Init(ApplicationDependencies applicationDependencies) // racy exception here | |
{ | |
} | |
} | |
public class ApplicationDependencies : MarshalByRefObject | |
{ | |
} | |
class Program | |
{ | |
private static ApplicationDependencies _applicationDependencies; | |
static void Main(string[] args) | |
{ | |
_applicationDependencies = new ApplicationDependencies(); | |
InitializePlugins(); | |
} | |
private static void InitializePlugins() | |
{ | |
for (int i = 0; i < 8; i++) | |
{ | |
var assembly = Assembly.GetCallingAssembly(); | |
var pluginName = "Plugin" + i; | |
Console.WriteLine($"Start '{pluginName}'..."); | |
Task.Factory.StartNew(() => InitializePlugin(assembly, pluginName)); | |
} | |
Console.ReadKey(); | |
} | |
private static void InitializePlugin(Assembly assembly, string pluginName) | |
{ | |
try | |
{ | |
Console.WriteLine($"Creating AppDomain for '{pluginName}'..."); | |
var appDomain = AppDomain.CreateDomain(pluginName); | |
Console.WriteLine($"Creating '{pluginName}' dependencies..."); | |
var pluginDependencies = (PluginDependencies)appDomain.CreateInstanceAndUnwrap(assembly.GetName().Name, typeof(PluginDependencies).FullName); | |
Console.WriteLine($"Initializing dependencies for '{pluginName}'..."); | |
pluginDependencies.Init(_applicationDependencies); | |
Console.WriteLine($"'{pluginName}' inited."); | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine($"{pluginName} error: {ex.Message}\n{ex.StackTrace}\n{ex.GetType()}\n{ex.InnerException}"); | |
Console.Error.WriteLine($"{pluginName} error: {ex.Message}\n{ex.StackTrace}\n{ex.InnerException.GetType()}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment