public EventStoreClient(EventStoreClientSettings settings = null)
        {
            _settings = settings ?? new EventStoreClientSettings();
            var connectionName = _settings.ConnectionName ?? $"ES-{Guid.NewGuid()}";
            Action <Exception> exceptionNotificationHook = null;
            var httpHandler = _settings.CreateHttpMessageHandler?.Invoke() ?? new HttpClientHandler();

            if (_settings.ConnectivitySettings.GossipSeeds.Length > 0)
            {
                ConfigureClusterAwareHandler();
            }

            _channel = GrpcChannel.ForAddress(_settings.ConnectivitySettings.Address, new GrpcChannelOptions {
                HttpClient = new HttpClient(httpHandler)
                {
                    Timeout = Timeout.InfiniteTimeSpan,
                    DefaultRequestVersion = new Version(2, 0),
                },
                LoggerFactory = _settings.LoggerFactory
            });
            var callInvoker = (_settings.Interceptors ?? Array.Empty <Interceptor>()).Aggregate(
                _channel.CreateCallInvoker()
                .Intercept(new TypedExceptionInterceptor(exceptionNotificationHook))
                .Intercept(new ConnectionNameInterceptor(connectionName)),
                (invoker, interceptor) => invoker.Intercept(interceptor));

            _client = new Streams.Streams.StreamsClient(callInvoker);
            PersistentSubscriptions = new EventStorePersistentSubscriptionsClient(callInvoker, _settings);
            ProjectionsManager      = new EventStoreProjectionManagerClient(callInvoker);
            UsersManager            = new EventStoreUserManagerClient(callInvoker);
            _log = _settings.LoggerFactory?.CreateLogger <EventStoreClient>() ?? new NullLogger <EventStoreClient>();

            void ConfigureClusterAwareHandler()
            {
                var clusterAwareHttpHandler = new ClusterAwareHttpHandler(
                    _settings.ConnectivitySettings.NodePreference == NodePreference.Leader,
                    new ClusterEndpointDiscoverer(
                        _settings.ConnectivitySettings.MaxDiscoverAttempts,
                        _settings.ConnectivitySettings.GossipSeeds,
                        _settings.ConnectivitySettings.GossipTimeout,
                        _settings.ConnectivitySettings.DiscoveryInterval,
                        _settings.ConnectivitySettings.NodePreference,
                        httpHandler))
                {
                    InnerHandler = httpHandler
                };

                exceptionNotificationHook = clusterAwareHttpHandler.ExceptionOccurred;
                httpHandler = clusterAwareHttpHandler;
            }
        }
Exemplo n.º 2
0
        public EventStoreGrpcClient(Uri address, Func <HttpClient> createHttpClient = default)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            _channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions {
                HttpClient = createHttpClient?.Invoke(),
            });
            var callInvoker = _channel.CreateCallInvoker().Intercept(new TypedExceptionInterceptor());

            _client = new Streams.Streams.StreamsClient(callInvoker);
            PersistentSubscriptions = new EventStorePersistentSubscriptionsGrpcClient(callInvoker);
            ProjectionsManager      = new EventStoreProjectionManagerGrpcClient(callInvoker);
            UsersManager            = new EventStoreUserManagerGrpcClient(callInvoker);
        }
