ravibpatel / ILRepack.Lib.MSBuild.Task

MSBuild task for ILRepack which is an open-source alternative to ILMerge.
Other
107 stars 30 forks source link

Build task isn't re-signing/strong-naming my assembly #36

Closed mamift closed 8 months ago

mamift commented 1 year ago

Which file do you specifically place the <KeyFile>$(ProjectDir)key.snk</KeyFile> element when you need to sign/strong name the resultant merged assembly?

I've copied the contents of this file into my project: https://github.com/ravibpatel/ILRepack.Lib.MSBuild.Task/blob/caf641eab2567434562e37768aba04559e6e0b24/ILRepack.Lib.MSBuild.Task/ILRepack.Config.props

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <KeyFile>$(ProjectDir)key.snk</KeyFile>
  </PropertyGroup>
</Project>

And the SNK file exists (key.snk) under the $(ProjectDir). But my output DLL is still not being signed; when I check the key with the sn.exe tool, it says:

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Failed to convert key to token -- Invalid assembly public k

Is it in the csproj file, the ILRepack.Config.props or the ILRepack.targets file?

I've also tried adding it to the ILRepack.targets file, then I get an error: 14/10/2022 12:13:07 PM Critical Project load failed| [MSB4067] The element <#text> beneath element <KeyFile> is unrecognized. C:\Users\mmiftah\source\repos\solution\project\ILRepack.targets at (18:3) I've also tried adding it to the CSPROJ file, but the build output still doesn't change.

mamift commented 1 year ago

OK I've solved this on my own.

Instead of putting the key details in it's own ILRepack.Config.Props file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <KeyFile>$(ProjectDir)key.snk</KeyFile>
  </PropertyGroup>
</Project>

It works to put a KeyFile XML attribute in the <ILRepack /> element inside of the ILRepack.targets file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="ILRepacker" AfterTargets="Build" Condition="'$(Configuration)' == 'Release' Or '$(Configuration)' == 'Debug'">
    <ItemGroup>
      <InputAssemblies Include="$(OutputPath)\$(AssemblyName).dll" />
      <InputAssemblies Include="$(OutputPath)\XObjectsCore.dll" />
    </ItemGroup>
    <ItemGroup>
      <!--<DoNotInternalizeAssemblies Include="System.ValueTuple.dll" />-->
    </ItemGroup>
    <ILRepack Parallel="true" Internalize="true" InternalizeExclude="@(DoNotInternalizeAssemblies)" InputAssemblies="@(InputAssemblies)" TargetKind="Dll" OutputFile="$(OutputPath)\$(AssemblyName).dll" 
    KeyFile="$(ProjectDir)key.snk" />
  </Target>
</Project>

Please update the README to indicate this.

ravibpatel commented 8 months ago

As mentioned in the doc, "This configuration option only applies if you are using the default targets file provided in NuGet package." So if you are using custom targets file then it won't work.