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 Code Generators โ Automate Updating Framework Dependencies
Install the @nx-dotnet/core Plugin
Make sure you have .NET installed on your machine. Consult the .NET docs for more details
โฏ
npm add -D @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:
1{
2 "version": 1,
3 "isRoot": true,
4 "tools": {}
5}
6
1{
2 "nugetPackages": {}
3}
4
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
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 "$(MSBuildProjectDirRelativePath)""/>
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.
โฏ
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.
โฏ
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:
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
:
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.