Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

.net - Accessing newly signed third party DLL gives error

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.

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"

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=..."]

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the exact same issue.

Why it happens

  • 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.

How to fix

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:

  • 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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...