# WithBuildEnvironment

- Module: [Aspire.Hosting.Dotnet](/reference/api/go/aspire.hosting.dotnet.md)
- Version: `17.0.0`
- Kind: `method`
- Defined on: [`DotnetProjectResource`](/reference/api/go/aspire.hosting.dotnet/dotnetprojectresource.md)
- Source: [GitHub](https://github.com/microsoft/aspire/tree/bcfa151cdca6d0a573f22d5d9c23f9c727cb3b2f)

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

## 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 `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.

## Definition

```go
func (DotnetProjectResource) WithBuildEnvironment(
    name string,
    value string,
) DotnetProjectResource
```

## Parameters

- `name` (`string`) -- The name of the environment variable.
- `value` (`string`) -- The value of the environment variable.

## Returns

[`DotnetProjectResource`](/reference/api/go/aspire.hosting.dotnet/dotnetprojectresource.md) -- A reference to the `IResourceBuilder` for chaining additional configuration.

Returns a handle; failures surface later via `Err()`.

## Applies to

- [`DotnetProjectResource`](/reference/api/go/aspire.hosting.dotnet/dotnetprojectresource.md)
