Only sign assemblies with strong name during release build

Felix picture Felix · Oct 26, 2011 · Viewed 9.9k times · Source

The scenario is: I'm building my solution with TeamCity, I'm not running the build account as an administrator; so I get problems with the strong name keys.

I know of a few solutions out there, like running the build as an administrator and registering the certificates in the proper container.

Is there anyway to sign the assemblies within a solution file only during a release build and not during a debug build. Or are there any similar solutions?

I think it is strange that there isn't a MSBuild parameter that can be set wether the assemblies should be signed or not. Because if you look at the csproj-files there is an option there for signed or not signed

Answer

Jehof picture Jehof · Oct 28, 2011

Another option is to edit the project file. By default if you enable assembly signing in Visual Studio it will be used for all build configurations. The project file contains an element like the following.

<PropertyGroup>
  <SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
  <AssemblyOriginatorKeyFile>YourKeyFile.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>

If you only want to sign the assemblies during a specifc build configuration, such as RELEASE. You can put the <SignAssembly> and <AssemblyOriginatorKeyFile> in the PropertyGroup element with the Condition that identifies your build configuration.

So if you want to sign your assembly during a release build, you can change your project file to the following.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <!-- other element of this PropertyGroup -->
  <SignAssembly>true</SignAssembly>      
  <AssemblyOriginatorKeyFile>YourKeyFile.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>

Note: When you change your project file, to the following. you cannot change the signing settings of the Project Properties in Visual Studio. That means in Visual Studio is signing of the assembly disabled, also if you change the build configuration in Visual Studio.