Ejemplo n.º 1
0
        private async Task <T> SendRequest <T>(RpcRequest rpcRequest)
        {
            var httpClient = _httpClientFactory.CreateClient(_clientName);

            using (var streamContent = new SerializbleContent(_serializer, rpcRequest))
                using (var httpResponseMessage = await httpClient.SendAsync(
                           new HttpRequestMessage
                {
                    Content = streamContent,
                    Method = HttpMethod.Post,
                },
                           HttpCompletionOption.ResponseHeadersRead,
                           CancellationToken.None).ConfigureAwait(false)
                       )
                {
                    httpResponseMessage.EnsureSuccessStatusCode();

                    var resultSerializer = serializationHelper.GetByContentType(httpResponseMessage.Content.Headers.ContentType.MediaType);
                    var stream           = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    var result = (RpcResponse)await resultSerializer.DeserializeAsync(stream, typeof(RpcResponse)).ConfigureAwait(false);

                    if (result.Error != null)
                    {
                        throw new RpcException(result.Error);
                    }

                    return((T)result.Result);
                }
        }
Ejemplo n.º 2
0
        private async Task <T> SendRequest <T>(RpcRequest rpcRequest)
        {
            var streamContent = new SerializbleContent(_serializer, rpcRequest);

            using (var httpResponseMessage = await _httpClient.PostAsync(string.Empty, streamContent, CancellationToken.None).ConfigureAwait(false))
            {
                httpResponseMessage.EnsureSuccessStatusCode();

                var resultSerializer = SerializationHelper.GetByContentType(httpResponseMessage.Content.Headers.ContentType.MediaType);
                var stream           = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

                var result = (RpcResponse)resultSerializer.Deserialize(stream, typeof(RpcResponse));

                if (result.Error != null)
                {
                    throw new RpcException(result.Error);
                }

                return((T)result.Result);
            }
        }