2012-05-22 205 views
2

我有以下插件:運行代碼

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(); 
     } 
    } 
} 

最後的代碼加載插件,並呼籲它的執行方法:

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

+0

http://msdn.microsoft.com/en-us/library/ms242202.aspx –

+0

@拉斐爾奧爾索斯:我禁用了Visual Studio的託管過程中,仍然獲取LearningPlugins.exe作爲AppDomain。我的問題是:爲什麼不是我使用的專門創建的PluginDomain? – lowerkey

回答

0

當以調試模式啓動應用程序時,vshost被添加到友好名稱。 解決方法是使用正則表達式的vshost

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

目的,你也可以檢查AppDomain.FriendlyName issue

0

這是因爲你使用AppDomain.CurrentDomain.FriendlyName

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

默認的應用程序域的友好名稱是過程可執行

=> LearningPlugin.exe的文件名 你

所以你應該這樣做:

in Main:

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(); 
     }