我有一个VisualStudio 2008的工程,它引用了Assembly nunit.framework.dll,以便进行单元测试。当我用一个帐号登录机器,用VS20008打开这个工程,在工程的References下面能够找到,VS能够找到对nunit.framework.dll的引用。但是,当我用另外一个帐号登录机器是,再用VS20008打开这个工程,在工程的References下面的nunit.framework前面出现了一个黄色的惊叹号,它表示VS没有找到对nunit.framework.dll的引用。这是为什么呢?
1. 首先要弄清楚VS是如何去查找应用的Assembly的。
VS在build工程的时候,是调用msbuild来完成的。所以,VS查找Assembly的方法与msbuild查找的方法是一样的。这样,我们通过直接调用msbuild来build这个工程,然后分析日志文件就能找到原因。在这里,我们需要使用到msbuild的一个参数:/verbosity:level(见参考1)。level包含这几种:q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]。这里我们需要使用到最后一种,也是给出信息最多的一种,它是 diag[nostic]。例如:
c:\temp> msbuild /verbosity:diag test.csproj >output.log
运行完这条命令,会在c:\temp下生成output.log。分析这个文件,我们就能够知道VS到底会在哪些目录下查找nunit.framework.dll。
Primary reference "nunit.framework, Version=2.6.1.12217, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77". (TaskId:89)
Resolved file path is "C:\Program Files (x86)\NUnit 2.6.1\bin\framework\nunit.framework.dll". (TaskId:89)
Reference found at search path location "{Registry:Software\Microsoft\.NetFramework,v3.5,AssemblyFoldersEx}". (TaskId:89)
For SearchPath "{CandidateAssemblyFiles}". (TaskId:89)
......
For SearchPath "{TargetFrameworkDirectory}". (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.5\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.5\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v2.0.50727\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v2.0.50727\nunit.framework.dll", but it didn't exist. (TaskId:89)
For SearchPath "{Registry:Software\Microsoft\.NetFramework,v3.5,AssemblyFoldersEx}". (TaskId:89)
Considered "C:\Program Files (x86)\NUnit 2.6.1\bin\framework\nunit.framework.exe", but it didn't exist. (TaskId:89)
Found related file "C:\Program Files (x86)\NUnit 2.6.1\bin\framework\nunit.framework.xml". (TaskId:89)
Primary reference "nunit.framework, Version=2.6.1.12217, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL". (TaskId:89)
C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets : warning MSB3245: Could not resolve this reference. Could not locate the assembly "nunit.framework, Version=2.6.1.12217, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
For SearchPath "{CandidateAssemblyFiles}". (TaskId:89)
......
For SearchPath "{TargetFrameworkDirectory}". (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.5\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.5\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v3.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v2.0.50727\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Windows\Microsoft.NET\Framework\v2.0.50727\nunit.framework.dll", but it didn't exist. (TaskId:89)
For SearchPath "{Registry:Software\Microsoft\.NetFramework,v3.5,AssemblyFoldersEx}". (TaskId:89)
Considered "C:\Program Files (x86)\Business Objects\Common\2.8\managed\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Business Objects\Common\2.8\managed\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\ReportViewer\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Visual Studio 9.0\ReportViewer\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Common Files\Microsoft Shared\Visual Basic Power Packs\1.1\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Common Files\Microsoft Shared\Visual Basic Power Packs\1.1\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\VSTA\v9.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\VSTA\v9.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\VSTO\v8.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\VSTO\v8.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\VSTO\v9.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\VSTO\v9.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
For SearchPath "{AssemblyFolders}". (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "c:\Program Files (x86)\Microsoft SQL Server\90\SDK\Assemblies\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "c:\Program Files (x86)\Microsoft SQL Server\90\SDK\Assemblies\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Desktop\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Desktop\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "c:\Program Files (x86)\Microsoft.NET\ ADOMD.NET\90\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "c:\Program Files (x86)\Microsoft.NET\ ADOMD.NET\90\nunit.framework.dll", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Synchronization Services\ ADO.NET\v1.0\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "C:\Program Files (x86)\Microsoft Synchronization Services\ ADO.NET\v1.0\nunit.framework.dll", but it didn't exist. (TaskId:89)
For SearchPath "{GAC}". (TaskId:89)
Considered "nunit.framework, Version=2.6.1.12217, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL", which was not found in the GAC. (TaskId:89)
For SearchPath "{RawFileName}". (TaskId:89)
Considered treating "nunit.framework, Version=2.6.1.12217, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" as a file name, but it didn't exist. (TaskId:89)
For SearchPath "bin\Debug\". (TaskId:89)
Considered "bin\Debug\nunit.framework.exe", but it didn't exist. (TaskId:89)
Considered "bin\Debug\nunit.framework.dll", but it didn't exist. (TaskId:89)
对比这两份日志文件,我们可以发现,VS会去注册表Software\Microsoft\.NetFramework,v3.5,AssemblyFoldersEx下面查找nunit.framework。一种登录情况能找到,另外一种登录情况下找不到,由此可以断定这应该是与HKEY_CURRENT_USER有关。因此我们可以推断出的完整注册表项是:HKEY_CURRENT_USER\Software\Microsoft\.NetFramework\3.5\AssemblyFoldersEx。果然,我发现,在能成功build的登录方式下,可以看到这个注册表项有默认值C:\Program Files (x86)\NUnit 2.6.1\bin\framework\;而在另一种登录方式下去找不到这个注册表项。
2. 解决办法
1)用能够build成功的帐号登录,打开注册表编辑器regedit.exe,找到注册表项HKEY_CURRENT_USER\Software\Microsoft\.NetFramework\3.5\AssemblyFoldersEx,右键该项,在弹出菜单中选择export命令。
这里给出的使用msbuild /verbosity:diag来查找问题的方法很有用,它不仅解决有关references的问题,还可以用来查找很多其它的有关build的问题。