# Send Your First Message (/sdks/csharp/tutorials/first-message)



This is your first hands-on lesson with the C# SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the [C# SDK overview](/sdks/csharp)).

## Create a Client [#create-a-client]

```csharp title="Connect.cs"
using KubeMQ.Sdk.Client;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

Console.WriteLine("Connected to KubeMQ");
```

## Send Your First Event [#send-your-first-event]

```csharp title="SendEvent.cs"
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Events;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

await client.SendEventAsync(new EventMessage
{
    Channel = "notifications",
    Body = Encoding.UTF8.GetBytes("hello kubemq")
});
Console.WriteLine("Event sent!");
```

## Receive Events [#receive-events]

```csharp title="ReceiveEvents.cs"
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Events;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

await foreach (var msg in client.SubscribeToEventsAsync(
    new EventsSubscription { Channel = "notifications" }))
{
    Console.WriteLine($"Received: {Encoding.UTF8.GetString(msg.Body.Span)}");
}
```

## Configuration Options [#configuration-options]

```csharp
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Config;

var client = new KubeMQClient(new KubeMQClientOptions
{
    Address = "kubemq-server:50000",
    ClientId = "my-service",
    DefaultTimeout = TimeSpan.FromSeconds(10),
    Tls = new TlsOptions { Enabled = true, CaFile = "/certs/ca.pem" },
    Retry = new RetryPolicy { MaxRetries = 5 },
});
```

| Option              | Type               | Default             | Description                    |
| ------------------- | ------------------ | ------------------- | ------------------------------ |
| `Address`           | `string`           | `"localhost:50000"` | KubeMQ server address          |
| `ClientId`          | `string?`          | Auto-generated      | Unique client identifier       |
| `AuthToken`         | `string?`          | `null`              | JWT authentication token       |
| `DefaultTimeout`    | `TimeSpan`         | `5s`                | Default operation timeout      |
| `ConnectionTimeout` | `TimeSpan`         | `10s`               | Initial connection timeout     |
| `WaitForReady`      | `bool`             | `true`              | Block during reconnection      |
| `Tls`               | `TlsOptions?`      | `null`              | TLS/mTLS configuration         |
| `Retry`             | `RetryPolicy`      | 3 retries           | Retry with exponential backoff |
| `Keepalive`         | `KeepaliveOptions` | 10s ping            | gRPC keepalive                 |
| `Reconnect`         | `ReconnectOptions` | Unlimited           | Auto-reconnection              |
| `LoggerFactory`     | `ILoggerFactory?`  | `null`              | Structured logging             |

### ASP.NET Core / Dependency Injection [#aspnet-core--dependency-injection]

```csharp title="Program.cs"
builder.Services.AddKubeMQ(opts =>
{
    opts.Address = "kubemq-server:50000";
});
```

Or bind from configuration:

```csharp
builder.Services.AddKubeMQ(builder.Configuration);
```

```json title="appsettings.json"
{
  "KubeMQ": {
    "Address": "kubemq-server:50000",
    "DefaultTimeout": "00:00:10"
  }
}
```

## Error Handling [#error-handling]

All SDK methods throw typed exceptions derived from `KubeMQException`:

```csharp title="ErrorHandling.cs"
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Events;
using KubeMQ.Sdk.Exceptions;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions());
await client.ConnectAsync();

try
{
    await client.SendEventAsync(new EventMessage
    {
        Channel = "events",
        Body = Encoding.UTF8.GetBytes("hello")
    });
}
catch (KubeMQTimeoutException ex)
{
    Console.WriteLine($"Timeout: {ex.Message}");
}
catch (KubeMQAuthenticationException ex)
{
    Console.WriteLine($"Auth failed: {ex.Message}");
}
catch (KubeMQConnectionException ex)
{
    Console.WriteLine($"Connection lost: {ex.Message}");
}
catch (KubeMQException ex)
{
    Console.WriteLine($"Error [{ex.ErrorCode}]: {ex.Message}");
}
```

## Next Steps [#next-steps]

* [C# SDK Reference](/sdks/csharp/reference) — full API documentation
* [C# SDK Examples](/sdks/csharp/how-to) — complete examples for all patterns
* [GitHub Repository](https://github.com/kubemq-io/kubemq-CSharp) — source code and issues