Exemplo n.º 3
0
        public EventStoreClient(EventStoreClientSettings settings = null)
        {
            settings ??= new EventStoreClientSettings(new UriBuilder {
                Scheme = Uri.UriSchemeHttps,
                Port   = 2113
            }.Uri);
            _channel = GrpcChannel.ForAddress(settings.Address, new GrpcChannelOptions {
                HttpClient = settings.CreateHttpClient?.Invoke()
            });
            var connectionName = settings.ConnectionName ?? $"ES-{Guid.NewGuid()}";

            var callInvoker = settings.Interceptors.Aggregate(
                _channel.CreateCallInvoker()
                .Intercept(new TypedExceptionInterceptor())
                .Intercept(new ConnectionNameInterceptor(connectionName)),
                (invoker, interceptor) => invoker.Intercept(interceptor));

            _client = new Streams.Streams.StreamsClient(callInvoker);
            PersistentSubscriptions = new EventStorePersistentSubscriptionsClient(callInvoker);
            ProjectionsManager      = new EventStoreProjectionManagerClient(callInvoker);
            UsersManager            = new EventStoreUserManagerClient(callInvoker);
        }
        private async Task <IWriteResult> AppendToStreamInternal(
            AppendReq header,
            IEnumerable <EventData> eventData,
            EventStoreClientOperationOptions operationOptions,
            UserCredentials?userCredentials,
            CancellationToken cancellationToken)
        {
            using var call = new Streams.Streams.StreamsClient(
                      await SelectCallInvoker(cancellationToken).ConfigureAwait(false)).Append(EventStoreCallOptions.Create(
                                                                                                   Settings, operationOptions, userCredentials, cancellationToken));

            IWriteResult writeResult;

            try {
                await call.RequestStream.WriteAsync(header).ConfigureAwait(false);

                foreach (var e in eventData)
                {
                    _log.LogTrace("Appending event to stream - {streamName}@{eventId} {eventType}.",
                                  header.Options.StreamIdentifier, e.EventId, e.Type);
                    await call.RequestStream.WriteAsync(new AppendReq {
                        ProposedMessage = new AppendReq.Types.ProposedMessage {
                            Id             = e.EventId.ToDto(),
                            Data           = ByteString.CopyFrom(e.Data.Span),
                            CustomMetadata = ByteString.CopyFrom(e.Metadata.Span),
                            Metadata       =
                            {
                                { Constants.Metadata.Type,        e.Type        },
                                { Constants.Metadata.ContentType, e.ContentType }
                            }
                        }
                    }).ConfigureAwait(false);
                }
            } finally {
                await call.RequestStream.CompleteAsync().ConfigureAwait(false);

                var response = await call.ResponseAsync.ConfigureAwait(false);

                if (response.Success != null)
                {
                    writeResult = new SuccessResult(response.Success.CurrentRevisionOptionCase ==
                                                    AppendResp.Types.Success.CurrentRevisionOptionOneofCase.NoStream
                                                        ? StreamRevision.None
                                                        : new StreamRevision(response.Success.CurrentRevision),
                                                    response.Success.PositionOptionCase == AppendResp.Types.Success.PositionOptionOneofCase.Position
                                                        ? new Position(response.Success.Position.CommitPosition,
                                                                       response.Success.Position.PreparePosition)
                                                        : default);
                    _log.LogDebug("Append to stream succeeded - {streamName}@{logPosition}/{nextExpectedVersion}.",
                                  header.Options.StreamIdentifier, writeResult.LogPosition, writeResult.NextExpectedStreamRevision);
                }
                else
                {
                    if (response.WrongExpectedVersion != null)
                    {
                        var actualStreamRevision = response.WrongExpectedVersion.CurrentRevisionOptionCase switch {
                            AppendResp.Types.WrongExpectedVersion.CurrentRevisionOptionOneofCase.CurrentNoStream =>
                            StreamRevision.None,
                            _ => new StreamRevision(response.WrongExpectedVersion.CurrentRevision)
                        };

                        _log.LogDebug(
                            "Append to stream failed with Wrong Expected Version - {streamName}/{expectedRevision}/{currentRevision}",
                            header.Options.StreamIdentifier, new StreamRevision(header.Options.Revision),
                            actualStreamRevision);

                        if (operationOptions.ThrowOnAppendFailure)
                        {
                            if (response.WrongExpectedVersion.ExpectedRevisionOptionCase == AppendResp.Types
                                .WrongExpectedVersion.ExpectedRevisionOptionOneofCase.ExpectedRevision)
                            {
                                throw new WrongExpectedVersionException(header.Options.StreamIdentifier,
                                                                        new StreamRevision(response.WrongExpectedVersion.ExpectedRevision),
                                                                        actualStreamRevision);
                            }

                            var expectedStreamState = response.WrongExpectedVersion.ExpectedRevisionOptionCase switch {
                                AppendResp.Types.WrongExpectedVersion.ExpectedRevisionOptionOneofCase.ExpectedAny =>
                                StreamState.Any,
                                AppendResp.Types.WrongExpectedVersion.ExpectedRevisionOptionOneofCase.ExpectedNoStream =>
                                StreamState.NoStream,
                                AppendResp.Types.WrongExpectedVersion.ExpectedRevisionOptionOneofCase.ExpectedStreamExists =>
                                StreamState.StreamExists,
                                _ => throw new InvalidOperationException()
                            };

                            throw new WrongExpectedVersionException(header.Options.StreamIdentifier,
                                                                    expectedStreamState, actualStreamRevision);
                        }

                        if (response.WrongExpectedVersion.ExpectedRevisionOptionCase == AppendResp.Types
                            .WrongExpectedVersion.ExpectedRevisionOptionOneofCase.ExpectedRevision)
                        {
                            writeResult = new WrongExpectedVersionResult(header.Options.StreamIdentifier,
                                                                         new StreamRevision(response.WrongExpectedVersion.ExpectedRevision),
                                                                         actualStreamRevision);
                        }
                        else
                        {
                            writeResult = new WrongExpectedVersionResult(header.Options.StreamIdentifier,
                                                                         StreamRevision.None,
                                                                         actualStreamRevision);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("The operation completed with an unexpected result.");
                    }
                }
            }

            return(writeResult);
        }
    }
}