C# 如何修复“引用的程序集没有强名称”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/331520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:31:51  来源:igfitidea点击:

How to fix "Referenced assembly does not have a strong name" error?

c#visual-studiovisual-studio-2005assembliesstrongname

提问by ng5000

I've added a weakly named assembly to my Visual Studio 2005project (which is strongly named). I'm now getting the error:

我在我的Visual Studio 2005项目中添加了一个弱命名的程序集(它是强命名的)。我现在收到错误:

"Referenced assembly 'xxxxxxxx' does not have a strong name"

“引用的程序集 'xxxxxxxx' 没有强名称”

Do I need to sign this third-party assembly?

我需要签署这个第三方程序集吗?

采纳答案by Dirk Vollmar

To avoid this error you could either:

为避免此错误,您可以:

  • Load the assembly dynamically, or
  • Sign the third-party assembly.
  • 动态加载程序集,或
  • 签署第三方程序集。

You will find instructions on signing third-party assemblies in .NET-fu: Signing an Unsigned Assembly (Without Delay Signing).

您将在.NET-fu: Signing an Unsigned Assembly (Without Delay Signing) 中找到有关签署第三方程序集的说明。

Signing Third-Party Assemblies

签署第三方程序集

The basic principle to sign a thirp-party is to

签署第三方协议的基本原则是

  1. Disassemble the assembly using ildasm.exeand save the intermediate language (IL):

    ildasm /all /out=thirdPartyLib.il thirdPartyLib.dll 
    
  2. Rebuild and sign the assembly:

    ilasm /dll /key=myKey.snk thirdPartyLib.il
    
  1. 使用ildasm.exe中间语言 (IL)反汇编并保存程序集:

    ildasm /all /out=thirdPartyLib.il thirdPartyLib.dll 
    
  2. 重建并签署程序集:

    ilasm /dll /key=myKey.snk thirdPartyLib.il
    

Fixing Additional References

修复其他参考

The above steps work fine unless your third-party assembly (A.dll) references another library (B.dll) which also has to be signed. You can disassemble, rebuild and sign both A.dlland B.dllusing the commands above, but at runtime, loading of B.dllwill fail because A.dllwas originally built with a reference to the unsignedversion of B.dll.

上述步骤工作正常,除非您的第三方程序集 ( A.dll) 引用另一个也必须签名的库 ( B.dll)。您可以使用上述命令对A.dllB.dll进行反汇编、重建和签名,但在运行时,B.dll 的加载将失败,因为A.dll最初构建时引用了B.dll未签名版本。

The fix to this issue is to patch the IL file generated in step 1 above. You will need to add the public key token of B.dll to the reference. You get this token by calling

解决此问题的方法是修补上述步骤 1 中生成的 IL 文件。您需要将 B.dll 的公钥标记添加到引用中。您通过调用获取此令牌

sn -Tp B.dll 

which will give you the following output:

这将为您提供以下输出:

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.33440
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key (hash algorithm: sha1):
002400000480000094000000060200000024000052534131000400000100010093d86f6656eed3
b62780466e6ba30fd15d69a3918e4bbd75d3e9ca8baa5641955c86251ce1e5a83857c7f49288eb
4a0093b20aa9c7faae5184770108d9515905ddd82222514921fa81fff2ea565ae0e98cf66d3758
cb8b22c8efd729821518a76427b7ca1c979caa2d78404da3d44592badc194d05bfdd29b9b8120c
78effe92

Public key token is a8a7ed7203d87bc9

The last line contains the public key token. You then have to search the IL of A.dllfor the reference to B.dlland add the token as follows:

最后一行包含公钥令牌。然后,您必须在A.dll的 IL 中搜索对B.dll的引用并添加如下标记:

.assembly extern /*23000003*/ MyAssemblyName
{
  .publickeytoken = (A8 A7 ED 72 03 D8 7B C9 )                         
  .ver 10:0:0:0
}

回答by Alexandr Nikitin

You can use unsigned assemblies if your assembly is also unsigned.

