# DotnetProjectHostingExtensions Methods

- Package: [Aspire.Hosting.Dotnet](/reference/api/csharp/aspire.hosting.dotnet.md)
- Type: [DotnetProjectHostingExtensions](/reference/api/csharp/aspire.hosting.dotnet/dotnetprojecthostingextensions.md)
- Kind: `Methods`
- Members: `9`

Provides extension methods for adding SDK-style .NET projects and file-based C# apps (by path) to an `Hosting.IDistributedApplicationBuilder`.

<a id="adddotnetproject"></a>
<a id="adddotnetproject-idistributedapplicationbuilder-string-string"></a>

## AddDotnetProject(IDistributedApplicationBuilder, string, string)

- Name: `AddDotnetProject(IDistributedApplicationBuilder, string, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L306-L310)

Adds an SDK-style .NET project or file-based C# app to the application model.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> AddDotnetProject(
        this IDistributedApplicationBuilder builder,
        string name,
        string path)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used for service discovery when referenced in a dependency.
- `path` (`string`)
  The path to the file-based app file, project file, or project directory.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This overload adds a path to a file-based app ( `.cs`), an SDK-style C#, F#, or Visual Basic project ( `.csproj`, `.fsproj`, or `.vbproj`), or a directory containing exactly one supported project. Directories containing multiple supported projects require an explicit project filename. If the path is not an absolute path then it will be computed relative to the AppHost directory.

Visual Basic projects prefer `My Project/launchSettings.json` and fall back to `Properties/launchSettings.json`. F# scripts and non-SDK project systems are not supported.

Add a file-based app to the app model via a file path.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

builder.AddDotnetProject("inventoryservice", @"..\InventoryService.cs");

builder.Build().Run();
```

## ATS metadata

### Ignored by ATS

- Reason: Polyglot AppHosts use the internal addDotnetProject dispatcher export.

<a id="adddotnetproject-idistributedapplicationbuilder-string-string-action-projectresourceoptions"></a>

## AddDotnetProject(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>)

- Name: `AddDotnetProject(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L361-L522)

Adds an SDK-style .NET project or file-based C# app to the application model.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> AddDotnetProject(
        this IDistributedApplicationBuilder builder,
        string name,
        string path,
        Action<ProjectResourceOptions> configure)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used for service discovery when referenced in a dependency.
- `path` (`string`)
  The path to the file-based app file, project file, or project directory.
- `configure` (`Action<ProjectResourceOptions>`)
  An action to configure the .NET project resource options.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This overload adds a path to a file-based app ( `.cs`), an SDK-style C#, F#, or Visual Basic project ( `.csproj`, `.fsproj`, or `.vbproj`), or a directory containing exactly one supported project. Directories containing multiple supported projects require an explicit project filename. If the path is not an absolute path then it will be computed relative to the AppHost directory.

Visual Basic projects prefer `My Project/launchSettings.json` and fall back to `Properties/launchSettings.json`. F# scripts and non-SDK project systems are not supported.

Add a file-based app to the app model via a file path.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

builder.AddDotnetProject("inventoryservice", @"..\InventoryService.cs", o => o.LaunchProfileName = "https");

builder.Build().Run();
```

## ATS metadata

### Ignored by ATS

- Reason: Polyglot AppHosts use the internal addDotnetProject dispatcher export.

<a id="disableforwardedheaders"></a>
<a id="disableforwardedheaders-iresourcebuilder-dotnetprojectresource"></a>

## DisableForwardedHeaders(IResourceBuilder<DotnetProjectResource>)

- Name: `DisableForwardedHeaders(IResourceBuilder<DotnetProjectResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L218)

Disables automatic forwarded-header configuration when publishing the .NET project.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> DisableForwardedHeaders(
        this IResourceBuilder<DotnetProjectResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<DotnetProjectResource>`)
  The .NET project resource builder.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- The same builder for chaining additional configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` is null.

## ATS metadata

### ATS export

- Capability ID: `Aspire.Hosting.Dotnet/disableDotnetProjectForwardedHeaders`
- Generated method name override: `disableForwardedHeaders`

<a id="getprojectmetadata"></a>
<a id="getprojectmetadata-dotnetprojectresource"></a>

## GetProjectMetadata(DotnetProjectResource)

- Name: `GetProjectMetadata(DotnetProjectResource)`
- Modifiers: `extension`
- Returns: `IProjectMetadata`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L253-L255)

Gets the project metadata for the .NET project resource.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IProjectMetadata GetProjectMetadata(
        this DotnetProjectResource projectResource)
    {
        // ...
    }
}
```

