public static IApplicationBuilder UseStreamMessages(this IApplicationBuilder builder, EventStoreClient eventStore) { return(builder.MapMethods( "/streams/{streamId}/{streamRevision:int}", (HttpMethod.Get, GetStreamMessage))); async ValueTask <Response> GetStreamMessage(HttpContext context) { var streamId = context.Request.GetStreamId(); var streamRevision = context.Request.GetStreamRevision(); try { var @event = await eventStore.ReadStreamAsync(Direction.Forwards, streamId !, StreamPosition.FromStreamRevision(streamRevision !.Value), 1, resolveLinkTos : false, userCredentials : context.GetUserCredentials(), cancellationToken : context.RequestAborted) .Where(e => e.OriginalEvent.EventNumber == streamRevision) .SingleOrDefaultAsync(context.RequestAborted); return(new HALResponse(StreamMessageRepresentation.Instance, new StreamMessageResource( streamId !, streamRevision !.Value, @event)) { StatusCode = @event.OriginalEvent == null ? HttpStatusCode.NotFound : HttpStatusCode.OK }); } catch (StreamNotFoundException) { return(new HALResponse(StreamMessageRepresentation.Instance, new StreamMessageResource( streamId !, streamRevision !.Value)) { StatusCode = HttpStatusCode.NotFound });
public static async Task <T?> AggregateStream <T>( this EventStoreClient eventStore, Guid id, CancellationToken cancellationToken, ulong?fromVersion = null ) where T : class, IProjection { var readResult = eventStore.ReadStreamAsync( Direction.Forwards, StreamNameMapper.ToStreamId <T>(id), fromVersion ?? StreamPosition.Start, cancellationToken: cancellationToken ); // TODO: consider adding extension method for the aggregation and deserialisation var aggregate = (T)Activator.CreateInstance(typeof(T), true) !; await foreach (var @event in readResult) { var eventData = @event.Deserialize(); aggregate.When(eventData !); } return(aggregate); }
/// <summary> /// Runs the health check, returning the status of the component being checked. /// </summary> /// <param name="context">A context object associated with the current execution.</param> /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> that can be used to cancel the health check.</param> /// <returns> /// A <see cref="T:System.Threading.Tasks.Task`1" /> that completes when the health check has finished, yielding the status of the component being checked. /// </returns> /// <exception cref="Exception">$all stream not found</exception> public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { try { EventStoreClient client = new EventStoreClient(this.EventStoreClientSettings); EventStoreClient.ReadStreamResult readResult = client.ReadStreamAsync(Direction.Forwards, "$all", StreamPosition.Start, userCredentials: this.UserCredentials, resolveLinkTos: true, cancellationToken: cancellationToken); ReadState readState = await readResult.ReadState; if (readState == ReadState.StreamNotFound) { throw new Exception("$all stream not found"); } return(HealthCheckResult.Healthy()); } catch (Exception ex) { return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); } }
public async Task <IList <ResolvedEvent> > ConvertAsync(EventStoreStreamsAttribute config, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(config?.ConnectionStringSetting)) { var esException = new EventStoreStreamsBindingException("ConnectionString cant be empty"); _logger.LogError(esException, esException.Message); throw esException; } using (var client = new EventStoreClient(config.ConnectionStringSetting, _logger)) { try { await client.Connect(); IList <ResolvedEvent> result = null; if (config.StreamReadDirection == StreamReadDirection.Forward) { result = await client.ReadFromStreamForward(config.StreamName, config.StreamOffset, config.ReadSize, config.ResolveLinkTos); } else { result = await client.ReadFromStreamBackward(config.StreamName, config.StreamOffset, config.ReadSize, config.ResolveLinkTos); } return(result); } catch (Exception esException) { _logger.LogError(esException, esException.Message); throw; } } }
static Task DefaultEventProcessingFailureHandler( EventStoreClient client, PersistentSubscription subscription, ResolvedEvent resolvedEvent, Exception exception ) => subscription.Nack(PersistentSubscriptionNakEventAction.Retry, exception.Message, resolvedEvent);
public void NotifyOfPackageDelivery(Package package) { const string stream = "Package-delivery-stream"; var settings = EventStoreClientSettings .Create("esdb://127.0.0.1:2113?tls=false"); var packageDeliveryInfo = new PackageDeliveryInfo { Number = package.Number, RecipientName = package.RecipientName, RecipientEmail = package.RecipientEmail, RecipientSurname = package.RecipientSurname, RecipientStreet = package.RecipientStreet, RecipientStreetNumber = package.RecipientStreetNumber, RecipientPostCode = package.RecipientPostCode, RecipientCity = package.RecipientCity, SenderEmail = package.Sender.Email, }; using (var client = new EventStoreClient(settings)) { client.AppendToStreamAsync( stream, StreamState.Any, new[] { GetEventDataFor(packageDeliveryInfo) }).Wait(); } }
/// <summary> /// Creates EventStoreDB catch-up subscription service for a given stream /// </summary> /// <param name="client"></param> /// <param name="checkpointStore">Checkpoint store instance</param> /// <param name="options">Subscription options</param> /// <param name="consumePipe"></param> public StreamSubscription( EventStoreClient client, StreamSubscriptionOptions options, ICheckpointStore checkpointStore, ConsumePipe consumePipe ) : base(client, options, checkpointStore, consumePipe) => Ensure.NotEmptyString(options.StreamName);
private HttpClient GetTestClient(String dbfileName, String eventStorePrefix) { var client = _factory.WithWebHostBuilder(builder => { String currentPath = System.IO.Path.GetFullPath("."); Int32 startIndex = currentPath.IndexOf("Beer.DaAPI.Service.IntegrationTests"); String basePath = currentPath.Substring(0, startIndex) + "Beer.DaAPI.Service.API"; builder.UseStartup <FakeStartup>(); builder.UseContentRoot(basePath); builder.ConfigureTestServices(services => { AddFakeAuthentication(services, "Bearer"); AddDatabase(services, dbfileName); var settings = EventStoreClientSettings.Create("esdb://127.0.0.1:2113?tls=false"); var client = new EventStoreClient(settings); ReplaceService(services, new EventStoreBasedStoreConnenctionOptions(client, eventStorePrefix)); }); }).CreateClient(); return(client); }
public EsCheckpointStore( EventStoreClient client, string subscriptionName) { _client = client; _streamName = CheckpointStreamPrefix + subscriptionName; }
protected override async Task <EventSubscription> Subscribe( Checkpoint checkpoint, CancellationToken cancellationToken) { var filterOptions = new SubscriptionFilterOptions( EventTypeFilter.ExcludeSystemEvents(), 10, (_, p, ct) => StoreCheckpoint( new EventPosition(p.CommitPosition, DateTime.UtcNow), ct)); var(_, position) = checkpoint; var subscribeTask = position != null ? EventStoreClient.SubscribeToAllAsync( new Position( position.Value, position.Value), TransactionalHandler, false, HandleDrop, filterOptions, cancellationToken : cancellationToken) : EventStoreClient.SubscribeToAllAsync( TransactionalHandler, false, HandleDrop, filterOptions, cancellationToken: cancellationToken); var sub = await subscribeTask.NoContext(); return(new EventSubscription(SubscriptionId, new Stoppable(() => sub.Dispose()))); }
public static async Task WithEventStore() { var connectionString = "esdb://localhost:2113?tls=true&tlsVerifyCert=false"; var settings = EventStoreClientSettings.Create(connectionString); settings.DefaultCredentials = new UserCredentials("admin", "changeit"); var esClient = new EventStoreClient(settings); var esPersistenSubscriptionClient = new EventStorePersistentSubscriptionsClient(settings); var projection = await CreateProjection(settings, persistent : true); Console.ReadLine(); repository = new EventStoreRepository <Counter>(esClient); for (int i = 0; i < 10; i++) { var counterId = Guid.NewGuid(); // Guid.Parse("fbb0f16b-646a-45d3-a1ee-596217897b61"); await CreateAndSaveCounter(counterId); await LoadAndUpdateCounter(counterId); } Console.ReadLine(); projection.Unsubscribe(); }
public MainProgram(IStoreEvents master, IStoreEvents mirror) { _master = master; _mirror = mirror; _replicationPipeine = new Mirroror(_master, _mirror); _mirrorClient = new EventStoreClient(_mirror.Advanced, 50); }
public GeneralLedgerModule(EventStoreClient eventStore, IMessageTypeMapper messageTypeMapper, JsonSerializerOptions serializerOptions) { Build <OpenGeneralLedger>() .Log() .UnitOfWork(eventStore, messageTypeMapper, serializerOptions) .Handle((_, ct) => { var(unitOfWork, command) = _; var handlers = new GeneralLedgerHandlers( new GeneralLedgerEventStoreRepository(eventStore, messageTypeMapper, unitOfWork), new GeneralLedgerEntryEventStoreRepository(eventStore, messageTypeMapper, unitOfWork), new ChartOfAccountsEventStoreRepository(eventStore, messageTypeMapper, unitOfWork)); return(handlers.Handle(command, ct)); }); Build <BeginClosingAccountingPeriod>() .Log() .UnitOfWork(eventStore, messageTypeMapper, serializerOptions) .Handle((_, ct) => { var(unitOfWork, command) = _; var handlers = new GeneralLedgerHandlers( new GeneralLedgerEventStoreRepository(eventStore, messageTypeMapper, unitOfWork), new GeneralLedgerEntryEventStoreRepository(eventStore, messageTypeMapper, unitOfWork), new ChartOfAccountsEventStoreRepository(eventStore, messageTypeMapper, unitOfWork)); return(handlers.Handle(command, ct)); }); }
public EventStoreReadOnlyRepository(IHoldAllConfiguration configs, EventStoreClient esClient) { new EventStoreClient(EventStoreClientSettings.Create("esdb://localhost:2113?tls=false")); this.configs = configs ?? throw new ArgumentNullException(nameof(configs)); reader = new EventReader(esClient, configs); }
private static async Task AppendWithNoStream(EventStoreClient client) { #region append-with-no-stream var eventDataOne = new EventData( Uuid.NewUuid(), "some-event", Encoding.UTF8.GetBytes("{\"id\": \"1\" \"value\": \"some value\"}") ); var eventDataTwo = new EventData( Uuid.NewUuid(), "some-event", Encoding.UTF8.GetBytes("{\"id\": \"2\" \"value\": \"some other value\"}") ); await client.AppendToStreamAsync( "no-stream-stream", StreamState.NoStream, new List <EventData> { eventDataOne }); // attempt to append the same event again await client.AppendToStreamAsync( "no-stream-stream", StreamState.NoStream, new List <EventData> { eventDataTwo }); #endregion append-with-no-stream }
/// <summary> /// Creates EventStoreDB catch-up subscription service for a given stream /// </summary> /// <param name="eventStoreClient">EventStoreDB gRPC client instance</param> /// <param name="streamName">Name of the stream to receive events from</param> /// <param name="subscriptionId">Subscription ID</param> /// <param name="checkpointStore">Checkpoint store instance</param> /// <param name="eventSerializer">Event serializer instance</param> /// <param name="eventHandlers">Collection of event handlers</param> /// <param name="loggerFactory">Optional: logger factory</param> /// <param name="measure">Optional: gap measurement for metrics</param> /// <param name="throwOnError"></param> public StreamSubscription( EventStoreClient eventStoreClient, string streamName, string subscriptionId, ICheckpointStore checkpointStore, IEnumerable <IEventHandler> eventHandlers, IEventSerializer?eventSerializer = null, ILoggerFactory?loggerFactory = null, ISubscriptionGapMeasure?measure = null, bool throwOnError = false ) : this( eventStoreClient, new StreamSubscriptionOptions { StreamName = streamName, SubscriptionId = subscriptionId, ThrowOnError = throwOnError }, checkpointStore, eventHandlers, eventSerializer, loggerFactory, measure ) { }
private static async Task AppendWithSameId(EventStoreClient client) { #region append-duplicate-event var eventData = new EventData( Uuid.NewUuid(), "some-event", Encoding.UTF8.GetBytes("{\"id\": \"1\" \"value\": \"some value\"}") ); await client.AppendToStreamAsync( "same-event-stream", StreamState.Any, new List <EventData> { eventData }); // attempt to append the same event again await client.AppendToStreamAsync( "same-event-stream", StreamState.Any, new List <EventData> { eventData }); #endregion append-duplicate-event }
protected async Task ExecuteAsync(CancellationToken cancellationToken) { #region createClient var settings = EventStoreClientSettings .Create("{connectionString}"); var client = new EventStoreClient(settings); #endregion createClient #region createEvent var evt = new TestEvent { EntityId = Guid.NewGuid().ToString("N"), ImportantData = "I wrote my first event!" }; var eventData = new EventData( Uuid.NewUuid(), "TestEvent", JsonSerializer.SerializeToUtf8Bytes(evt) ); #endregion createEvent #region writingEvent await client.AppendToStreamAsync( "testStream", StreamState.Any, new[] { eventData }, cancellationToken : cancellationToken ); #endregion writingEvent }
public static async Task <int> Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Console() .CreateLogger(); var eventStoreClient = new EventStoreClient(new EventStoreClientSettings { LoggerFactory = new SerilogLoggerFactory() }); await Task.WhenAll(Enumerable.Range(0, 100) .Select(i => eventStoreClient.AppendToStreamAsync($"stream-{i}", StreamState.Any, new[] { new EventData(Uuid.NewUuid(), "-", Array.Empty <byte>(), contentType: "application/octet-stream"), }))); await new HostBuilder() .ConfigureHostConfiguration(builder => builder .AddEnvironmentVariables("DOTNET_") .AddCommandLine(args ?? Array.Empty <string>())) .ConfigureAppConfiguration(builder => builder .AddEnvironmentVariables() .AddCommandLine(args ?? Array.Empty <string>())) .ConfigureLogging(logging => logging.AddSerilog()) .ConfigureWebHostDefaults(builder => builder .UseKestrel() .ConfigureServices(services => services.AddCors().AddRouting()) .Configure(app => app .UseEventStoreHALBrowser() .UseEventStoreHAL(eventStoreClient))) .RunConsoleAsync(); return(0); }
public PrimeOnlyGrain(ILoggerFactory factory, EventStoreClient eventStore) { _logger = factory.CreateLogger <PrimeOnlyGrain>(); _client = eventStore; _observer = new RXObserver <int>(factory, (int number) => this.UpdateAsync(number)); }
public AllPersistentSubscription( EventStoreClient eventStoreClient, AllPersistentSubscriptionOptions options, ConsumePipe consumePipe ) : base(eventStoreClient, options, consumePipe) { }
static async Task Main(string[] args) { // run against // .\EventStore-OSS-Windows-2019-v20.6.1\EventStore.ClusterNode.exe --insecure // make sure http://localhost:2113 works var connectionString = "esdb://*****:*****@localhost:2113/?TlsVerifyCert=false&Tls=false"; // run against // .\EventStore-OSS-Windows-2019-v20.6.0\EventStore.ClusterNode.exe --dev // make sure https://localhost:2113 works //var connectionString = "esdb://*****:*****@localhost:2113/?TlsVerifyCert=false"; var settings = EventStoreClientSettings.Create(connectionString); var client = new EventStoreClient(settings); await client.SubscribeToAllAsync(EventAppeared); Console.WriteLine("Subscribed to all events."); var data = Encoding.UTF8.GetBytes("{}"); var eventData = new EventData(Uuid.NewUuid(), "test-event", data); await client.AppendToStreamAsync("test-events", StreamState.Any, new[] { eventData }); Console.WriteLine("Keypress to exit."); Console.ReadKey(); }
public async Task SaveAndGetEventsInChunks() { Random random = new Random(); Guid id = random.NextGuid(); PseudoAggregateRoot aggregateRoot = PseudoAggregateRoot.Create(id, "my name"); Int32 amount = 10_000; for (int i = 0; i < amount; i++) { aggregateRoot.ChangeName($"iteration {i}"); } var settings = EventStoreClientSettings.Create("esdb://127.0.0.1:2113?tls=false"); var client = new EventStoreClient(settings); String prefix = random.GetAlphanumericString(); EventStoreBasedStore store = new(new EventStoreBasedStoreConnenctionOptions(client, prefix)); try { await store.Save(aggregateRoot); var events = await store.GetEvents <PseudoAggregateRoot>(id, 10); Assert.Equal(amount + 1, events.Count()); Assert.Equal($"iteration {amount - 1}", ((PseudoAggregateRootNameChangedEvent)events.Last()).SecondName); } finally { await EventStoreClientDisposer.CleanUp(prefix, settings); } }
public ToDoController( ToDoListContext dbContext, EventStoreClient eventStoreClient) { this.dbContext = dbContext; this.eventStoreClient = eventStoreClient; }
internal Task Should_Throw_On_DeleteSubscription_When_ConsumerGroup_IsNull( EventStoreClient sut, CancellationToken cancellationToken) => FluentActions .Awaiting(() => sut.DeleteSubscriptionAsync(null, cancellationToken)) .Should() .ThrowAsync <ArgumentNullException>();
public ProjectionRunner(EventStoreClient client, ILogger <ProjectionRunner> logger, Projection[] projections) { _client = client; _logger = logger; _projections = projections; }
public StreamPersistentSubscription( EventStoreClient eventStoreClient, StreamPersistentSubscriptionOptions options, IEnumerable <IEventHandler> eventHandlers, IEventSerializer?eventSerializer = null, ILoggerFactory?loggerFactory = null, ISubscriptionGapMeasure?measure = null ) : base( eventStoreClient, options, new NoOpCheckpointStore(), eventHandlers, eventSerializer, loggerFactory, measure ) { Ensure.NotEmptyString(options.Stream, nameof(options.Stream)); var settings = eventStoreClient.GetSettings().Copy(); var opSettings = settings.OperationOptions.Clone(); options.ConfigureOperation?.Invoke(opSettings); settings.OperationOptions = opSettings; _subscriptionClient = new EventStorePersistentSubscriptionsClient(settings); _handleEventProcessingFailure = options.FailureHandler ?? DefaultEventProcessingFailureHandler; _options = options; }
private static EventStoreClient CreateEventStoreClientWithConnection() { /** https://discuss.eventstore.com/t/basic-eventstoredb-v20-example/2553 * read this for settings workaround for certificate issues when trying out the client * I didn't have this problem but if you are running event store in --dev mode this might be an issue' * @see example code in .Writer project (same as in link above) */ var settings = new EventStoreClientSettings { ConnectivitySettings = new EventStoreClientConnectivitySettings { /* * Note: gRPC uses the https and thus needs to use port 2113 (same as admin UI), * instead of 1113 as the tcp client uses. */ Address = new Uri("https://localhost:2113/") } }; var client = new EventStoreClient(settings); return(client); /* * In this example we used the EventStoreConnection.Create() overloaded method but others are available. * https://eventstore.com/docs/dotnet-api/code/EventStore.ClientAPI.EventStoreConnection.html * instead of using tcp for the client connection gRPC is recommended. (but not yet in documentation) */ }
private App() { var settings = new DefaultHandlerSettings( new HandlerModule(), new DefaultRequestTypeResolver("cedar", Enumerable.Empty <Type>())); var commitDispatcherFailed = new TaskCompletionSource <Exception>(); //MidFunc blah = CommandHandlingMiddleware.HandleCommands(settings); //_middleware = CreateGate(commitDispatcherFailed.Task) _middleware = CommandHandlingMiddleware.HandleCommands(settings); _storeEvents = Wireup.Init().UsingInMemoryPersistence().Build(); var eventStoreClient = new EventStoreClient(_storeEvents.Advanced); _durableCommitDispatcher = new DurableCommitDispatcher( eventStoreClient, new InMemoryCheckpointRepository(), new HandlerModule(), TransientExceptionRetryPolicy.Indefinite(TimeSpan.FromMilliseconds(500))); _durableCommitDispatcher.ProjectedCommits.Subscribe( _ => { }, commitDispatcherFailed.SetResult); _durableCommitDispatcher.Start().Wait(); }
public StatsSubscriber(EventStoreClient eventStoreClient, string subscriptionId, ICheckpointStore checkpointStore, IEventSerializer eventSerializer, IEnumerable <IEventHandler> eventHandlers, ILoggerFactory loggerFactory = null, IEventFilter eventFilter = null, SubscriptionGapMeasure measure = null) : base(eventStoreClient, subscriptionId, checkpointStore, eventSerializer, eventHandlers, loggerFactory, eventFilter, measure) { }
public NEventStoreProjectionDispatcher( EventStoreClient eventStoreClient, IProjectionHandlerResolver handlerResolver, ICheckpointRepository checkpointRepository) : base(handlerResolver, checkpointRepository) { _eventStoreClient = eventStoreClient; }
public async Task Blah() { using(var eventStore = Wireup .Init() .UsingInMemoryPersistence() .Build()) { using(var client = new EventStoreClient(eventStore.Advanced)) { var handlerResolver = new ProjectionHandlerResolver(new TestProjectionModule()); var dispatcher = new NEventStoreProjectionDispatcher( client, handlerResolver, new InMemoryCheckpointRepository()); await dispatcher.Start(); } } }
public void SetUp() { _connectionFactory = new EmbeddedEventStoreConnectionFactory(); _eventStoreClient = new EventStoreClient(_connectionFactory, new ConsoleLoggerFactory()); }