如果您的程序集也是未签名的,则可以使用未签名的程序集。

回答by MrOli3000

Expand the project file that is usingthe project that does not "have a strong name key" and look for the .snkfile (.StrongNameKey).

展开使用没有“强名称键”的项目的项目文件并查找.snk文件 (.StrongNameKey)。

Browse through to this file in Windows Explorer(just so that you know where it is).

Windows 资源管理器中浏览到此文件(以便您知道它在哪里)。

Back in Visual Studio in the project that does not "have a strong name key", do

回到没有“具有强名称键”的项目中的 Visual Studio,执行

  • Right click on the project file
  • Select Properties
  • Select "Signing tab" (on the left)
  • Click the check box "Sign the assembly"
  • Then <Browse>to the .snkfile you found earlier
  • 右键单击项目文件
  • 选择属性
  • 选择“签名选项卡”(在左侧)
  • 单击复选框“签署程序集”
  • 然后<Browse>.snk您之前找到的文件

That should do the trick. This solved a problem for me for one project using a form inside another project in the same solution.

这应该够了吧。这解决了我在同一解决方案中使用另一个项目中的表单的一个项目的问题。

I hope it helps.

我希望它有帮助。

回答by Joe

I had this issue for an app that was strongly named then had to change it in order to reference a non-strongly named assembly, so I unchecked 'Sign the assembly' in the project properties Signing section but it stillcomplained. I figured it had to be an artifact somewhere causing the problem since I did everything else correctly and it was just that. I found and removed the line: [assembly: AssemblyKeyFile("yourkeyfilename.snk")] from its assemblyInfo.cs file. Then no build complaints after that.

我对一个强命名的应用程序有这个问题,然后不得不更改它以引用非强命名的程序集,所以我在项目属性签名部分取消选中“签署程序集”,但它仍然抱怨。我认为它必须是导致问题的某个地方的工件,因为我正确地做了所有其他事情,就是这样。我从它的 assemblyInfo.cs 文件中找到并删除了行:[assembly: AssemblyKeyFile("yourkeyfilename.snk")]。然后在此之后没有构建投诉。

回答by BrutalDev

I have written a tool to automatically strong-name sign assemblies including ones you do not have the source code for or projects that have been abandoned. It uses many of the techniques described in the answers in a simple way without any of the flaws or drawbacks of existing tools or dated instructions.

我编写了一个工具来自动对程序集进行强名称签名,包括您没有源代码的程序集或已放弃的项目。它以简单的方式使用了答案中描述的许多技术,没有任何现有工具或过时说明的缺陷或缺点。

http://brutaldev.com/post/2013/10/18/NET-Assembly-Strong-Name-Signer

http://brutaldev.com/post/2013/10/18/NET-Assembly-Strong-Name-Signer

Hope this helps out anyone that need to sign a third party assembly without having to jump through hoops to get there.

希望这可以帮助需要签署第三方程序集而无需跳过箍到那里的任何人。

回答by Mars Robertson

I was searching for solution to the very same problem and un-ticking "Sign the assembly" option works for me:

我正在寻找相同问题的解决方案,取消勾选“签署程序集”选项对我有用:

enter image description here

在此处输入图片说明

(as you may notice screenshot comes from VS2010 but hopefully it will help someone)

(你可能会注意到截图来自 VS2010 但希望它会帮助别人)

回答by mateuscb

Signing the third party assembly worked for me:

签署第三方程序集对我有用:

http://www.codeproject.com/Tips/341645/Referenced-assembly-does-not-have-a-strong-name

http://www.codeproject.com/Tips/341645/Referenced-assembly-does-not-have-a-strong-name

EDIT: I've learned that it's helpful to post steps in case the linked article is no longer valid. All credit goes to Hiren Khirsaria:

