Search code examples
c#visual-studio-2022.net-standard-2.0post-build-event

Read Assembly Version in a Post Build Event in Visual Studio 2022 targeting Netstandard2 .0


I have a project in netstandard 2.0 and I use post build events to create a nuget package and to push it automatically to my feed, that was working on .NetFramework. I know that with netstandard and netcore I can create them through the package function automatically but this is not an option because I override the assembly properties to increase automatically my version number.

Anyway I found a solution that in Visual Studio 2019 with .NetFramework 4.8 was working perfectly, nevertheless for Visual Studio 2022 with netStandard this macro does not work.

<Target Name="PostBuildMacros">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="Targets" />
  </GetAssemblyIdentity>
  <ItemGroup>
    <VersionNumber Include="@(Targets->'%(Version)')" />
  </ItemGroup>
</Target>

Does anyone have an idea why is not working? is there an alternative solution used for netcore or netstadard?

I appreciate your comments

PS: The version number increment is working, I double checked in the build folders.


Solution

  • After try and error i found that the attribute DependsOnTargets="PostBuildMacros" was missing, the following post build event solved my problem:

    <Target Name="PostBuildMacros">
        <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
            <Output TaskParameter="Assemblies" ItemName="Targets" />
        </GetAssemblyIdentity>
        <ItemGroup>
            <VersionNumber Include="@(Targets->'%(Version)')" />
        </ItemGroup>
    </Target>
    <Target Name="PostBuild" AfterTargets="PostBuildEvent" DependsOnTargets="PostBuildMacros">
        <Exec Command="$(ExecNuget) pack $(ProjectPath)&#xD;&#xA;$(ExecNuget) push -Source "YourSourceName" -ApiKey "YourKey" $(ProjectDir)$(ProjectName).@(VersionNumber).nupkg" />
    </Target>
    

    to avoid compiling errors replace "YourSourceName" and "YourKey" with your repository data