## Parameters

- `projectResource` ([DotnetProjectResource](/reference/api/csharp/aspire.hosting.dotnet/dotnetprojectresource.md))
  The .NET project resource.

## Returns

`IProjectMetadata` -- The project metadata.

## Exceptions

- `ArgumentNullException` -- Thrown when `projectResource` is null.
- `InvalidOperationException` -- The resource does not carry exactly one stable project metadata annotation.

## Remarks

Project metadata cannot be replaced after project defaults have been applied because launch settings, endpoints, and rebuild behavior are derived from that annotation.

## ATS metadata

### Ignored by ATS

- Reason: Project metadata is a .NET-specific contract and is not part of the ATS surface.

<a id="withbuildenvironment"></a>
<a id="withbuildenvironment-iresourcebuilder-dotnetprojectresource-string-string"></a>

## WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, string, string)

- Name: `WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, string, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L77-L81)

Adds an environment variable to the build process for a .NET project.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment(
        this IResourceBuilder<DotnetProjectResource> builder,
        string name,
        string value)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<DotnetProjectResource>`)
  The .NET project resource builder.
- `name` (`string`)
  The name of the environment variable.
- `value` (`string`)
  The value of the environment variable.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining additional configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder`, `name`, or `value` is null.
- `ArgumentException` -- Thrown when `name` is empty.
- `DistributedApplicationException` -- Thrown when `builder` represents a file-based C# app.

## Remarks

This method supports SDK-style project files ( `.csproj`, `.fsproj`, and `.vbproj`) only. File-based C# apps ( `.cs`) do not support build-only environment variables.

The variable is available while Aspire builds the project but is not added to the environment of the launched project. Use `WithEnvironment` separately when the same variable is also needed at runtime.

Configuring a build environment causes Aspire to build this project separately from traversal groups.

IDE launches require support for `KnownLaunchConfigurationTypes.ProjectWithExternalBuild` so the launcher can consume the same build context. Aspire rejects legacy project-only IDE sessions because they cannot reliably locate output produced with project-specific build settings.

When publishing a container, do not use this API to set MSBuild properties that control the output artifact's identity, destination, format, or target platform. Aspire rejects those properties because downstream publishing steps use the values configured with `WithContainerBuildOptions`.

Do not use this API for secrets. Aspire carries the value in build process environments and, for external-build-capable IDEs, launch metadata. The value can appear in build diagnostics. Protected temporary MSBuild response files preserve global-property semantics without exposing values in process command lines, but they are not a general-purpose secret transport.

## Examples

Configure an environment variable that selects a custom build output:

```csharp
builder.AddDotnetProject("worker", "../Worker/Worker.csproj")
    .WithBuildEnvironment("BUILD_FLAVOR", "custom");
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

<a id="withbuildenvironment-iresourcebuilder-dotnetprojectresource-action-environmentcallbackcontext"></a>

## WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Action<EnvironmentCallbackContext>)

- Name: `WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Action<EnvironmentCallbackContext>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs)

Adds a callback that configures build-only environment variables for a .NET project.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment(
        this IResourceBuilder<DotnetProjectResource> builder,
        Action<EnvironmentCallbackContext> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<DotnetProjectResource>`)
  The .NET project resource builder.
- `callback` (`Action<EnvironmentCallbackContext>`)
  The callback that configures the build environment.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining additional configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `callback` is null.
- `DistributedApplicationException` -- Thrown when `builder` represents a file-based C# app.

## Remarks

This method supports SDK-style project files ( `.csproj`, `.fsproj`, and `.vbproj`) only. File-based C# apps ( `.cs`) do not support build-only environment variables.

IDE launches require support for `KnownLaunchConfigurationTypes.ProjectWithExternalBuild` so the launcher can consume the same build context. Aspire rejects legacy project-only IDE sessions because they cannot reliably locate output produced with project-specific build settings.

When publishing a container, do not use this API to set MSBuild properties that control the output artifact's identity, destination, format, or target platform. Aspire rejects those properties because downstream publishing steps use the values configured with `WithContainerBuildOptions`.