编辑:我了解到,如果链接的文章不再有效,发布步骤会很有帮助。所有功劳都归功于Hiren Khirsaria

  1. Run visual studio command prompt and go to directory where your DLL located.

    For Example my DLL is located inD:/hiren/Test.dll

  2. Now create the IL file using the command below.

    D:/hiren> ildasm /all /out=Test.il Test.dll(this command generates the code library)

  3. Generate new key to sign your project.

    D:/hiren> sn -k mykey.snk

  4. Now sign your library using ilasmcommand.

    D:/hiren> ilasm /dll /key=mykey.snk Test.il

  1. 运行 Visual Studio 命令提示符并转到 DLL 所在的目录。

    For Example my DLL is located inD:/hiren/Test.dll

  2. 现在使用以下命令创建 IL 文件。

    D:/hiren> ildasm /all /out=Test.il Test.dll(此命令生成代码库)

  3. 生成新密钥以签署您的项目。

    D:/hiren> sn -k mykey.snk

  4. 现在使用ilasm命令签署您的库。

    D:/hiren> ilasm /dll /key=mykey.snk Test.il

回答by Pabinator

Removing the "Sign the assembly"check mark under the "Signing"tab works as @Michal Stefanow said.

删除“登录大会”复选标记下的“签名”标签作品@Michal Stefanow说。

Add here is the simplest way to sign your own files and/or other people's files. You just need to add this line under the "Post-build event command line":

在此处添加是签署您自己的文件和/或其他人的文件的最简单方法。您只需要在“构建后事件命令行”下添加这一行:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\signtool.exe" sign /f "$(ProjectDir)\YourPfxFileNameHere.pfx" /p YourPfxFilePasswordHere /d "Your software title here" /du http://www.yourWebsiteHere.com /t http://timestamp.verisign.com/scripts/timstamp.dll /v "$(BaseOutputPath)$(TargetFileName)"

You can sign other people's files or your own files and as many as you want.

您可以签署其他人的文件或您自己的文件,而且数量不限。

enter image description here

在此处输入图片说明

回答by Martin Devillers

How to sign an unsigned third-party assembly

如何签署未签名的第三方程序集

  1. Open up Developer Command Prompt for Visual Studio. This tool is available in your Window programs and can be found using the default Windows search.
  2. Ensure your prompt has access to the following tools by executing them once: snildasmand ilasm
  3. Navigate to the folder where your Cool.Library.dll is located
  4. sn –k Cool.Library.snkto create a new key pair
  5. ildasm Cool.Library.dll /out:Cool.Library.ilto disassemble the library
  6. move Cool.Library.dll Cool.Library.unsigned.dllto keep the original library as a back-up
  7. ilasm Cool.Library.il /dll /resource=Cool.Library.res /key=Cool.Library.snkto reassemble the library with a strong name
  8. powershell -command "& {[System.Reflection.AssemblyName]::GetAssemblyName($args).FullName} Cool.Library.dll"to get the assembly fully qualified name. You will need this bit if you have to reference the DLL in external configuration files like web.config or app.config.
  1. 打开 Visual Studio 的开发人员命令提示符。此工具在您的 Window 程序中可用,可以使用默认的 Windows 搜索找到。
  2. 通过执行一次确保您的提示可以访问以下工具:snildasmilasm
  3. 导航到 Cool.Library.dll 所在的文件夹
  4. sn –k Cool.Library.snk创建一个新的密钥对
  5. ildasm Cool.Library.dll /out:Cool.Library.il拆解图书馆
  6. move Cool.Library.dll Cool.Library.unsigned.dll保留原始库作为备份
  7. ilasm Cool.Library.il /dll /resource=Cool.Library.res /key=Cool.Library.snk使用强名称重新组装库
  8. powershell -command "& {[System.Reflection.AssemblyName]::GetAssemblyName($args).FullName} Cool.Library.dll"获取程序集的完全限定名称。如果您必须在外部配置文件(如 web.config 或 app.config)中引用 DLL,您将需要此位。

回答by Demodave

For me my issue was that I had two of the same NuGet Packages installed with different Versions.

对我来说,我的问题是我安装了两个不同版本的相同 NuGet 包。