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