访问新签名的第三方 DLL 出错 [英] Accessing newly signed third party DLL gives error

查看:32
本文介绍了访问新签名的第三方 DLL 出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用第三方 DLL 的签名应用程序.这些 DLL 未签名.- 到目前为止,第一步没有问题:我刚刚签署了它们(使用 ildasm.exe 获取 *.il,在 *.il 中只是获得 publickeytoken 因为它们具有相互依赖性,并使用 ilasm.exe 制作了 *.dll)

I have a signed application that uses third party DLLs. These DLLs were not signed. - So far no problem for the first step: I just signed them (getting *.il with ildasm.exe, ajust publickeytoken in the *.il 's because they have interdependencies, and made the *.dll's with ilasm.exe)

该项目现在可以正常编译并启动.

The project now compiles fine and also starts up.

但是当在我的代码中调用了 3rd-party-DLL 的类构造函数时(或其他什么?-这只是我做的第一件事),我收到错误 强名称签名的程序集必须在其 InternalsVisibleTo 声明中指定一个公钥"

But when in my code, a class constructor of the 3rd-party-DLL is called (or something else? - was just the first thing I did), I get the error "Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations"

如果你有DLL的源并且可以通过设置在AssemblyInfo.cs中进行调整,似乎不会有问题

It seems there won't be a problem if you have the source of the DLL and can ajust in AssemblyInfo.cs by setting

[assembly: InternalsVisibleTo("MyProject.Domain.Tests, PublicKey=..."]

但是: 如上所述,我有一个第三方 DLL,我没有有源代码.所以没有办法解决这样的问题.

But: As mentioned above I have a third-party DLL I don't have the source. So no way to solve the problem like this.

有什么建议可以让它运行吗?

Any suggestions to get this running?

推荐答案

我遇到了完全相同的问题.

I had the exact same issue.

为什么会发生

  • 第三方程序集使用 InternalsVisibleTo 声明,使其成为其他程序集的朋友",例如InternalsVisibleTo("OtherAssembly")
  • .NET 要求强名称程序集只能是其他强名称程序集的朋友",在这种情况下,InternalsVisibleTo 属性必须指定那些其他程序集的公钥,例如InternalsVisibleTo("OtherAssembly, PublicKey=[key]")
  • 在运行时,CLR 发现 InternalsVisibleTo 没有为相关程序集正确声明,因此会引发异常.
  • The 3rd-party assembly is declared with InternalsVisibleTo to make it "friend" to other assemblies, e.g. InternalsVisibleTo("OtherAssembly")
  • .NET requires that strong-name assembly can only be "friend" to other strong-name assemblies, in which case the InternalsVisibleTo attribute must specify the public keys of those other assemblies, e.g. InternalsVisibleTo("OtherAssembly, PublicKey=[key]")
  • At runtime, the CLR sees that InternalsVisibleTo is not properly declared for the assembly in question, so it throws the exception.

如何修复

如果程序执行不需要朋友"程序集(例如,它是一个未部署在生产中的测试程序集),请执行以下步骤:

If the "friend" assemblies aren't needed for the program execution (e.g. it's a Test assembly, which isn't deployed in production), follow these steps:

  • 反汇编有问题的程序集:ildasm.exe ThirdParty.dll/OUTPUT=ThirdParty.il
  • 使用文本编辑器编辑 IL 文件,删除 InternalsVisibleTo
  • 的任何声明
  • 组装并签署 IL:ilasm.exe ThirdParty.il/DLL/OUTPUT=ThirdParty.modified.dll/KEY=key.snk
  • 注意:生成密钥:sn.exe -k key.snk

如果程序执行需要朋友"程序集,您必须签署所有这些朋友程序集.然后按照与上述类似的步骤进行操作,除了不是删除 InternalsVisibleTo,您必须使用正确的公钥修改每个声明.

If the "friend" assemblies are needed for the program execution, you have to sign all those friend assemblies. Then follow similar steps as above, except instead of removing InternalsVisibleTo, you have to amend each declaration with the correct public key.

这篇关于访问新签名的第三方 DLL 出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