Skip to content

Instantly share code, notes, and snippets.

@alexischr

alexischr/bug46473.cs Secret

Last active Feb 15, 2018
Embed
What would you like to do?
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