Skip to content
DocsTry Aspire
DocsTry

Set up Garnet in the AppHost

Garnet logo

This article is the reference for the Aspire Garnet Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to model a Garnet resource in your AppHost project.

If you’re new to the Garnet integration, start with the Get started with Garnet integrations guide. For how consuming apps read the connection information this page exposes, see Connect to Garnet.

To start building an Aspire app that uses Garnet, install the 📦 Aspire.Hosting.Garnet NuGet package:

Terminal
aspire add garnet

Learn more about aspire add in the command reference.

This updates your aspire.config.json with the Garnet hosting integration package:

aspire.config.json
{
"packages": {
"Aspire.Hosting.Garnet": "13.5.3"
}
}

Once you’ve installed the hosting integration in your AppHost project, you can add a Garnet resource as shown in the following examples:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const cache = await builder.addGarnet("cache");
await builder.addNodeApp("api", "./api", "index.js")
.withReference(cache);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the ghcr.io/microsoft/garnet image, it creates a new Garnet instance on your local machine.

  2. The Garnet resource is configured with a randomly generated password by default. To set an explicit password, see Add Garnet resource with parameters.

  3. The AppHost reference call configures a connection in the consuming project named after the referenced Garnet resource, such as cache in the preceding example.

Tags reflect the latest defaults on the microsoft/aspire main branch, and may be newer than the version pinned by the package you install.

Add a data volume to the Garnet resource as shown in the following examples:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const cache: GarnetResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addGarnet(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): GarnetResource (+1 overload)

Adds a Garnet container resource to the application model.

addGarnet
("cache");
await
const cache: GarnetResource
cache
.
GarnetResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
}): GarnetResource (+1 overload)

Adds a named volume for the data folder to a Garnet container resource and enables Garnet persistence.

withDataVolume
({
isReadOnly?: boolean | undefined
isReadOnly
: false });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: GarnetResource
cache
);
// After adding all resources, run the app...

The data volume is used to persist Garnet data outside the lifecycle of its container. The data volume is mounted at the /data path in the Garnet container, and when a name parameter isn’t provided, the name is generated at random. Calling WithDataVolume (or withDataVolume) also enables Garnet persistence so the in-memory state survives container restarts. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add a data bind mount to the Garnet resource as shown in the following examples:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const cache: GarnetResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addGarnet(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): GarnetResource (+1 overload)

Adds a Garnet container resource to the application model.

addGarnet
("cache");
await
const cache: GarnetResource
cache
.
GarnetResource.withDataBindMount(source: string, options?: {
isReadOnly?: boolean;
}): GarnetResource (+1 overload)

Adds a bind mount for the data folder to a Garnet container resource and enables Garnet persistence.

withDataBindMount
("/Garnet/Data", {
isReadOnly?: boolean | undefined
isReadOnly
: false });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: GarnetResource
cache
);
// After adding all resources, run the app...

Data bind mounts rely on the host machine’s filesystem to persist Garnet data across container restarts. The data bind mount is mounted at the C:\Garnet\Data on Windows (or /Garnet/Data on Unix) path on the host machine in the Garnet container. As with WithDataVolume, this call also enables persistence. For more information on data bind mounts, see Docker docs: Bind mounts.

To configure Garnet snapshot persistence explicitly, call WithPersistence (or withPersistence) alongside a data volume or bind mount:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const cache: GarnetResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addGarnet(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): GarnetResource (+1 overload)

Adds a Garnet container resource to the application model.

addGarnet
("cache");
await
const cache: GarnetResource
cache
.
GarnetResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
}): GarnetResource (+1 overload)

Adds a named volume for the data folder to a Garnet container resource and enables Garnet persistence.

withDataVolume
();
await
const cache: GarnetResource
cache
.
GarnetResource.withPersistence(options?: {
interval?: timespan;
}): GarnetResource (+1 overload)

Configures a Garnet container resource for persistence.

withPersistence
({
interval?: timespan | undefined
interval
: 5 * 60 * 1000,
});
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: GarnetResource
cache
);
// After adding all resources, run the app...

The preceding code adds explicit persistence to the Garnet resource by snapshotting data at the configured interval. The C# AppHost accepts a TimeSpan for interval and a keysChangedThreshold to also trigger snapshots when a minimum number of keys change; the TypeScript AppHost accepts the interval as milliseconds.

When you want to explicitly provide the port and password used by the Garnet container, you can pass them as parameters:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const password: ParameterResource
password
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
("password", {
secret?: boolean | undefined
secret
: true });
const
const cache: GarnetResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addGarnet(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): GarnetResource (+1 overload)

