Beispiel #1
0
        //http://stackoverflow.com/questions/12739114/asp-net-mvc-4-async-child-action
        private TResponse Send <TResponse>(object request, string operationType)
        {
            HttpResponseMessage response = SendOneWay(request, operationType);

            using (Stream stream = response.Content.ReadAsStreamAsync().Result)
            {
                return(JsonDataSerializer.ToValue <TResponse>(stream));
            }
        }
Beispiel #2
0
        private async Task <TResponse> ProcessWithResponseAsync <TResponse>(
            object request, string operationType)
        {
            string urlRequest = request.ToUrl(_serviceAddress, operationType);

            using (HttpClient client = CreateHttpClient())
            {
                HttpResponseMessage response;

                switch (operationType)
                {
                case OperationType.Get:
                    response = await client.GetAsync(urlRequest);

                    break;

                case OperationType.Post:
                    response = await client.PostAsync(urlRequest, CreateContent(request));

                    break;

                case OperationType.Put:
                    response = await client.PutAsync(urlRequest, CreateContent(request));

                    break;

                case OperationType.Delete:
                    response = await client.DeleteAsync(urlRequest);

                    break;

                default:
                    string errorMessage = string.Format("OperationType {0} with Response return is absent",
                                                        operationType);
                    throw Error.InvalidOperation(errorMessage);
                }
                if (!response.IsSuccessStatusCode)
                {
                    throw new WebFaultException(response.StatusCode);
                }
                using (Stream stream = await response.Content.ReadAsStreamAsync())
                {
                    return(JsonDataSerializer.ToValue <TResponse>(stream));
                }
            }
        }
Beispiel #3
0
        private Task <TResponse> SendAsync4 <TResponse>(object request, string operationType)
        {
            string urlRequest = request.ToUrl(_serviceAddress, operationType);

            Task <HttpResponseMessage> response;

            switch (operationType)
            {
            case OperationType.Get:
                response = _httpClient.GetAsync(urlRequest);
                break;

            case OperationType.Post:
                response = _httpClient.PostAsync(urlRequest, CreateContent(request));
                break;

            case OperationType.Put:
                response = _httpClient.PutAsync(urlRequest, CreateContent(request));
                break;

            case OperationType.Delete:
                response = _httpClient.DeleteAsync(urlRequest);
                break;

            default:
                string errorMessage = string.Format("OperationType {0} with Response return is absent", operationType);
                throw Error.InvalidOperation(errorMessage);
            }
            return(response.ContinueWith(x =>
            {
                HttpResponseMessage responseMessage = x.Result;
                if (responseMessage.IsSuccessStatusCode == false)
                {
                    throw new WebFaultException(responseMessage.StatusCode);
                }
                using (Stream stream = responseMessage.Content.ReadAsStreamAsync().Result)
                {
                    return JsonDataSerializer.ToValue <TResponse>(stream);
                }
            }));
        }