访问新签约第三方DLL提供错误第三方、错误、DLL

2023-09-02 10:53:17 作者:乱世惊梦

我有一个使用第三方DLL的一个签名的应用程序。这些DLL都没有签署。 - 到目前为止的第一个步骤没有问题:我刚刚签下他们(获取* .il内与程序Ildasm.exe,在* .il内的ajust公钥,因为他们有相互依存关系,并取得* .DLL同ilasm.exe)

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.

但是,当我在code,该第三方-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中可以ajust

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?

推荐答案

我有相同的问题。

为什么会发生

的第三方组件与 InternalsVisibleTo 宣布,以使其朋友到其他组件,如: InternalsVisibleTo(OtherAssembly) 在.NET要求强名称程序集只能是朋友到其他强名称程序集,在这种情况下, InternalsVisibleTo 属性必须指定这些的公钥其他组件,如: InternalsVisibleTo(OtherAssembly,公钥= [关键]) 在运行时,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 的 Disassemble the assembly in question: ildasm.exe ThirdParty.dll /OUTPUT=ThirdParty.il Use a text editor to edit the IL file, remove any declaration of InternalsVisibleTo Assemble and sign the IL: ilasm.exe ThirdParty.il /DLL /OUTPUT=ThirdParty.modified.dll /KEY=key.snk Note: generate a key by: 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.