加载已签名的程序集时要小心版本号

时间:2010-10-21 16:03:15

标签: c# .net reflection internationalization assembly-signing

我花了一个小时调试这个,最后我终止了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);

我签署代码时失败了。这是融合日志查看器的输出:

*** 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 个答案:

答案 0 :(得分:1)

您可以这样做,但是您必须明确允许重定向到其他版本。详情请见MSDN: Redirecting Assembly Versions