Add a New .NET Project

Supported Features

Because we are using an Nx plugin for .NET, all the features of Nx are available.

โœ… Run Tasks โœ… Cache Task Results โœ… Share Your Cache โœ… Explore the Graph โœ… Distribute Task Execution โœ… Integrate with Editors โœ… Automate Updating Nx โœ… Enforce Module Boundaries โœ… Use Task Executors โœ… Use Code Generators โœ… Automate Updating Framework Dependencies

Install the @nx-dotnet/core Plugin

Have .NET already installed?

Make sure you have .NET installed on your machine. Consult the .NET docs for more details

โฏ

npm i --save-dev @nx-dotnet/core

Set up your workspace

Use the init generator to scaffold out some root level configuration files.

โฏ

nx g @nx-dotnet/core:init

This generates the following files:

.config/dotnet-tools.json
1{ 2 "version": 1, 3 "isRoot": true, 4 "tools": {} 5} 6
.nx-dotnet.rc.json
1{ 2 "nugetPackages": {} 3} 4
Directory.Build.props
1<!-- 2 This file is imported early in the build order. 3 Use it to set default property values that can be overridden in specific projects. 4--> 5<Project> 6 <PropertyGroup> 7 <!-- Output path configuration --> 8 <RepoRoot>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</RepoRoot> 9 <ProjectRelativePath>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectDirectory)))</ProjectRelativePath> 10 <BaseOutputPath>$(RepoRoot)dist/$(ProjectRelativePath)</BaseOutputPath> 11 <OutputPath>$(BaseOutputPath)</OutputPath> 12 <BaseIntermediateOutputPath>$(RepoRoot)dist/intermediates/$(ProjectRelativePath)/obj</BaseIntermediateOutputPath> 13 <IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath> 14 <AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath> 15 </PropertyGroup> 16 <PropertyGroup> 17 <RestorePackagesWithLockFile>false</RestorePackagesWithLockFile> 18 </PropertyGroup> 19</Project> 20
Directory.Build.targets
1<!-- 2 This file is imported late in the build order. 3 Use it to override properties and define dependent properties. 4--> 5<Project> 6 <PropertyGroup> 7 <MSBuildProjectDirRelativePath>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectDirectory)))</MSBuildProjectDirRelativePath> 8 <NodeModulesRelativePath>$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), $(RepoRoot)))</NodeModulesRelativePath> 9 </PropertyGroup> 10 <Target Name="CheckNxModuleBoundaries" BeforeTargets="Build"> 11 <Exec Command="node $(NodeModulesRelativePath)/node_modules/@nx-dotnet/core/src/tasks/check-module-boundaries.js --project-root &quot;$(MSBuildProjectDirRelativePath)&quot;"/> 12 </Target> 13</Project> 14

Create an Application

Use the app generator to create a new .NET app. For this demo, use the nx path naming convention and the web-api project template.

Directory Flag Behavior Changes

The command below uses the as-provided directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived option, omit the --directory flag. See the as-provided vs. derived documentation for more details.

โฏ

nx g @nx-dotnet/core:app my-api --directory=apps/my-api --test-template nunit --language C#

Serve the API by running

โฏ

nx serve my-api

Create a Library

To create a new library, run the library generator. Use the classlib template.

Directory Flag Behavior Changes

The command below uses the as-provided directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived option, omit the --directory flag. See the as-provided vs. derived documentation for more details.

โฏ

nx g @nx-dotnet/core:lib dotnet-lib --directory=libs/dotnet-lib

We also want to add a project reference from my-api to dotnet-lib using the project-reference generator:

โฏ

nx generate @nx-dotnet/core:project-reference --project=my-api --reference=dotnet-lib

Now we can move the WeatherForecast.cs file out of the my-api folder and into the dotnet-lib folder. We also need to update the namespace for the file like this:

libs/dotnet-lib/WeatherForecast.cs
1namespace DotnetLib; 2 3public class WeatherForecast 4{ 5 public DateOnly Date { get; set; } 6 7 public int TemperatureC { get; set; } 8 9 public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); 10 11 public string? Summary { get; set; } 12} 13

Now use the dotnet-lib version of WeatherForecast in my-api:

apps/my-api/Controllers/WeatherForecastController.cs
1using Microsoft.AspNetCore.Mvc; 2using DotnetLib; 3 4namespace MyApi.Controllers; 5 6// the rest of the file is unchanged 7

Now when you serve your api it will use the class from the library.

More Documentation