# Quick tour
This is a quick tour into the basic operations with EventStoreDb. We will look at creating a connection, writing an event and reading an event.
# Requirements
These examples have the following requirements:
- At least .NET Core SDK 3.1 (opens new window)
- Docker (opens new window)
- A reference to the EventStore.Client.Grpc.Streams (opens new window) NuGet package
# Setup the certificates and running the server
To run the EventStoreDB, create a new file called docker-compose.yml
and copy the following contents into it.
version: '3'
services:
eventstore:
image: docker.pkg.github.com/eventstore/eventstore/eventstore:20.6.1-buster-slim
environment:
- EVENTSTORE_INSECURE=true
- EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true
ports:
- 2113:2113
Then run the command.
docker-compose up
This will launch a new instance of the EventStoreDb server .
# Creating a connection
Create a new console application.
The following example shows the simplest way to create a connection to EventStoreDb on your local machine.
var settings = new EventStoreClientSettings {
ConnectivitySettings = {
Address = new Uri("http://localhost:2113")
}
};
var client = new EventStoreClient(settings);
EventStoreDB runs secure by default. To avoid setting up any certificates we need to override the HttpClientHandler
to tell it to trust all certificates.
TIP
By default, the server listens to port 2113 for requests.
# Writing an event
Writing an event to the database involves two stages.
Firstly you have to construct a new EventData
that contains a unique id, an event type, and a byte array containing the event data. Usually this is represented by json but can take any format.
Secondly you have to append that EventData
to a stream. Making sure to specify the stream name, the expected state of the stream and then the data.
var eventData = new EventData(
Uuid.NewUuid(),
"some-event",
Encoding.UTF8.GetBytes("{\"id\": \"1\" \"value\": \"some value\"}")
);
await client.AppendToStreamAsync(
"some-stream",
StreamState.NoStream,
new List<EventData> {
eventData
});
# Reading an event
You can read events from a stream in both directions. In this case we are reading the some-stream
forwards from the start. We are reading a single event.
This provides an IAsyncEnumerable
, which you can then iterate on.
var events = client.ReadStreamAsync(
Direction.Forwards,
"some-stream",
StreamPosition.Start,
1);
await foreach (var @event in events) {
Console.WriteLine(Encoding.UTF8.GetString(@event.Event.Data.Span));
}