最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - CreateDomain not probing for assemblies in ApplicationBasePrivateBinPath as I would have expected - Stack Overflow

programmeradmin1浏览0评论

At some point in an AssemblyResolve handler I have tried to load assemblies coming with complex app.config scheme in a separate domain to let this domain dynamically solve assemblyBinding found in the configuration file for me (as I cannot change current domain bindings once it is running).

Anyhow I noticed that, even if setting correctly ApplicationBase, PrivateBinPath, and ConfigurationFile, and checking very carefully for deployed version of assemblies:

  • Binding redirection was successfull (looking for expected version described in app.config)
  • But was unable to locate the file (even if deployed ... I checked hundred times with DotPeek)

Here is shortened version of code for illustration:

static void Main(string[] args)
{
    // Init
    var configFilename = Path.GetFullPath(@".\..\..\TestApp\TestApp.exe.config");
    var configFolder = Path.GetDirectoryName(configFilename);

    // Emulate same 'AssemblyResolve' callback arguments
    var resolveEventArgs = new ResolveEventArgs("System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51", null);

    // Create separate domain to resolve third - party using same 'app.config' scheme
    var setup = new System.AppDomainSetup()
    {
        ApplicationName = "CrashTestDummies",
        ApplicationBase = configFolder, // Use same folder as config for application
        PrivateBinPath = configFolder, // and private assemblies probing
        ConfigurationFile = configFilename, // and force to use config                
     };
     var crashTestDomain = System.AppDomain.CreateDomain("CrashTestDummies", null, setup);

    // Ask other domain to load the assembly for us (hence solving dynamically for app.config binding stuff)
    Directory.SetCurrentDirectory(configFolder); // Try forcing .. did not help :(
    var arequestinfo = new AssemblyName(resolveEventArgs.Name);
    var asmInCrashDomain = crashTestDomain.Load(arequestinfo);
}

This code will fail on final Load operation with following exception:

Cannot load file or assembly 'System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. File not found.

So all looks like redirection from 4.0.1.1 to 4.0.1.2 as per ConfigurationFile has been ok but then it fails to find the file (eventhough it is really there with correct Version/Culture/PublicKeyToken ... loading directly 'System.Memory.dll' has no issue).

I since solve my issue by directly loading "System.Memory.dll" file without any check during AssemblyResolve anyhow I'm still curious why separate domain approach is not working fully (redirection ok, but finding file ko).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论