As a sidebar to my post on fully-qualified assembly names, I wanted to post the steps I use to sign an assembly. These steps have changed from what I used to do in .NET 1.1, where I used an assembly annotation in AssemblyInfo.cs to point to the key file. That attribute has been deprecated in .NET 2.0.
I sign an assembly by creating an SNK file with the SN.exe utility.
- At the command line, execute sn -k BradTestKey.snk
- Import the key pair into the machine container by executing, sn -i BradTestKey BradTestKey.snk
- Open the CSPROJ file in Notepad
- Under the first <PropertyGroup> node, add <KeyContainerName>BradTestKey</KeyContainerName>
Now, your assembly is signed.
But
wait a minute, what if I only want to sign my Release builds, not my
Debug builds. Can I do that? Why yes, you can. A default CSPROJ file
will contain MSBuild PropertyGroup nodes that execute either under the
Debug or Release configuration mode. So, here's what you do:
- Open your CSPROJ file in Notepad
- Find the <PropertyGroup> that looks like this: <PropertyGroup
Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
- Under that node, add <KeyContainerName>BradTestKey</KeyContainerName>
Now, only your Release binaries will be signed.
Remember that the BradTestKey.snk and the BradTestKey machine container name are my conventions and you should use your own.
Proof that is worked (test code is also attached to this post):
ConfigFullDNTests.zip (25.66 kb)