# Connecting to EventStoreDB

Describe connecting to the single node and to the cluster.

Gossip seeds, using ip addresses and DNS.

The .NET Client API communicates with EventStoreDB over TCP, using length-prefixed serialised protocol buffers. The API allows for reading and writing operations, as well as for subscriptions to individual event streams or all events written.

# EventStoreConnection

The EventStoreConnection class maintains a full-duplex connection between the client and the EventStoreDB server. EventStoreConnection is thread-safe, and we recommend that you create one node per application.

EventStoreDB handles all connections asynchronously, returning either a Task or a Task<T>.

TIP

To get maximum performance from a non-blocking connection, we recommend you use it asynchronously.

# Quick start

The code below shows how to connect to an EventStoreDB server, write to a stream, and read back the events. For more detailed information, read the full pages for connecting to a server, reading events and writing to a stream

    using System;
    using System.Text;
    using System.Threading.Tasks;
    using EventStore.ClientAPI;
    
    namespace DocsExample.DotNetClient
    {
        public class QuickStartJsonFormat
        {
            public static async Task JsonFormat()
            {
                var conn = EventStoreConnection.Create(new Uri("tcp://admin:changeit@localhost:1113"));
                await conn.ConnectAsync();
    
                var data     = Encoding.UTF8.GetBytes("{\"a\":\"2\"}");
                var metadata = Encoding.UTF8.GetBytes("{}");
                var evt      = new EventData(Guid.NewGuid(), "testEvent", true, data, metadata);
    
                await conn.AppendToStreamAsync("test-stream", ExpectedVersion.Any, evt);
    
                var streamEvents  = await conn.ReadStreamEventsForwardAsync("test-stream", 0, 1, false);
                var returnedEvent = streamEvents.Events[0].Event;
    
                Console.WriteLine(
                    "Read event with data: {0}, metadata: {1}",
                    Encoding.UTF8.GetString(returnedEvent.Data),
                    Encoding.UTF8.GetString(returnedEvent.Metadata)
                );
            }
        }
    }
    
    using EventStore.ClientAPI;
    using System;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DocsExample
    {
        public class QuickStartPlainFormat
        {
            public static async Task PlainFormat()
            {
                var conn = EventStoreConnection.Create(new Uri("tcp://admin:changeit@localhost:1113"));
                await conn.ConnectAsync();
    
                var data     = Encoding.UTF8.GetBytes("some data");
                var metadata = Encoding.UTF8.GetBytes("some metadata");
                var evt      = new EventData(Guid.NewGuid(), "testEvent", false, data, metadata);
    
                await conn.AppendToStreamAsync("test-stream", ExpectedVersion.Any, evt);
    
                var streamEvents  = await conn.ReadStreamEventsForwardAsync("test-stream", 0, 1, false);
                var returnedEvent = streamEvents.Events[0].Event;
    
                Console.WriteLine(
                    "Read event with data: {0}, metadata: {1}",
                    Encoding.UTF8.GetString(returnedEvent.Data),
                    Encoding.UTF8.GetString(returnedEvent.Metadata)
                );
            }
        }
    }
    
    // Make sure to add code blocks to your code group

    TIP

    We recommended to use the JSON format for data and metadata.

    Last Updated: 10/28/2020, 5:09:53 PM