我有一个为.NET Framework4.8编译的工具,它使用NuGet包Microsoft.VisualStudio.Services.Client
,其中包含一个程序集Microsoft.VisualStudio.Services.Common
。
该工具在大多数机器上运行良好,但在某些机器上却失败了:
System.IO.FileLoadException: Could not load file or assembly 'Microsoft.VisualStudio.Services.Common, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)
File name: 'Microsoft.VisualStudio.Services.Common, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.Security.SecurityException: Strong name validation failed. (Exception from HRESULT: 0x8013141A)
The Zone of the assembly that failed was:
MyComputer
我已经检查过了,在这两种情况下,它都使用完全相同的.NET框架(4.8)运行。我试图检查融合日志,但是那里没有报告错误,即使是导致它报告成功的问题的程序集也是如此。
(在.NET 6上使用相同的代码。)
我的问题是,怎样才能诊断出这类问题?有什么我能查的线索吗?
更新:反思@CherryDT的评论:奇怪的是,使用sn.exe
工具跳过验证也没有帮助:
C:\Users\<user>\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\net48>sn -Vr Microsoft.VisualStudio.Services.Common.dll
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Verification entry added for assembly 'Microsoft.VisualStudio.Services.Common,B03F5F7F11D50A3A'
C:\Users\<user>\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\net48>sn -Vl
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Assembly/Strong Name Users
===========================================
Microsoft.VisualStudio.Services.Common,B03F5F7F11D50A3A All users
C:\Users\<user>\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\net48>sn -v Microsoft.VisualStudio.Services.Common.dll
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
Assembly 'Microsoft.VisualStudio.Services.Common.dll' is valid
Assembly 'Microsoft.VisualStudio.Services.Common.dll' is verified with a key other than the identity key
C:\Users\<user>\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\net48>ConsoleApp1.exe
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.VisualStudio.Services.Common, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A) ---> System.Security.SecurityException: Strong name validation failed. (Exception from HRESULT: 0x8013141A)
--- End of inner exception stack trace ---
at Program.<Main>$(String[] args)
发布于 2022-04-27 10:35:59
我终于找到原因了。(感谢@CherryDT迫使我再次检查sn.exe
。)
问题是,程序集的签名确实是错误的,但是在没有导致错误的机器上,对于所有程序集(*,*
)都有一个全局强名称验证忽略,这很可能是某个应用程序安装程序早些时候悄悄添加的。(在不通知我的用户的情况下做这样的事情是非常不友好的。)
我之前没有意识到这一点的原因是,我使用的是x86版本的sn.exe
,而不是应用程序使用的x64
版本。
最后,我使用了一个不同版本的依赖项来解决这个问题,其中已经解决了签名问题。
因此,结论是,我应该始终确保使用正确的sn.exe
版本(有关如何使用的详细信息,请参见https://stackoverflow.com/a/18760340/26530 )。
发布于 2022-09-02 12:11:34
可以使用以下命令跳过强名称验证。
对于x64
机器,
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\sn.exe" -Vr *,*
请注意,我们使用的是x64
版本的sn.exe
。
对于x32
机器,
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\sn.exe" -Vr *,*
请注意,我们使用的是x32
版本的sn.exe
。
https://stackoverflow.com/questions/72026072
复制相似问题