posts - 17 , comments - 52 , trackbacks - 0

My Links

News

Contact Me anne.bougie@gmail.com

Tag Cloud

Article Categories

Archives

Post Categories

InternalsVisibleTo with Strongly-Type Assemblies

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.

Print | posted on Wednesday, October 1, 2008 2:18 AM | Filed Under [ C# Code ]

Feedback

Gravatar

# re: InternalsVisibleTo with Strongly-Type Assemblies

I guess you hear and read this quite a bit: Thank you, thank you, thank you
11/12/2010 2:35 AM | Lucas Hougham
Gravatar

# re: InternalsVisibleTo with Strongly-Type Assemblies

i am doing the same but still getting the same error,i am using VS 2010,Plz help me
12/25/2010 10:27 AM | slash
Gravatar

# re: InternalsVisibleTo with Strongly-Type Assemblies

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.
3/2/2011 5:21 PM | Doof
Gravatar

# re: InternalsVisibleTo with Strongly-Type Assemblies

Awesome. I was having this issue and the article cleared me right up!
3/22/2011 8:02 AM | James Michael Hare
Gravatar

# re: InternalsVisibleTo with Strongly-Type Assemblies

Thank you.
my problem was I just wrote public key token instead of the whole public key and it didn't work.
6/29/2012 5:18 PM | Maziar Taheri
Gravatar

# re: InternalsVisibleTo with Strongly-Type Assemblies

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=...
9/23/2012 3:55 AM | Tiago Freitas Leal
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: