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);
                }
        }
Exemple #2
0
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Path != _httpServerTransportOptions.Path)
            {
                await _next(context);
            }
            else
            {
                var rpcRequest   = (RpcRequest)null;
                var rpcError     = (RpcError)null;
                var result       = (object)null;
                var deserializer = _serializationHelper.GetByContentType(context.Request.ContentType);

                if (deserializer == null)
                {
                    rpcError = new RpcError {
                        Code = RpcErrorCode.NotSupportedContentType
                    };
                    _logger.LogError(rpcError.Code.ToString(), context.Request.ContentType);
                }
                else
                {
                    try
                    {
                        rpcRequest = (RpcRequest)await deserializer.DeserializeAsync(context.Request.Body, typeof(RpcRequest));
                    }
                    catch (Exception e)
                    {
                        rpcError = new RpcError
                        {
                            Code      = RpcErrorCode.IncorrectRequestBodyFormat,
                            Exception = e,
                        };

                        _logger.LogError(e, rpcError.Code.ToString());
                    }
                }

                if (rpcRequest != null)
                {
                    try
                    {
                        result = await rpcRequest.Invoke(context.RequestServices);
                    }
                    catch (Exception e)
                    {
                        rpcError = new RpcError
                        {
                            Code      = RpcErrorCode.RemoteMethodInvocation,
                            Exception = e,
                        };

                        _logger.LogError(e, rpcError.Code.ToString(), rpcRequest);
                    }
                }


                var serializer = deserializer ?? _serializationHelper.TryGetByTypeName(null);

                context.Response.ContentType = serializer.ContentType;
                await serializer.SerializeAsync(
                    context.Response.Body,
                    new RpcResponse
                {
                    Result = result,
                    Error  = rpcError
                },
                    typeof(RpcResponse));
            }
        }