Beispiel #1
0
        private async Task <Metadata> GetResponseHeadersCoreAsync()
        {
            try
            {
                var httpResponse = await HttpResponseTask.ConfigureAwait(false);

                // Check if the headers have a status. If they do then wait for the overall call task
                // to complete before returning headers. This means that if the call failed with a
                // a status then it is possible to await response headers and then call GetStatus().
                var grpcStatus = GrpcProtocolHelpers.GetHeaderValue(httpResponse.Headers, GrpcProtocolConstants.StatusTrailer);
                if (grpcStatus != null)
                {
                    await CallTask.ConfigureAwait(false);
                }

                var metadata = GrpcProtocolHelpers.BuildMetadata(httpResponse.Headers);

                // https://github.com/grpc/proposal/blob/master/A6-client-retries.md#exposed-retry-metadata
                if (_attemptCount > 1)
                {
                    metadata.Add(GrpcProtocolConstants.RetryPreviousAttemptsHeader, (_attemptCount - 1).ToString(CultureInfo.InvariantCulture));
                }

                return(metadata);
            }
            catch (Exception ex) when(ResolveException(ErrorStartingCallMessage, ex, out _, out var resolvedException))
            {
                throw resolvedException;
            }
        }
Beispiel #2
0
        private async Task <Metadata> GetResponseHeadersCoreAsync()
        {
            try
            {
                var httpResponse = await HttpResponseTask.ConfigureAwait(false);

                // Check if the headers have a status. If they do then wait for the overall call task
                // to complete before returning headers. This means that if the call failed with a
                // a status then it is possible to await response headers and then call GetStatus().
                var grpcStatus = GrpcProtocolHelpers.GetHeaderValue(httpResponse.Headers, GrpcProtocolConstants.StatusTrailer);
                if (grpcStatus != null)
                {
                    await CallTask.ConfigureAwait(false);
                }

                var metadata = GrpcProtocolHelpers.BuildMetadata(httpResponse.Headers);

                // https://github.com/grpc/proposal/blob/master/A6-client-retries.md#exposed-retry-metadata
                if (_attemptCount > 1)
                {
                    metadata.Add(GrpcProtocolConstants.RetryPreviousAttemptsHeader, (_attemptCount - 1).ToString(CultureInfo.InvariantCulture));
                }

                return(metadata);
            }
            catch (Exception ex)
            {
                // If there was an error fetching response headers then it's likely the same error is reported
                // by response TCS. The user is unlikely to observe both errors.
                // Observe the task's exception to prevent TaskScheduler.UnobservedTaskException from firing.
                _responseTcs?.Task.ObserveException();

                if (ResolveException(ErrorStartingCallMessage, ex, out _, out var resolvedException))
                {
                    throw resolvedException;
                }
                else
                {
                    throw;
                }
            }
        }