# Dotnet Operations
Build, test, and publish .NET projects using the dotnet CLI.
## Operations
### Dotnet.SdkInstall
Install .NET SDK globally in the container. Skips installation if already present (for warm containers). Calling this method disables automatic SDK installation for subsequent operations. *Note: Dotnet operations (Build, Test, Restore, Publish) automatically install the SDK if not present.*
```csharp
Dotnet.SdkInstall(); // Installs .NET SDK 9.0
Dotnet.SdkInstall("8.0"); // Installs .NET SDK 8.0
```
### Dotnet.Project
Creates a reference to a .NET project file (.csproj). Used with Dotnet and Ef operations. The returned ProjectRef has properties: Path, Name, Directory, and Version (reads from csproj).
```csharp
var app = Dotnet.Project("./src/MyApp/MyApp.csproj");
Dotnet.Build(app);
Log.Info($"Building {app.Name} version {app.Version}");
```
### Dotnet.Restore
Restore NuGet packages for a project. Automatically installs .NET SDK if not present.
```csharp
Dotnet.Restore(App);
Dotnet.Restore(App, o => o.NoCache = true);
```
### Dotnet.Build
Compile a project with optional configuration. Automatically installs .NET SDK if not present.
```csharp
Dotnet.Build(App);
Dotnet.Build(App, o => o.Configuration = Configuration.Release);
```
### Dotnet.Test
Run unit tests for a project. Automatically installs .NET SDK if not present.
```csharp
Dotnet.Test(Tests);
Dotnet.Test(Tests, o => o.Filter = "Category=Unit");
```
### Dotnet.Publish
Create deployment artifacts with full publish options. Automatically installs .NET SDK if not present.
```csharp
Dotnet.Publish(App);
Dotnet.Publish(App, o => o
.Output(Root / "dist")
.WithConfiguration(Configuration.Release)
.WithRuntime("linux-x64")
.AsSelfContained()
.AsSingleFile());
```
### Dotnet.Tool
Create a reference to a .NET CLI tool for installation.
```csharp
var efTool = Dotnet.Tool("dotnet-ef");
var efTool = Dotnet.Tool("dotnet-ef", "9.0.0");
```
## Example
Build and publish a .NET application.
```csharp
// Define project references
var App = Dotnet.Project("./src/App/App.csproj");
var Tests = Dotnet.Project("./tests/App.Tests/App.Tests.csproj");
// Restore, build, and test
Dotnet.Restore(App);
Dotnet.Build(App, o => o.Configuration = Configuration.Release);
Dotnet.Test(Tests);
// Publish as self-contained single file
Dotnet.Publish(App, o => o
.Output(Root / "dist")
.WithConfiguration(Configuration.Release)
.WithRuntime("linux-x64")
.AsSelfContained()
.AsSingleFile());
```
## Options Reference
### Dotnet.Restore Options
| Option | Description |
|--------|-------------|
| `WithRuntime(string)` | Set target runtime identifier (e.g., "linux-x64", "win-x64", "osx-arm64"). Downloads runtime-specific packages. |
| `NoCache` | Bypass the NuGet cache, forcing fresh downloads of all packages. Useful when debugging package resolution issues. |
### Dotnet.Build Options
| Option | Description |
|--------|-------------|
| `Configuration` | Build configuration: `Configuration.Debug` or `Configuration.Release`. Debug includes symbols and disables optimizations. Release enables optimizations for production. |
| `NoRestore` | Skip the implicit restore before building. Use when you've already run `Dotnet.Restore()` separately. |
### Dotnet.Test Options
| Option | Description |
|--------|-------------|
| `Configuration` | Build configuration for running tests. Tests typically run in Debug for better diagnostics. |
| `NoRestore` | Skip restore before testing. Use when dependencies are already restored. |
| `NoBuild` | Skip build before testing. Use when the project is already built. Implies `NoRestore`. |
| `Filter` | Test filter expression to run specific tests. Examples: `"Category=Unit"`, `"FullyQualifiedName~MyNamespace"`, `"Name=MyTest"`. |
### Dotnet.Publish Options
| Option | Description |
|--------|-------------|
| `Output(path)` | Output directory for published artifacts. Accepts `BuildPath` or `string`. |
| `WithConfiguration(Configuration)` | Build configuration. Use `Configuration.Release` for production deployments. |
| `WithRuntime(string)` | Target runtime identifier. Required for self-contained deployments. Common values: `"linux-x64"`, `"linux-arm64"`, `"win-x64"`, `"osx-x64"`, `"osx-arm64"`. |
| `AsSelfContained(bool)` | Include the .NET runtime with the application. Creates a standalone deployment that doesn't require .NET to be installed. Defaults to `true` when called. |
| `AsSingleFile(bool)` | Bundle the entire application into a single executable. Makes deployment simpler but increases startup time slightly. |
| `SkipRestore()` | Skip restore before publishing. Use when dependencies are already restored. |
| `SkipBuild()` | Skip build before publishing. Use when the project is already built. |