Values configured by this callback are not added to the environment of the launched project. Do not use this API for secrets because Aspire carries the values in build process environments and, for external-build-capable IDEs, launch metadata. Protected temporary MSBuild response files preserve global-property semantics without exposing values in process command lines, but the values can appear in build diagnostics.

## ATS metadata

### Ignored by ATS

- Reason: Raw Action delegate callbacks are not ATS-compatible.

<a id="withbuildenvironment-iresourcebuilder-dotnetprojectresource-func-environmentcallbackcontext-task"></a>

## WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EnvironmentCallbackContext, Task>)

- Name: `WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EnvironmentCallbackContext, Task>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs)

Adds an asynchronous callback that configures build-only environment variables for a .NET project.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment(
        this IResourceBuilder<DotnetProjectResource> builder,
        Func<EnvironmentCallbackContext, Task> callback)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<DotnetProjectResource>`)
  The .NET project resource builder.
- `callback` (`Func<EnvironmentCallbackContext, Task>`)
  The callback that configures the build environment.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining additional configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `callback` is null.
- `DistributedApplicationException` -- Thrown when `builder` represents a file-based C# app.

## Remarks

This method supports SDK-style project files ( `.csproj`, `.fsproj`, and `.vbproj`) only. File-based C# apps ( `.cs`) do not support build-only environment variables.

IDE launches require support for `KnownLaunchConfigurationTypes.ProjectWithExternalBuild` so the launcher can consume the same build context. Aspire rejects legacy project-only IDE sessions because they cannot reliably locate output produced with project-specific build settings.

When publishing a container, do not use this API to set MSBuild properties that control the output artifact's identity, destination, format, or target platform. Aspire rejects those properties because downstream publishing steps use the values configured with `WithContainerBuildOptions`.

Values configured by this callback are not added to the environment of the launched project. Do not use this API for secrets because Aspire carries the values in build process environments and, for external-build-capable IDEs, launch metadata. Protected temporary MSBuild response files preserve global-property semantics without exposing values in process command lines, but the values can appear in build diagnostics.

## ATS metadata

### Ignored by ATS

- Reason: Raw Func delegate callbacks are not ATS-compatible.

<a id="withendpointsinenvironment"></a>
<a id="withendpointsinenvironment-iresourcebuilder-dotnetprojectresource-func-endpointannotation-bool"></a>

## WithEndpointsInEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EndpointAnnotation, bool>)

- Name: `WithEndpointsInEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EndpointAnnotation, bool>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L236)

Configures which endpoints contribute environment variables for the .NET project.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> WithEndpointsInEnvironment(
        this IResourceBuilder<DotnetProjectResource> builder,
        Func<EndpointAnnotation, bool> filter)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<DotnetProjectResource>`)
  The .NET project resource builder.
- `filter` (`Func<EndpointAnnotation, bool>`)
  A filter that returns true for endpoints whose environment variables should be injected.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- The same builder for chaining additional configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `filter` is null.

## Remarks

Filters compose: an endpoint is included only when every configured filter accepts it.

## ATS metadata

### Ignored by ATS

- Reason: Uses Func<EndpointAnnotation, bool>; polyglot AppHosts use the endpoint-name dispatcher.

<a id="withreplicas"></a>
<a id="withreplicas-iresourcebuilder-dotnetprojectresource-int"></a>

## WithReplicas(IResourceBuilder<DotnetProjectResource>, int)

- Name: `WithReplicas(IResourceBuilder<DotnetProjectResource>, int)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<DotnetProjectResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/2d325b13709752ba6d9a943635c5e2940a2530c0/src/Aspire.Hosting.Dotnet/DotnetProjectHostingExtensions.cs#L205)

Configures the number of replicas of the .NET project.

```csharp
public static class DotnetProjectHostingExtensions
{
    public static IResourceBuilder<DotnetProjectResource> WithReplicas(
        this IResourceBuilder<DotnetProjectResource> builder,
        int replicas)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<DotnetProjectResource>`)
  The .NET project resource builder.
- `replicas` (`int`)
  The number of replicas.

## Returns

`IResourceBuilder<DotnetProjectResource>` -- The same builder for chaining additional configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` is null.

## ATS metadata

### ATS export

- Capability ID: `Aspire.Hosting.Dotnet/withDotnetProjectReplicas`
- Generated method name override: `withReplicas`
