2012-06-20 40 views
3

當我設置DisallowApplicationBaseProbing = true時,需要在創建的AppDomain上連接AssemblyResolve事件。我這樣做的原因是強制運行時調用它需要解析程序集的AssemblyResolve事件,而不是首先進行探測。這樣,另一個開發人員不能將MyDllName.dll粘貼到ApplicationBase目錄中,並覆蓋我想要在AssemblyResolve事件中加載的程序集。當DisallowApplicationBaseProbing = true時需要連接AssemblyResolve事件

有這樣做的問題是以下...

class Program 
    { 
static void Main() 
{ 
    AppDomainSetup ads = new AppDomainSetup(); 
    ads.DisallowApplicationBaseProbing = true; 
    AppDomain appDomain = AppDomain.CreateDomain("SomeDomain", null, ads); 
    appDomain.AssemblyResolve += OnAssemblyResolve; 
    appDomain.DoCallBack(target); 
} 

static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) 
{ 
    Console.WriteLine("Hello"); 
    return null; 

} 

private static void target() 
{ 
    Console.WriteLine(AppDomain.CurrentDomain); 
} 
    } 

代碼永遠不會越過+ = OnAssemblyResolve線。

當代碼嘗試執行時,新的應用程序域嘗試解析正在執行的程序集。由於DisallowApplicationBaseProbing = true,因此不知道在哪裏可以找到此程序集。我似乎有一隻雞蛋&雞蛋問題。它需要解析我的程序集來連接程序集解析器,但需要程序集解析器來解析我的程序集。

感謝您的幫助。

-Mike

+0

你看到'Console.WriteLine(「Hello」);'?的輸出嗎?如果是的話,也許你應該實現一個能夠解析程序集的邏輯。我偶然發現了這個問題,因爲我有相同的問題。如果我找到解決方案,我會發布答案。 –

回答

4

隨着大量的實驗,我得到了以下工作:

internal class AssemblyResolver : MarshalByRefObject 
{ 
    static internal void Register(AppDomain domain) 
    { 
    AssemblyResolver resolver = 
     domain.CreateInstanceFromAndUnwrap(
      Assembly.GetExecutingAssembly().Location, 
      typeof(AssemblyResolver).FullName) as AssemblyResolver; 

    resolver.RegisterDomain(domain); 
    } 

    private void RegisterDomain(AppDomain domain) 
    { 
    domain.AssemblyResolve += ResolveAssembly; 
    domain.AssemblyLoad += LoadAssembly; 
    } 

    private Assembly ResolveAssembly(object sender, ResolveEventArgs args) 
    { 
    // implement assembly resolving here 
    return null; 
    } 

    private void LoadAssembly(object sender, AssemblyLoadEventArgs args) 
    { 
    // implement assembly loading here 
    } 
} 

您是這樣創建的:

AppDomainSetup setupInfo = AppDomain.CurrentDomain.SetupInformation; 
    setupInfo.DisallowApplicationBaseProbing = true; 

    domain = AppDomain.CreateDomain("Domain name. ", null, setupInfo); 
    AssemblyResolver.Register(domain); 

對不起,我不能共享代碼用於解決和加載組件。首先,它還沒有工作。其次,在這方面與公衆分享太多太具體。

我將引導一個對象結構,該對象結構與單個文件中反序列化所需的程序集一起序列化。爲此,我真的應該直接去.dll地獄。

相關問題