Skip to content

Cratis Chronicle connection strings in .NET

When constructing a Cratis Chronicle .NET client yourself, pass the connection string to ChronicleOptions or directly to ChronicleClient. If your application uses a .NET host, use connection string configuration to bind settings through AddCratisChronicle instead.

ChronicleConnectionString.Development points to localhost:35000 with the built-in development credentials. Both the default ChronicleOptions constructor and the parameterless ChronicleClient constructor use this value:

public static class ConnectionStringsDevelopmentDefaults
{
public static ChronicleClient Create()
{
var options = new ChronicleOptions();
return new ChronicleClient(options);
}
}

View C# snippet source on GitHub

You can select the same connection explicitly through either options or the client constructor:

using Cratis.Chronicle.Connections;
public static class ConnectionStringsDevelopmentDefaultsEquivalent
{
public static ChronicleClient CreateFromOptions()
{
var options = ChronicleOptions.FromDevelopmentConnectionString();
return new ChronicleClient(options);
}
public static ChronicleClient CreateFromConnectionString() => new(ChronicleConnectionString.Development);
}

View C# snippet source on GitHub

Use explicit connection settings and credentials for production. The .NET client skips server certificate validation by default; follow TLS configuration to enable validation before connecting to a remote server.

Use ChronicleOptions.FromConnectionString when you need to set other client options alongside the endpoint:

public static class ConnectionStringsFromConnectionString
{
public static ChronicleClient Create()
{
var options = ChronicleOptions.FromConnectionString("chronicle://localhost:35000");
return new ChronicleClient(options);
}
}

View C# snippet source on GitHub

The resulting options belong to this client instance. Constructing a client this way does not bind the host’s Cratis:Chronicle configuration section. You can also pass a string or a ChronicleConnectionString directly to the ChronicleClient constructor.

Use ChronicleConnectionStringBuilder when your application supplies the host, port, and credentials as separate values:

using Cratis.Chronicle.Connections;
public static class ConnectionStringsFluentBuilder
{
public static ChronicleClient Create()
{
var connectionString = new ChronicleConnectionStringBuilder()
.WithHost("server.example.com")
.WithPort(35000)
.WithCredentials("clientId", "clientSecret")
.Build();
var options = ChronicleOptions.FromConnectionString(connectionString);
return new ChronicleClient(options);
}
}

View C# snippet source on GitHub

Build() returns a string that you can pass to ChronicleOptions.FromConnectionString or the client constructor. For the available URI options, see connection string format and server support.

A connection string can contain a client secret, an API key, or a certificate password, so never write it to a log or an error message as it stands. Use ChronicleConnectionString.Redacted instead: it replaces the user name and every credential value with REDACTED while preserving the scheme, host, port, and non-sensitive options you need to diagnose a connection problem:

using Cratis.Chronicle.Connections;
public static class ConnectionStringsRedactingForLogs
{
public static void LogConnectionTarget(ILogger logger)
{
var connectionString = new ChronicleConnectionString("chronicle://clientId:clientSecret@server.example.com:35000");
// Logs: chronicle://REDACTED:REDACTED@server.example.com:35000
logger.LogInformation("Connecting to {RedactedConnectionString}", connectionString.Redacted);
}
}

View C# snippet source on GitHub

ToString() renders the full connection string, including credentials. Use it only when passing the connection value to code that needs it. The client uses Redacted in its own connection log messages; use the same property in your application logs.

Choose either client credentials or an API key for a connection string. Specifying both throws AmbiguousAuthenticationMode. See connection string format and server support for the authentication parameters.