Adds a Garnet container resource to the application model.

addGarnet
("cache", {
port?: number | undefined
port
: 6379,
password?: string | ParameterResource | undefined
password
});
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: GarnetResource
cache
);
// After adding all resources, run the app...

When no password parameter is provided, Aspire generates a strong password automatically using the CreateDefaultPasswordParameter method.

By default, Aspire injects the Garnet connection information using variable names derived from the resource name (for example, CACHE_URI, CACHE_HOST, CACHE_PORT, CACHE_PASSWORD). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
,
type EndpointProperty = "Url" | "Host" | "IPV4Host" | "Port" | "Scheme" | "TargetPort" | "HostAndPort" | "TlsEnabled"
const EndpointProperty: {
readonly Url: "Url";
readonly Host: "Host";
readonly IPV4Host: "IPV4Host";
readonly Port: "Port";
readonly Scheme: "Scheme";
readonly TargetPort: "TargetPort";
readonly HostAndPort: "HostAndPort";
readonly TlsEnabled: "TlsEnabled";
}

Enum Aspire.Hosting.ApplicationModel.EndpointProperty

EndpointProperty
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const cache: GarnetResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addGarnet(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): GarnetResource (+1 overload)

Adds a Garnet container resource to the application model.

addGarnet
("cache");
const
const cacheEndpoint: EndpointReference
cacheEndpoint
= await
const cache: GarnetResource
cache
.
ContainerResource.getEndpoint(name: string): EndpointReference

Gets an endpoint reference

getEndpoint
("tcp");
const
const cacheHost: EndpointReferenceExpression
cacheHost
= await
const cacheEndpoint: EndpointReference
cacheEndpoint
.
EndpointReference.property(property: EndpointProperty): EndpointReferenceExpression

Gets the specified property expression of the endpoint.

property
(
const EndpointProperty: {
readonly Url: "Url";
readonly Host: "Host";
readonly IPV4Host: "IPV4Host";
readonly Port: "Port";
readonly Scheme: "Scheme";
readonly TargetPort: "TargetPort";
readonly HostAndPort: "HostAndPort";
readonly TlsEnabled: "TlsEnabled";
}

Enum Aspire.Hosting.ApplicationModel.EndpointProperty

EndpointProperty
.
type Host: "Host"
Host
);
const
const cachePort: EndpointReferenceExpression
cachePort
= await
const cacheEndpoint: EndpointReference
cacheEndpoint
.
EndpointReference.property(property: EndpointProperty): EndpointReferenceExpression

Gets the specified property expression of the endpoint.

property
(
const EndpointProperty: {
readonly Url: "Url";
readonly Host: "Host";
readonly IPV4Host: "IPV4Host";
readonly Port: "Port";
readonly Scheme: "Scheme";
readonly TargetPort: "TargetPort";
readonly HostAndPort: "HostAndPort";
readonly TlsEnabled: "TlsEnabled";
}

Enum Aspire.Hosting.ApplicationModel.EndpointProperty

EndpointProperty
.
type Port: "Port"
Port
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("my-app", "./app", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: GarnetResource
cache
)
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
("GARNET_HOST",
const cacheHost: EndpointReferenceExpression
cacheHost
)
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
("GARNET_PORT",
const cachePort: EndpointReferenceExpression
cachePort
)
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
("GARNET_PASSWORD", await
const cache: GarnetResource
cache
.
GarnetResource.passwordParameter: () => Promise<ParameterResource>

Gets the parameter that contains the Garnet server password.

passwordParameter
());
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

To reference an externally managed Garnet instance instead of running one as a container, use AddConnectionString:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const cache = await builder.addConnectionString("cache");
await builder.addNodeApp("my-app", "./app", "index.js")
.withReference(cache);
// After adding all resources, run the app...
await builder.build().run();

With AddConnectionString and addConnectionString, Aspire resolves cache from ConnectionStrings:cache (or environment variable ConnectionStrings__cache) in the AppHost configuration. Consuming apps receive that value as a single connection string, not deconstructed Garnet resource-specific connection-property variables such as CACHE_HOST, CACHE_PORT, or CACHE_URI.

For the full reference of Garnet resource connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Garnet.

The Garnet hosting integration automatically adds a health check for the Garnet resource. The health check verifies that the Garnet instance is running and that a connection can be established to it.

The hosting integration relies on the 📦 AspNetCore.HealthChecks.Redis NuGet package, which works for Garnet because it speaks the Redis serialization protocol.