# WithLifetime

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

Sets the lifetime behavior of the container resource.

## Remarks

Prefer `WithPersistentLifetime` or
`WithSessionLifetime` for new code.
Marking a container resource to have a `Persistent` lifetime.
```
var builder = DistributedApplication.CreateBuilder(args);
builder.AddContainer("mycontainer", "myimage")
.WithPersistentLifetime();
builder.Build().Run();
```

## Definition

```go
func (ContainerResource) WithLifetime(lifetime ContainerLifetime) ContainerResource
```

## Parameters

- `lifetime` ([`ContainerLifetime`](/reference/api/go/aspire.hosting/containerlifetime.md)) -- The lifetime behavior of the container resource. The default behavior is `Session`.

## Returns

[`ContainerResource`](/reference/api/go/aspire.hosting/containerresource.md) -- The resource builder.

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

## Applies to

- [`ContainerResource`](/reference/api/go/aspire.hosting/containerresource.md)
