Search code examples
c#.net-assemblysigningpost-build-eventstrongname

Is an assembly signed with a strong name before or after the post-build event?


I'm modifying the stack size of an assembly using editbin, see Increase stack size of main program or create a new thread with larger stack size for recursive code blocks?

Now I'm asking myself: Is an assembly signed with a strong name before or after the post-build event? Because editbin is changing the assembly in the post-build event.

My post build-event looks like that:

"$(DevEnvDir)..\..\VC\bin\editbin.exe" /STACK:16777216 "$(TargetPath)"

And my project .csproj file contains the following lines:

<PropertyGroup>

  <SignAssembly>true</SignAssembly>
  <AssemblyOriginatorKeyFile>..\STRONGNAME.snk</AssemblyOriginatorKeyFile>

</PropertyGroup>

<PropertyGroup>
  <PostBuildEvent>"$(DevEnvDir)..\..\VC\bin\editbin.exe" /STACK:16777216 "$(TargetPath)"</PostBuildEvent>
</PropertyGroup>

Solution

  • An assembly is signed with a strong name before the post-build event. That means editbin will change that assembly and the signature is not valid anymore.

    sn.exe -v assembly.exe will return Failed to verify assembly -- Strong name validation failed ...

    A workaround to get a valid signed assembly which was modified using editbin is to use the AfterCompile event and to resign the assembly using sn.

    The Project file should look like that:

      <Target Name="AfterCompile">
        <Exec Command="
    &quot;$(DevEnvDir)..\..\VC\bin\editbin.exe&quot; /STACK:16777216 &quot;$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)&quot;
    echo $(FrameworkSDKDir)bin\NETFX 4.5.1 Tools\
    &quot;$(FrameworkSDKDir)bin\NETFX 4.5.1 Tools\sn.exe&quot; -Ra &quot;$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)&quot; &quot;$(SolutionDir)\STRONGNAME.snk&quot;
    " />
      </Target>
      <PropertyGroup>
        <PostBuildEvent>REM "See AfterCompile for stack size and resigning"</PostBuildEvent>
      </PropertyGroup>