コード例 #1
0
        public override async Task Send(
            IAsyncStreamReader <Request> requestStream,
            IServerStreamWriter <Response> responseStream,
            ServerCallContext context)
        {
            var clientId = Guid.NewGuid();

            try
            {
                var client = new ServerStreamWriterAdapter(responseStream);
                _clientsRepository.RegisterClient(clientId, client);
                _logger.LogInformation($"Connected {clientId}");

                await foreach (var request in requestStream.ReadAllAsync())
                {
                    await _requestsHandler.HandleAsync(clientId, request, context.CancellationToken);
                }
            }
            catch (IOException) when(context.CancellationToken.IsCancellationRequested)
            {
            }
            finally
            {
                _clientsRepository.UnregisterClient(clientId);
                _logger.LogInformation($"Disconnected {clientId}");
            }
        }