Share via


ValkeyBuilderExtensions.AddValkey Method

Definition

Adds a Valkey container to the application model.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ValkeyResource> AddValkey (this Aspire.Hosting.IDistributedApplicationBuilder builder, string name, int? port = default);
static member AddValkey : Aspire.Hosting.IDistributedApplicationBuilder * string * Nullable<int> -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ValkeyResource>
<Extension()>
Public Function AddValkey (builder As IDistributedApplicationBuilder, name As String, Optional port As Nullable(Of Integer) = Nothing) As IResourceBuilder(Of ValkeyResource)

Parameters

name
String

The name of the resource. This name will be used as the connection string name when referenced in a dependency.

port
Nullable<Int32>

The host port to bind the underlying container to.

Returns

A reference to the IResourceBuilder<T>.

Examples

Use in application host

var builder = DistributedApplication.CreateBuilder(args);

var valkey = builder.AddValkey("valkey");
var api = builder.AddProject<Projects.Api>("api)
                 .WithReference(valkey);

builder.Build().Run(); 

Use in service project with Aspire.StackExchange.Redis package.

var builder = WebApplication.CreateBuilder(args);
builder.AddRedisClient("valkey");

var multiplexer = builder.Services.BuildServiceProvider()
                                  .GetRequiredService<IConnectionMultiplexer>();

var db = multiplexer.GetDatabase();
db.HashSet("key", [new HashEntry("hash", "value")]);
var value = db.HashGet("key", "hash");

Applies to