# Service Methods

- Package: [Aspire.Hosting.Docker](/reference/api/csharp/aspire.hosting.docker.md)
- Type: [Service](/reference/api/csharp/aspire.hosting.docker/service.md)
- Kind: `Methods`
- Members: `4`

Represents a Docker Compose service definition.

## AddConfig(ConfigReference)

- Name: `AddConfig(ConfigReference)`
- Returns: [Service](/reference/api/csharp/aspire.hosting.docker/service.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Docker/Resources/ComposeNodes/Service.cs#L510-L511)

Adds a configuration reference to the service's list of configurations. This method allows you to include external configuration resources that the service can utilize at runtime.

```csharp
public sealed class Service
{
    public Service AddConfig(
        ConfigReference config)
    {
        // ...
    }
}
```

## Parameters

- `config` ([ConfigReference](/reference/api/csharp/aspire.hosting.docker/configreference.md))
  The config reference to add

## Returns

[Service](/reference/api/csharp/aspire.hosting.docker/service.md) -- The updated [Service](/reference/api/csharp/aspire.hosting.docker/service.md) instance with the added environmental variable.

## AddEnvironmentalVariable(string, string?)

- Name: `AddEnvironmentalVariable(string, string?)`
- Returns: [Service](/reference/api/csharp/aspire.hosting.docker/service.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Docker/Resources/ComposeNodes/Service.cs#L496-L498)

Adds an environmental variable to the service's environment dictionary. If the specified value is null, it assigns an empty string as the value.

```csharp
public sealed class Service
{
    public Service AddEnvironmentalVariable(
        string key,
        string? value)
    {
        // ...
    }
}
```

## Parameters

- `key` (`string`)
  The key for the environmental variable.
- `value` (`string?`)
  The value of the environmental variable. If null, an empty string will be used.

## Returns

[Service](/reference/api/csharp/aspire.hosting.docker/service.md) -- The updated [Service](/reference/api/csharp/aspire.hosting.docker/service.md) instance with the added environmental variable.

## AddVolume(Volume)

- Name: `AddVolume(Volume)`
- Returns: [Service](/reference/api/csharp/aspire.hosting.docker/service.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Docker/Resources/ComposeNodes/Service.cs#L471-L472)

Adds a volume to the service's list of volumes. If the volumes collection is null, it initializes a new collection before adding the volume.

```csharp
public sealed class Service
{
    public Service AddVolume(
        Volume volume)
    {
        // ...
    }
}
```

## Parameters

- `volume` ([Volume](/reference/api/csharp/aspire.hosting.docker/volume.md))
  The volume to be added to the service.

## Returns

[Service](/reference/api/csharp/aspire.hosting.docker/service.md) -- The updated [Service](/reference/api/csharp/aspire.hosting.docker/service.md) instance with the added volume.

## AddVolumes(IEnumerable<Volume>)

- Name: `AddVolumes(IEnumerable<Volume>)`
- Returns: [Service](/reference/api/csharp/aspire.hosting.docker/service.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Docker/Resources/ComposeNodes/Service.cs#L483-L484)

Adds multiple volumes to the service's list of volumes. If the volumes collection is empty, the provided volumes will be appended to the existing collection.

```csharp
public sealed class Service
{
    public Service AddVolumes(
        IEnumerable<Volume> volumes)
    {
        // ...
    }
}
```

## Parameters

- `volumes` ([IEnumerable<Volume>](/reference/api/csharp/aspire.hosting.docker/volume.md))
  A collection of volumes to be added to the service.

## Returns

[Service](/reference/api/csharp/aspire.hosting.docker/service.md) -- The updated [Service](/reference/api/csharp/aspire.hosting.docker/service.md) instance with the added volumes.
