2

我有以下插件:

IPlugin.cs:

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

独立插件.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。我想知道为什么PluginDomain不显示 AppDomain。即使我想为插件创建一个额外的 appdomain,似乎也使用了父 AppDomain

4

2 回答 2

0

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

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

vshost的目的
您还可以检查 AppDomain.FriendlyName问题

于 2012-05-22T11:29:44.657 回答
0

It's because you're using AppDomain.CurrentDomain.FriendlyName.

As explained in msdn ( http://msdn.microsoft.com/en-us/library/system.appdomain.friendlyname )

The friendly name of the default application domain is the file name of the process executable

=> LearningPlugin.exe for you

So you should make something like that

in Main :

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

in Standalone Plugin :

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();
        }
于 2012-05-22T11:39:26.760 回答