BetterPackageReferences 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package BetterPackageReferences --version 1.0.0                
NuGet\Install-Package BetterPackageReferences -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BetterPackageReferences" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BetterPackageReferences --version 1.0.0                
#r "nuget: BetterPackageReferences, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BetterPackageReferences as a Cake Addin
#addin nuget:?package=BetterPackageReferences&version=1.0.0

// Install BetterPackageReferences as a Cake Tool
#tool nuget:?package=BetterPackageReferences&version=1.0.0                

Introduction

This project consists of a MSBuild targets file that provider greater control over NuGet dependencies when using SDK-style projects.

When packing SDK-style projects, there is no control over the resulting NuGet package's dependencies. The resulting package merely specifies the minimum version that is accepted. This is a problem because a later version of the dependency may contain breaking changes.

This project allows to specify stricter version requirements for each referenced NuGet package.

The following version range types are supported:

Type Description Resulting version range from 1.2.3
Exact Exact version match. Only this specific version will be allowed. [1.2.3]
SemVer Allow any minor version increment, but require the same major version. [1.2.3, 2.0.0)
Minimum Minimum version, inclusive. Any superior version will be allowed. 1.2.3

Installation

Simply install this package:

Install-Package BetterPackageReferences

By default, the following version range types are assumed:

Reference type Default value Control Variable
ProjectReference Exact DefaultVersionRangeTypeForProjectReferences
PackageReference SemVer DefaultVersionRangeTypeForPackageReferences

The defaults can be changed by setting the corresponding variable:

<PropertyGroup>
  <DefaultVersionRangeTypeForProjectReferences>SemVer</DefaultVersionRangeTypeForProjectReferences>
  <DefaultVersionRangeTypeForPackageReferences>Minimum</DefaultVersionRangeTypeForPackageReferences>
</PropertyGroup>

The version range type can also be configured for a specific reference by adding the VersionRangeType metadata:

<ProjectReference Include="..\OtherPackage\OtherPackage.csproj">
  <VersionRangeType>SemVer</VersionRangeType>
</ProjectReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.1">
  <VersionRangeType>Exact</VersionRangeType>
</PackageReference>

Example

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="BetterPackageReferences" Version="1.0.0">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>

    <PackageReference Include="AutoMapper" Version="7.0.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.1">
      <VersionRangeType>Exact</VersionRangeType>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\OtherPackage\OtherPackage.csproj" />
  </ItemGroup>

</Project>

Packing the above project will result in a NuGet package with the following metadata:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>MyPackage</id>
    <version>1.2.3</version>
    <dependencies>
      <group targetFramework=".NETStandard2.0">
        <dependency id="AutoMapper" version="[7.0.1, 8.0.0)" />
        <dependency id="Microsoft.Extensions.Logging.Abstractions" version="[3.0.1]" />
        <dependency id="OtherPackage" version="[1.2.3]" />
      </group>
    </dependencies>
  </metadata>
</package>
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 216 1/16/2024
1.0.7 2,131 1/15/2023
1.0.6 367 11/5/2021
1.0.4 464 12/14/2020
1.0.3 603 10/16/2020
1.0.2 526 9/8/2020
1.0.0 625 12/1/2019