【发布时间】:2011-08-18 12:23:22
【问题描述】:
我有两个程序集:HelloWorld.exe 和 Hello.dll。 exe是主程序集,而主程序集正在使用dll。
我编译了 HelloWorld.exe (1.0.0) 和 Hello.dll (1.0.0)。我将程序集放在不同的文件夹中。
然后我将 Hello.dll 的版本更改为 2.0.0 并继续用 2.0.0 版本覆盖 Hello.dll 1.0.0。然后我启动 HelloWorld.exe,它运行良好。
我预计它会立即崩溃并烧毁,因为我编译 EXE 时引用的 Hello.dll 是 1.0.0。现在,1.0.0 DLL 已被 2.0.0 替换,但它仍然可以工作!
根据MSDN:
默认情况下,程序集只会使用完全相同的类型 构建和测试它的程序集(名称和版本号)。 也就是说,如果您的程序集使用 1.0.0.2 版中的类型 另一个程序集,它将(默认情况下)不使用相同的类型 另一个程序集的 1.0.0.4 版本。这种名称和 识别引用程序集的版本有助于避免“DLL 地狱” 升级一个应用程序会破坏其他应用程序的问题。
问题:
- 为什么有效?
- 如何让它不起作用?
- 额外问题:在构建过程中会发生什么?外部依赖的版本不是硬编码到主依赖的吗?
请注意,Hello.dll 不是强命名的。
这是 HelloWorld.exe 的清单:
// Metadata version: v2.0.50727
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 2:0:0:0
}
.assembly extern Hello
{
.ver 2:0:0:0
}
.assembly HelloWorld
{
...//snipped
}
这是从 Fuslogvw.exe(程序集绑定日志查看器)获取的程序集绑定日志:
=== Pre-bind state information ===
LOG: User = ian.uy
LOG: DisplayName = Hello, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Users/ian.uy/Desktop/HelloWorld/HelloWorld/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/ian.uy/Desktop/HelloWorld/HelloWorld/bin/Debug/Hello.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\ian.uy\Desktop\HelloWorld\HelloWorld\bin\Debug\Hello.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Hello, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: Binding succeeds. Returns assembly from C:\Users\ian.uy\Desktop\HelloWorld\HelloWorld\bin\Debug\Hello.dll.
LOG: Assembly is loaded in default load context.
【问题讨论】:
标签: .net assemblies dll