Esempio n. 1
0
        public async Task <ulong> TerminateAsync(Guid eventID, string eventName, string streamName, ulong?expectedEventNumber, EventStoreState expectedState)
        {
            var eventData = new EventData(Uuid.FromGuid(eventID), "Delete", null, null);

            if (expectedEventNumber.HasValue)
            {
                var revision = new StreamRevision(expectedEventNumber.Value);

                var writeResult = await client.AppendToStreamAsync(streamName, revision, new EventData[] { eventData });

                await client.TombstoneAsync(streamName, revision);

                return(writeResult.NextExpectedStreamRevision);
            }
            else
            {
                StreamState state;
                switch (expectedState)
                {
                case EventStoreState.Any: state = StreamState.Any; break;

                case EventStoreState.NotExisting: state = StreamState.NoStream; break;

                case EventStoreState.Existing: state = StreamState.StreamExists; break;

                default: throw new NotImplementedException();
                }

                var writeResult = await client.AppendToStreamAsync(streamName, state, new EventData[] { eventData });

                await client.TombstoneAsync(streamName, state);

                return(writeResult.NextExpectedStreamRevision);
            }
        }
        private static async Task TestReadFromtombstoned(EventStoreClient client)
        {
            var idForTombstoneTest = Guid.NewGuid().ToString();
            await client.AppendToStreamAsync(idForTombstoneTest, StreamRevision.FromInt64(-1),
                                             new[] { new EventData(Uuid.NewUuid(), "TestType", new byte[] { 0x00 }) });

            await client.TombstoneAsync(idForTombstoneTest, StreamState.Any);

            var testResultNonExistent =
                client.ReadStreamAsync(Direction.Backwards, Guid.NewGuid().ToString(), StreamPosition.End);
            var testResult = client.ReadStreamAsync(Direction.Backwards, idForTombstoneTest, StreamPosition.End,
                                                    configureOperationOptions: o => { o.ThrowOnAppendFailure = false; });
            var testState = await testResultNonExistent.ReadState;

            testState = await testResult.ReadState;
        }
Esempio n. 3
0
        public static async Task CleanUp(String prefix, EventStoreClientSettings settings)
        {
            if (settings == null)
            {
                settings = EventStoreClientSettings.Create("esdb://127.0.0.1:2113?tls=false");
            }

            var client = new EventStoreClient(settings);

            var result = client.ReadAllAsync(Direction.Backwards, Position.End);
            HashSet <String> streamsToDelete = new HashSet <string>(await result.Where(x => x.OriginalStreamId.StartsWith($"{prefix}-")).Select(x => x.OriginalStreamId).ToListAsync());

            foreach (var item in streamsToDelete)
            {
                await client.TombstoneAsync(item, StreamState.Any);
            }
        }
 public Task <DeleteResult> TombstoneAsync(string streamName, StreamRevision expectedRevision, Action <EventStoreClientOperationOptions> configureOperationOptions = null,
                                           UserCredentials userCredentials = null, CancellationToken cancellationToken = new CancellationToken())
 {
     return(Client.TombstoneAsync(streamName, expectedRevision, configureOperationOptions, userCredentials, cancellationToken));
 }
Esempio n. 5
0
        public async Task DeleteAsync <TAggregate>(TAggregate aggregate) where TAggregate : class, IAggregateRoot
        {
            string streamName = $"{aggregate.GetType().Name}:{aggregate.Id}";
            await Policies.RedisFallbackPolicy.ExecuteAsync(() => _cache.KeyDeleteAsync(streamName));

            var service = _context.GetService <IAggregateMap <TAggregate> >();

            if (service != null)
            {
                await Policies.SqRetryPolicy.ExecuteAsync(() => service.DeleteAsync(aggregate));
            }

            var result = await Policies.EventStoreRetryPolicy.ExecuteAndCaptureAsync(() => _eventStoreClient.TombstoneAsync(streamName, StreamRevision.FromInt64(aggregate.Version)));

            if (result.Outcome == OutcomeType.Successful)
            {
                await Policies.RedisFallbackPolicy.ExecuteAsync(() => _cache.KeyDeleteAsync(streamName));

                return;
            }

            if (service != null)
            {
                var publishResult = await Policies.PublishRetryPolicy.ExecuteAndCaptureAsync(() => _publishEndpoint.Publish(new AggregateInOutOfSyncState()
                {
                    AggregateId = aggregate.Id,
                    Type        = aggregate.GetType().FullName,
                    Version     = aggregate.Version
                }));

                if (publishResult.Outcome == OutcomeType.Failure)
                {
                    throw new AggregateInOutOfSyncException(aggregate, result.FinalException);
                }

                throw result.FinalException;
            }
        }