Search code examples
c#.netmavennuget

how are dependencies managed in C#?


how are dependencies managed in C# ? For example in java there is maven (pom.xml), and in JS there is npm (package.json), and in C# there is nuget... but what is the alternative of pom.xml in C# ?

Is that maybe .csproj file ?


Solution

  • The package manager for .NET C# is nuget,

    See: https://learn.microsoft.com/en-us/nuget/what-is-nuget

    .NET used to have a package.json file containing the packages for a project, but nowadays the packages are defined right in to the .csproj file. For example:

    <ItemGroup>
        <PackageReference Include="AspNetCore.HealthChecks.Hangfire" Version="6.0.2" />
        <PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.2" />
        <PackageReference Include="AspNetCore.HealthChecks.System" Version="6.0.5" />
        <PackageReference Include="AspNetCore.HealthChecks.Uris" Version="6.0.3" />
        <PackageReference Include="AutoMapper" Version="12.0.0" />
        <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
        <PackageReference Include="Hangfire.AspNetCore" Version="1.7.32" />
        <PackageReference Include="Hangfire.Console" Version="1.4.2" />
        <PackageReference Include="Hangfire.SqlServer" Version="1.7.32" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.11" />
    </ItemGroup>
    

    Packages can be managed right in visual studio by the nuget package manager:

    enter image description here enter image description here

    Same can be done for the whole solution: enter image description here

    From the command line you can work with nuget.exe, or dotnet.exe;

    nuget install Flurl -Version 3.0.6
    dotnet add package Flurl --version 3.0.6
    

    There are also the nuget package manager console intergrated into visual studio:

    enter image description here

    You can select a default project from the dropdown, and then call Install-Package.

    Generally most library repositories on github have either a link to the package on nuget.org or have a snippet to directly install the package via the nuget package manager console.

    For example a random library flurl : enter image description here