Is an assembly signed with a strong name before or

2020-08-07 03:17发布

问题:

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>

回答1:

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>