Exemplo n.º 1
0
 public JRpcIntercepter(IJRpcClient client, string taskName, JsonSerializerSettings jsonSerializerSettings, IAbstractCredentials credentials)
 {
     _client   = client;
     _taskName = taskName;
     _jsonSerializerSettings = jsonSerializerSettings;
     _credentials            = credentials;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Аснихронное выполнение jrpc запроса
        /// </summary>
        /// <param name="method"></param>
        /// <param name="contentType"></param>
        /// <param name="url"></param>
        /// <param name="jRpcRequest"></param>
        /// <param name="timeout"></param>
        /// <param name="credentials"></param>
        /// <param name="proxyType"></param>
        /// <param name="clientServiceName"></param>
        /// <param name="clientServiceProxyName"></param>
        /// <returns></returns>
        private async Task <JRpcResponse <T> > HttpAsyncRequest <T>(string method, string contentType, string url,
                                                                    JRpcRequest jRpcRequest, TimeSpan timeout, IAbstractCredentials credentials, Type proxyType,
                                                                    string clientServiceName, string clientServiceProxyName)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);

            if (request.ServicePoint.ConnectionLimit < 100)
            {
                request.ServicePoint.ConnectionLimit = 100;
            }

            request.Method                       = method;
            request.ContentType                  = contentType;
            request.KeepAlive                    = true;
            request.Timeout                      = (int)timeout.TotalMilliseconds;
            request.ReadWriteTimeout             = request.Timeout;
            request.AllowAutoRedirect            = true;
            request.MaximumAutomaticRedirections = 5;

            if (credentials != null)
            {
                request.Headers.Add(HttpRequestHeader.Authorization, credentials.GetHeaderValue());
            }

            request.Headers.Add(JRpcHeaders.CLIENT_IP_HEADER_NAME, CurrentIp);
            request.Headers.Add(JRpcHeaders.CLIENT_PROCESS_NAME_HEADER_NAME, ProcessName);
            request.Headers.Add(JRpcHeaders.CLIENT_PROXY_INTERFACE_NAME, proxyType.FullName);
            if (!string.IsNullOrEmpty(clientServiceName))
            {
                request.Headers.Add(JRpcHeaders.CLIENT_SERVICE_NAME, clientServiceName);
            }

            if (!string.IsNullOrEmpty(clientServiceProxyName))
            {
                request.Headers.Add(JRpcHeaders.CLIENT_SERVICE_PROXY_NAME, clientServiceProxyName);
            }



            var serializer = JsonSerializer.Create(_jsonSerializerSettings);

            if (jRpcRequest != null)
            {
                using (var streamWriter =
                           new JsonTextWriter(new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))) {
                    serializer.Serialize(streamWriter, jRpcRequest);
                    streamWriter.Flush();
                }
            }

            HttpWebResponse response;

            try {
                response = (HttpWebResponse)await request.GetResponseAsync()
                           .WithTimeout(request.Timeout)
                           .ConfigureAwait(false);
            }
            catch (WebException ex) {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    response = (HttpWebResponse)ex.Response;
                }
                else
                {
                    response = null;
                }
            }
            catch (TimeoutException) {
                _logger.Error("Timeout occurred during service invocation.");
                response = null;
            }

            var stream = response?.GetResponseStream();

            if (stream == null)
            {
                throw new Exception($"Response from {url} is empty.");
            }

            using (var sr = new StreamReader(stream)) {
                using (var jsonTextReader = new JsonTextReader(sr)) {
                    return(serializer.Deserialize <JRpcResponse <T> >(jsonTextReader));
                }
            }
        }
Exemplo n.º 3
0
        public T GetProxy <T>(string taskName, IAbstractCredentials credentials) where T : class
        {
            var cacheKey = GetEndPoint(taskName);

            return(JRpcStaticClientFactory.Get <T>(this, taskName, cacheKey, _jsonSerializerSettings, credentials));
        }
Exemplo n.º 4
0
 public static T Get <T>(IJRpcClient client, string taskName, string cacheKey, JsonSerializerSettings jsonSerializerSettings, IAbstractCredentials credentials) where T : class
 {
     return((T)_proxiesCache.GetOrAdd(Tuple.Create(cacheKey, taskName, typeof(T)),
                                      k => ServiceFactory.CreateWithInterceptor <T>(new JRpcIntercepter(client, k.Item2, jsonSerializerSettings, credentials))));
 }