Exemplo n.º 1
0
        public async Task <bool> ConnectAsync(TimeSpan timeout)
        {
            if (State == GrpcConnectionState.Ready)
            {
                return(true);
            }
            State = GrpcConnectionState.Connecting;
            try
            {
                var deadLine = DateTime.UtcNow.AddSeconds(timeout.TotalSeconds);
                await GrpcChannel.ConnectAsync(deadLine);

                State = GrpcConnectionState.Ready;
                _logger.Info($"Grpc channel connect success. [Server] = {GrpcChannel.Target}");
            }
            catch (TaskCanceledException ex)
            {
                State = GrpcConnectionState.Failure;
                _logger.Warning($"Grpc channel connect timeout. {ex.Message}");
            }
            catch (Exception ex)
            {
                State = GrpcConnectionState.Failure;
                _logger.Warning($"Grpc channel connect fail. {ex.Message}");
            }

            return(State == GrpcConnectionState.Ready);
        }