.NET, Archive, Visual Studio

Acquiring a Public Key Before Compiling

In the past I have talked about the The Wonders Of InternalsVisibleTo and how such a simple attribute allows for internal components to be shared between different assemblies, opening the door to greater code separation and dependency control. In that previous article I illustrated how you can use the sn tool that ships with Visual Studio to get the public key signature from one of your existing dlls.

sn -Tp MyExample.ServiceLayer.dll

The shortcomming of the above approach is that it requires your code to be compiled. This normally isn’t a big deal, in fact I prefer my code to be compliable more times than not. But what if you cannot compile your code?

This was preciously the situation I found myself in. I had a newly created assembly containing code I was refactoring out of a monolithic assembly. These newer, more modular, assemblies contained code that would need internal visibility into a common core assembly. So there I found myself with three assemblies where I had previously only had one, and nothing is compiling because the new assemblies do not have internal visibility rights to the common assembly.

Fortunately the sn tool has the ability to get the public key signature from an assembly’s private key. To do this you will first have to generate a public key from your assemblies private key.

sn -p Assembly_Key.snk public.key

Once the public key has been generated you can now reveal the public key signature.

sn -tp public.key

Notice that unlike the original command from the beginning of this article this last command has a lower case “t” as a switch option. After the public key has been revealed you can delete the public key file (not your private key), as it is no longer needed.

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.


About Jason

Jason is an experienced entrepreneur & software developer skilled in leadership, mobile development, data synchronization, and SaaS architecture. He earned his Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *