C# 在探测程序集时,为什么搜索到的publicKeyToken在以管理员身份运行时与以普通用户身份运行时有所不同?

C# 在探测程序集时,为什么搜索到的publicKeyToken在以管理员身份运行时与以普通用户身份运行时有所不同?,c#,.net,assemblies,gac,C#,.net,Assemblies,Gac,我正在按照2006年Microsoft.Net课程工作簿中的说明进行其中一个练习。(具体来说,本课程是MS2349B,我正在做模块4练习2。)。这些练习似乎是为Vista之前的日子设计的,那时每个人都有完全的管理员权限。(我正在使用.NET4.0。) 本练习涉及构建强名称程序集、将其安装在GAC中、针对强名称程序集构建本地可执行文件、验证可执行文件是否运行 根据教程,我使用#if块对程序集进行签名: #if STRONG [assembly: System.Reflection.Assembly

我正在按照2006年Microsoft.Net课程工作簿中的说明进行其中一个练习。(具体来说,本课程是MS2349B,我正在做模块4练习2。)。这些练习似乎是为Vista之前的日子设计的,那时每个人都有完全的管理员权限。(我正在使用.NET4.0。)

本练习涉及构建强名称程序集、将其安装在GAC中、针对强名称程序集构建本地可执行文件、验证可执行文件是否运行

根据教程,我使用
#if
块对程序集进行签名:

#if STRONG
[assembly: System.Reflection.AssemblyVersion("2.0.0.0")]
[assembly: System.Reflection.AssemblyKeyFile("OrgVerKey.snk")]
#endif
我以本地用户身份构建可执行文件:

C:\path\to\lab>csc /define:STRONG /target:library 
 /out:AReverser_v2.0.0.0\AReverser.dll AReverser_v2.0.0.0\AReverser.cs
C:\path\to\lab>csc /reference:MyStringer\Stringer.dll     
 /reference:AReverser_v2.0.0.0\AReverser.dll Client.cs
我通过以管理员身份运行的visual studio命令提示符将其安装到GAC中:

C:\path\to\lab>gacutil /i AReverser_v2.0.0.0\AReverser.dll
当我在管理员提示下运行我的exe时,我得到了预期的输出——应用程序运行良好,并且似乎能够从gac正确加载dll。当我在非admin命令提示符下运行时,我得到以下错误

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembl
y 'AReverser, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b5fcbdcff229fabb'
 or one of its dependencies. The located assembly's manifest definition does not
 match the assembly reference. (Exception from HRESULT: 0x80131040)
   at MainApp.Main()
让我感到奇怪的是,publicKeyToken与GAC中的不同:

AReverser, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f0548c0027634b66
但是,如果我从GAC卸载AReverser并尝试以管理员提示的方式运行我的exe,则会出现以下错误,表明它正在查找预期的公钥令牌f0548c0027634b66:

C:\path\to\lab>gacutil /u "AReverser,Version=2.0.0.0,Culture=neutral,
PublicKeyToken=f0548c0027634b66"
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.


Assembly: AReverser, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f0548c0027
634b66
Uninstalled: AReverser, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f0548c0
027634b66
Number of assemblies uninstalled = 1
Number of failures = 0

C:\path\to\lab>Client.exe

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembl
y 'AReverser, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f0548c0027634b66'
 or one of its dependencies. The located assembly's manifest definition does not
 match the assembly reference. (Exception from HRESULT: 0x80131040)
   at MainApp.Main()
注意,在Admin下,它实际上是在搜索正确的publicKeyToken

有什么好处?为什么搜索的publickKeyTokens会有所不同?我会做错什么

编辑

我们被告知要使用的应用程序配置可能是罪魁祸首,我想知道你是否必须是管理员才能应用这些设置。摆脱它似乎会导致以管理员身份运行失败(尽管在这种情况下,publicKeyToken被列为NULL)。这是我的应用程序配置

<configuration>   
    <runtime>
        <assemblyBinding
            xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="MyStringer"/>
            <publisherPolicy apply="no"/>
            <dependentAssembly>
                <assemblyIdentity name="AReverser" 
                    publicKeyToken="f0548c0027634b66" 
                    culture=""/>
                <publisherPolicy apply="no"/>
                <bindingRedirect oldVersion="2.0.0.0" 
                    newVersion="2.0.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

在磁盘上搜索AReverser.dll。你可能在某个隐藏的地方有一些额外的副本。VS可以生成已编译DLL的卷影副本

如果这无助于激活fusion日志(使用fuslogvw.exe或spool fusion logs到磁盘),然后查看加载问题dll的日志。
在我看来,加载的dll是错误的。

你的解决方案在哪里?比如说程序集在哪里?VisualStudio不使用gac进行生成,因此,如果您的引用目录中留下了aigned程序集的不同版本,则将根据该版本生成客户端,并在运行时尝试加载时失败,因为gac是先加载的

你在集会上签名了吗?我在您的CSC语句中没有看到关键文件。@user957902我用上面的
#if STRONG
块签名,正如MS2349B工作簿中的示例告诉我的那样。snk文件来自何处?如果您自己做了,那么您必须更改配置文件中的令牌值。@HansPassant我在做此练习之前使用了
sn-k OrgVerKey.snk