gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:59:42 28 4
gpt4 key购买 nike

我正在按照 2006 年 Microsoft .Net 类(class)工作簿中的说明进行其中一项练习。 (具体来说,这门类(class)是 MS2349B,我正在做模块 4 练习 2。)。这些练习似乎是为 Vista 之前的日子而构建的,那时每个人都始终拥有完全的管理员权限。 (我使用的是 .net 4.0。)

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

按照教程,我使用 #if block 对程序集进行签名:

#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。当我在非管理员命令提示符下运行时,出现以下错误

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,我会收到以下错误,表明它正在寻找预期的公钥 token 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 的卷影副本。

如果这不能帮助激活融合日志记录(使用 fuslogvw.exe 或假脱机融合日志到磁盘),然后查看从中加载有问题的 dll 的日志。IMO 是加载了错误的 dll。

关于c# - 在探测程序集时,为什么搜索的 publicKeyToken 在以管理员身份运行与以普通用户运行时不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7617450/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com