代码之家  ›  专栏  ›  技术社区  ›  David Braverman

加载已签名程序集时,请注意版本号

  •  3
  • David Braverman  · 技术社区  · 15 年前

    我花了一个小时调试这个,最终消除了PEBCAK问题。

    我有一个国际化的申请。应用程序的关键部分使用反射从配置的源加载类型。所以在配置中,有这样一个条目:

    <component type="Application.Component" assembly="Application, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"/>
    

    应用程序使用以下代码加载组件:

    var assembly = Assembly.Load(assemblyName);
    var rawComponent = assembly.CreateInstance(typeName, false, BindingFlags.CreateInstance, null, instanceParams, CultureInfo.CurrentUICulture, null);
    

    在我签密码的时候失败得很惨。以下是fusion日志查看器的输出:

    *** Assembly Binder Log Entry  (2010-10-21 @ 11:10:52) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\JetBrains\ReSharper\v5.1\Bin\JetBrains.ReSharper.TaskRunner.CLR4.MSIL.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: User = DOMAIN\user
    LOG: DisplayName = Application.resources, Version=2.0.3946.17829, Culture=es-ES, PublicKeyToken=0123456789abcdef
     (Fully-specified)
    LOG: Appbase = file:///C:/Source/Application/UnitTests/bin
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = C:\Users\user\AppData\Local\Temp\w3gjhpl2.blr
    LOG: AppName = Application.UnitTests
    Calling assembly : Application, Version=2.0.3946.17829, Culture=neutral, PublicKeyToken=0123456789abcdef.
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: C:\Source\Application\UnitTests\bin\Application.UnitTests.dll.config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
    LOG: Post-policy reference: Application.resources, Version=2.0.3946.17829, Culture=es-ES, PublicKeyToken=0123456789abcdef
    LOG: The same bind was seen before, and was failed with hr = 0x80070002.
    ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002).
    

    PEBCAK是我花了一个小时试图找出问题是否出在多个区域性中的附属程序集。

    不,问题是,如果指定2.0.0.0作为版本(请参阅上面的配置代码段),并且程序集的调试版本为2.0.3946.17829,则它是另一个版本号。解决方案是将配置更改为:

    <component type="Application.Component" assembly="Application, Version=2.0, Culture=neutral, PublicKeyToken=0123456789abcdef"/>
    

    所以我的问题是:我认为可以指定要加载的版本,而CLR将加载任何更高的版本。为什么不是这样?我错过了什么?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Chris Shaffer    15 年前

    您可以这样做,但是您必须显式地允许重定向到其他版本。详情如下: MSDN: Redirecting Assembly Versions .

    推荐文章