how to update all the reference(Target, package reference) of a .net standard app's csproj of a specific package during installation

Aditya Dalai 20 Reputation points
2023-11-21T06:48:05.8133333+00:00

currently when I install a nuget package inside .net standard app i can only able update the PackageReference indie the csproj

<PackageReference Include="sip-si" Version="4.0.10" />

rest of all the referance are not able to update the updated version inside the same csproj file ex

<Target Name="CopyReferenceDll" AfterTargets="Build">
	<Copy SourceFiles="..\..\_Common\packages\sip-si.4.0.9\lib\netstandard2.0\Wrapper.dll" DestinationFolder="$(TargetDir)" />
	<Copy SourceFiles="..\..\_Common\packages\sip-si.4.0.9\lib\netstandard2.0\Wrapper.pdb" DestinationFolder="$(TargetDir)" />
  </Target>


	<Import Project="..\..\_Common\packages\sip-si.4.0.9\build\sip5-simu-dsi.targets" Condition="Exists('..\..\_Common\packages\sip-si.4.0.9\build\sip5-simu-dsi.targets')" />

and it gives an error after installation and i need to update them manually is there any way to modify the sip-si.nuspec file so after installation of the nuget pack the target section also will get updated with the particular version

and I saw it is not possible to run a powershell script during installation of a nuget package . could some body help me what is the best way to handle this ,I mean how to update the .csproj file during installation.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,459 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,239 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,385 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jalpa Panchal-MSFT 480 Reputation points Microsoft Vendor
    2023-11-21T09:47:58.3+00:00

    Hi @Aditya Dalai ,

    Unfortunately, there isn't a direct way to automatically update these file paths when you update your NuGet package version, as NuGet doesn't support running custom scripts during package installation in .NET Core and .NET Standard projects because of security reasons.

    However, you could try below ways:

    1)Instead of hardcoding the version number in your .csproj file, you could use an MSBuild property. This allows you to define the version in one place and use that property throughout your project file.

    <PropertyGroup>
    	<SipSiVersion>4.0.10</SipSiVersion>
    </PropertyGroup>
        
    <PackageReference Include="sip-si" Version="$(SipSiVersion)" />
        
    <Target Name="CopyReferenceDll" AfterTargets="Build">
    	<Copy SourceFiles="..\..\_Common\packages\sip-si.$(SipSiVersion)\lib\netstandard2.0\Wrapper.dll" DestinationFolder="$(TargetDir)" />
    </Target>
    

    2)For the DLL and PDB files, you can use wildcard versioning in the file path if your package versions follow a consistent naming convention.

    <Copy SourceFiles="..\..\_Common\packages\sip-si.*\lib\netstandard2.0\Wrapper.dll" DestinationFolder="$(TargetDir)" />
    

    3)If you have control over the NuGet package (sip-si), you can include a .targets file within the package that contains the necessary MSBuild logic to copy the files and reference them correctly using the property.

    4)Although NuGet doesn't allow running PowerShell scripts during package installation anymore, you could use a custom MSBuild target that runs after package restoration to update the references.

    Best regards,
    Jalpa Panchal


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful