Geeks With Blogs
Annie Bougie
For testing, I often use the InternalsVisibleTo for my unit tests. It looks like this:

[assembly: InternalsVisibleTo("MyProject.Domain.Tests")]

When the assembly is strongly typed, though, this simple statement will not work, and we get the following message:

Error: "Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations"

To fix this, you must do two things: You must sign the test assembly, and you must include the public key in the InternalsVisibleTo statement.

To get the public key value from the .snk file, use the sn.exe utility included in the .NET SDK. You must do two things, first, create a .snk file that contains only the public key:

sn -p MyKey.snk MyKey.PublicKeyOnly.snk

Then, you can extract the public key value with this command:

sn -tp MyKey.PublicKeyOnly.snk

This will produce a similar output:

Public key is
0024000004800000940000000602000000240000525341310004000001000100cfb8bc23b86a08
e70d021dd53d3b0293e716e71015870bdcc58a0231a4228618851a83e06077f5a44f42beb2baf3
56ad2d344521a96b0081ed0f25f9227523e3625eda524efe1cf2e1e5e41f3693a76ec52347684b
8129a4bb2d5fc49681adf33da0eecc4f81f011af4539d12abe1b4e760b5ce32d766db1012d4402
8381f0b4

Public key token is 2ff2b71993eeff95

Copy the public key value and update the InternalsVisibleTo:

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

e70d021dd53d3b0293e716e71015870bdcc58a0231a4228618851a83e06077f5a44f42beb2baf3
56ad2d344521a96b0081ed0f25f9227523e3625eda524efe1cf2e1e5e41f3693a76ec52347684b
8129a4bb2d5fc49681adf33da0eecc4f81f011af4539d12abe1b4e760b5ce32d766db1012d4402
8381f0b4")]

Voila! You can now access the internals from your test project for strong named assemblies. Posted on Wednesday, October 1, 2008 2:18 AM C# Code | Back to top


Comments on this post: InternalsVisibleTo with Strongly-Type Assemblies

# re: InternalsVisibleTo with Strongly-Type Assemblies
Requesting Gravatar...
I guess you hear and read this quite a bit: Thank you, thank you, thank you
Left by Lucas Hougham on Nov 12, 2010 2:35 AM

# re: InternalsVisibleTo with Strongly-Type Assemblies
Requesting Gravatar...
i am doing the same but still getting the same error,i am using VS 2010,Plz help me
Left by slash on Dec 25, 2010 10:27 AM

# re: InternalsVisibleTo with Strongly-Type Assemblies
Requesting Gravatar...
To fix this i did the following.

On the project, use the sn.exe -Tp to get the public key of the .snk file.

On the project, update the internals visable to, with the attribute PublicKey=...

Now, in the Unit Test project, sign the project with the same .snk file as the project.
Left by Doof on Mar 02, 2011 5:21 PM

# re: InternalsVisibleTo with Strongly-Type Assemblies
Requesting Gravatar...
Awesome. I was having this issue and the article cleared me right up!
Left by James Michael Hare on Mar 22, 2011 8:02 AM

# re: InternalsVisibleTo with Strongly-Type Assemblies
Requesting Gravatar...
Thank you.
my problem was I just wrote public key token instead of the whole public key and it didn't work.
Left by Maziar Taheri on Jun 29, 2012 5:18 PM

# re: InternalsVisibleTo with Strongly-Type Assemblies
Requesting Gravatar...
There are a lot of different ways to get the public key. My issue was a missing comma after the assembly name and before the PublicKey=...
Left by Tiago Freitas Leal on Sep 23, 2012 3:55 AM

Your comment:
 (will show your gravatar)


Copyright © Annie Bougie | Powered by: GeeksWithBlogs.net