# Connect (/sdks/csharp/tutorials/connect)



## Overview [#overview]

Every KubeMQ application starts the same way: open a connection to the broker and prove it actually works before building anything on top of it. This tutorial is that first lesson — create a client, give it a stable identity, and confirm connectivity with a health check, so the pattern is muscle memory before you move on to real messaging.

`KubeMQClientOptions` is constructed with an `Address` and a `ClientId` — the ID tags this connection in broker logs, subscriptions, and management views, so pick something stable rather than a random string. `new KubeMQClient(options)` only builds the client; `await client.ConnectAsync()` opens the actual gRPC channel, and `await client.PingAsync()` verifies the round trip cheaply, returning live server info instead of just "no exception." The `await using` pattern calls `DisposeAsync()` automatically, releasing the channel even on exception.

**Gotchas:** constructing the client doesn't connect it — call `ConnectAsync()` explicitly before assuming connectivity; reusing the same client ID across running instances causes routing confusion on the broker; and forgetting the `await using` pattern in quick scripts is a common source of leaked channels under load.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* C# SDK installed (`dotnet add package KubeMQ.SDK.CSharp`)

## Code [#code]

```csharp title="Program.cs"
// KubeMQ .NET SDK — Connection: Connect
//
// This example demonstrates connecting to a KubeMQ server with explicit options.
//
// Prerequisites:
//   - KubeMQ server running on localhost:50000
//   - dotnet run

using KubeMQ.Sdk.Client;

var options = new KubeMQClientOptions
{
    Address = "localhost:50000", // TODO: Replace with your KubeMQ server address
    ClientId = "csharp-connection-connect-client",
};

await using var client = new KubeMQClient(options);
await client.ConnectAsync();
var info = await client.PingAsync();
Console.WriteLine($"Connected to {info.Host} v{info.Version}");

// Expected output:
// Connected to localhost v<version>

```

## How It Works [#how-it-works]

* `KubeMQClientOptions` is constructed with an `Address` and a `ClientId` — the client ID uniquely identifies this connection on the broker.
* `new KubeMQClient(options)` creates the client; the actual gRPC channel is opened by `await client.ConnectAsync()`.
* `await client.PingAsync()` sends a lightweight health-check RPC and returns a `ServerInfo` object with the host name and server version.
* The `await using` pattern ensures `DisposeAsync()` is called automatically, closing the gRPC channel cleanly even on exception.

## Related [#related]

* Getting started
* [C# SDK Reference](/sdks/csharp/reference)
* [Close](/sdks/csharp/how-to/connection/close)
* [Ping](/sdks/csharp/how-to/connection/ping)
