从自定义AppDomain运行代码

时间:2012-05-22 11:07:25

标签: c# appdomain

我有以下插件:

IPlugin.cs:

namespace StandalonePlugin
{
    public interface IPlugin
    {
        string Execute(string data);
    }
}

StandalonePlugin.cs:

namespace StandalonePlugin
{
    [Export(typeof(IPlugin))]
    public class StandalonePlugin : IPlugin
    {
        public string Execute(string data)
        {
            StringBuilder result = new StringBuilder();
            result.AppendFormat("Plugintype: {0}\r\n", this.GetType().FullName);
            result.AppendFormat("Data: {0}\r\n", data);
            result.AppendFormat("AppDomain: {0}\r\n", AppDomain.CurrentDomain.FriendlyName);
            result.AppendFormat("Current Dir: {0}\r\n", Environment.CurrentDirectory);
            result.AppendFormat("Assembly location: {0}\r\n", Assembly.GetExecutingAssembly().Location);

            return result.ToString();
        }
    }
}

最后代码加载插件并调用它的Execute方法:

namespace CustomAppDomain
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            setup.PrivateBinPath = @".";

            AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain", null, setup);

            string pathToDll = @"D:\Code\LearningPlugins\StandalonePlugin\bin\Debug";
            Type t = typeof(IPlugin);
            IPlugin plugin = (IPlugin)pluginDomain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

            Console.WriteLine(plugin.Execute("Test"));

            Console.WriteLine("<ENTER> to continue");
            Console.ReadLine();
        }
    }
}

这是输出:

> Plugintype: StandalonePlugin.StandalonePlugin 
> Data: Test 
> AppDomain:LearningPlugins.vshost.exe 
> Current Dir: D:\Code\LearningPlugins\LearningPlugins\bin\Debug 
> Assembly location: D:\Code\LearningPlugins\LearningPlugins\bin\Debug\StandalonePlugin.dll
> 
> <ENTER> to continue

现在提出问题:为什么AppDomain列表LearningPlugins.exe代替PluginDomain,如AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain", null, setup);中所述?

为了澄清,我不关心添加vshost。我想知道为什么不显示AppDomain PluginDomain。似乎使用了父AppDomain,即使我想为插件创建一个额外的appdomain

2 个答案:

答案 0 :(得分:0)

在调试模式下启动应用程序时,会将vshost添加到友好名称中。 解决方法是使用正则表达式

string myName = Regex.Replace(AppDomain.CurrentDomain.FriendlyName, 
    @"(?<basename>.*).vshost(?<extension>\..*)", "${basename}${extension}");

vshost的目的 您也可以查看AppDomain.FriendlyName issue

答案 1 :(得分:0)

这是因为你正在使用AppDomain.CurrentDomain.FriendlyName

如msdn(http://msdn.microsoft.com/en-us/library/system.appdomain.friendlyname

中所述
  

默认应用程序域的友好名称是文件名   过程可执行文件

=&GT; LearningPlugin.exe为你

所以你应该做那样的事情

主页中的

Console.WriteLine(plugin.Execute("Test", pluginDomain));

在独立插件中:

public string Execute(string data, AppDomain childDomain)
        {
            StringBuilder result = new StringBuilder();
            result.AppendFormat("Plugintype: {0}\r\n", this.GetType().FullName);
            result.AppendFormat("Data: {0}\r\n", data);
            result.AppendFormat("AppDomain: {0}\r\n", childDomain.FriendlyName);//CHANGE HERE
            result.AppendFormat("Current Dir: {0}\r\n", Environment.CurrentDirectory);
            result.AppendFormat("Assembly location: {0}\r\n", Assembly.GetExecutingAssembly().Location);

            return result.ToString();
        }