Exemplo n.º 1
0
        private void consumeRealTimeService()
        {
            try
            {
                string         message = string.Format("Consume Real Time Service {0}, version {1} ", realTimeServiceName, version);
                ConsumeService service = new ConsumeService(Uri);
                SetAuthorizationHeader(service.HttpClient.DefaultRequestHeaders);
                var dataFrame = new Dictionary <string, object>
                {
                    { "Kyphosis", new [] { "absent", "absent" } },
                    { "Age", new [] { 71, 55 } },
                    { "Number", new [] { 3, 4 } },
                    { "Start", new [] { 5, 7 } }
                };

                ValidateRealTimeService.Models.InputParameters inputParams = new ValidateRealTimeService.Models.InputParameters()
                {
                    InputData = dataFrame
                };

                Console.WriteLine(message);
                var response = service.KyphosisService(realTimeServiceName, version, inputParams);

                if (response.Success == false)
                {
                    throw new Exception(response.ErrorMessage);
                }

                Console.WriteLine("Consume Real Time Service Result : " + response.OutputParameters.OutputData);
            }
            catch (Exception e)
            {
                string message = string.Format("Consume Real Time Service {0}, version {1} failed ", realTimeServiceName, version);
                throw new Exception(message + e.Message);
            }
        }
Exemplo n.º 2
0
 public static async Task <ValidateRealTimeService.Models.WebServiceResult> KyphosisServiceAsync(this ConsumeService operations, string name, string version, ValidateRealTimeService.Models.InputParameters webServiceParameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.KyphosisServiceWithHttpMessagesAsync(name, version, webServiceParameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Exemplo n.º 3
0
        public async Task <HttpOperationResponse <ValidateRealTimeService.Models.WebServiceResult> > KyphosisServiceWithHttpMessagesAsync(string name, string version, ValidateRealTimeService.Models.InputParameters webServiceParameters, Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (name == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "name");
            }

            if (version == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "version");
            }

            if (webServiceParameters == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "webServiceParameters");
            }
            // Tracing
            bool   _shouldTrace  = ServiceClientTracing.IsEnabled;
            string _invocationId = null;

            if (_shouldTrace)
            {
                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("name", name);
                tracingParameters.Add("version", version);
                tracingParameters.Add("webServiceParameters", webServiceParameters);
                tracingParameters.Add("cancellationToken", cancellationToken);
                ServiceClientTracing.Enter(_invocationId, this, "KyphosisService", tracingParameters);
            }
            // Construct URL
            var _baseUrl = BaseUri.AbsoluteUri;
            var _url     = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "api/{name}/{version}").ToString();

            _url = _url.Replace("{name}", System.Uri.EscapeDataString(name));
            _url = _url.Replace("{version}", System.Uri.EscapeDataString(version));
            // Create HTTP transport objects
            var _httpRequest = new HttpRequestMessage();
            HttpResponseMessage _httpResponse = null;

            _httpRequest.Method     = new HttpMethod("POST");
            _httpRequest.RequestUri = new System.Uri(_url);
            // Set Headers


            if (customHeaders != null)
            {
                foreach (var _header in customHeaders)
                {
                    if (_httpRequest.Headers.Contains(_header.Key))
                    {
                        _httpRequest.Headers.Remove(_header.Key);
                    }
                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
                }
            }

            // Serialize Request
            string _requestContent = null;

            if (webServiceParameters != null)
            {
                _requestContent      = SafeJsonConvert.SerializeObject(webServiceParameters, SerializationSettings);
                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
                _httpRequest.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
            }
            // Send Request
            if (_shouldTrace)
            {
                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

            if (_shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
            }
            HttpStatusCode _statusCode = _httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string _responseContent = null;

            if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 500)
            {
                var ex = new ErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
                try
                {
                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    Error _errorBody = SafeJsonConvert.DeserializeObject <Error>(_responseContent, DeserializationSettings);
                    if (_errorBody != null)
                    {
                        ex.Body = _errorBody;
                    }
                }
                catch (JsonException)
                {
                    // Ignore the exception
                }
                ex.Request  = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
                if (_shouldTrace)
                {
                    ServiceClientTracing.Error(_invocationId, ex);
                }
                _httpRequest.Dispose();
                if (_httpResponse != null)
                {
                    _httpResponse.Dispose();
                }
                throw ex;
            }
            // Create Result
            var _result = new HttpOperationResponse <ValidateRealTimeService.Models.WebServiceResult>();

            _result.Request  = _httpRequest;
            _result.Response = _httpResponse;
            // Deserialize Response
            if ((int)_statusCode == 200)
            {
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                try
                {
                    _result.Body = SafeJsonConvert.DeserializeObject <ValidateRealTimeService.Models.WebServiceResult>(_responseContent, DeserializationSettings);
                }
                catch (JsonException ex)
                {
                    _httpRequest.Dispose();
                    if (_httpResponse != null)
                    {
                        _httpResponse.Dispose();
                    }
                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
                }
            }
            if (_shouldTrace)
            {
                ServiceClientTracing.Exit(_invocationId, _result);
            }
            return(_result);
        }
Exemplo n.º 4
0
 public static ValidateRealTimeService.Models.WebServiceResult KyphosisService(this ConsumeService operations, string name, string version, ValidateRealTimeService.Models.InputParameters webServiceParameters)
 {
     return(operations.KyphosisServiceAsync(name, version, webServiceParameters).GetAwaiter().GetResult());
 }