Esempio n. 1
0
        public async Task <IHubMessage?> RehydrateAsync(Stream message, Type messageType)
        {
            try
            {
                var request = await _jsonSerializer.DeserializeAsync(message, messageType).ConfigureAwait(false);

                return(request as IHubMessage);
            }
            catch (JsonException e)
            {
                LogException(e);
            }

            return(null);
        }
Esempio n. 2
0
 public async Task <ApplicationConfig> ParseFile(string path, AppInstallConfig appInstallConfig)
 {
     using (StreamReader r = new StreamReader(path))
     {
         return(await Parse(await _jsonSerializer.DeserializeAsync <ApplicationConfigData>(await r.ReadToEndAsync()), appInstallConfig));
     }
 }
 public static T DeserializeSync <T>(this IJsonSerializer serializer, string source)
 {
     using (var reader = new StringReader(source)) {
         return(serializer
                .DeserializeAsync <T>(reader)
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult());
     }
 }
Esempio n. 4
0
        public async System.Threading.Tasks.Task <IEnumerable <ConfigKey> > GetConfigurationsAsync(string path, IDictionary <string, string> properties, System.Threading.CancellationToken cancellationToken = default)
        {
            using var _requst = ApiRequestFactory.Create();

            // Requests and response stream
            using var _stream = await _requst.GetStreamAsync(Settings, path, properties, cancellationToken : cancellationToken);

            // Deserialize stream and returns.
            return(await m_JsonSerializer.DeserializeAsync <IEnumerable <ConfigKey> >(_stream));
        }
Esempio n. 5
0
        public async Task <TResponse> InvokeAsync <TRequest, TResponse>(string functionName, TRequest request)
        {
            var invokeRequest = new InvokeRequest
            {
                FunctionName = functionName
                , Payload    = _jsonSerializer.Serialize(request)
            };

            var lambdaResponse = await InvokeLambda(invokeRequest).ConfigureAwait(false);

            if (string.IsNullOrWhiteSpace(lambdaResponse.FunctionError))
            {
                return(await _jsonSerializer.DeserializeAsync <TResponse>(lambdaResponse.Payload).ConfigureAwait(false));
            }

            var runtimeError = await _jsonSerializer.DeserializeAsync <LambdaRuntimeErrorModel>(lambdaResponse.Payload).ConfigureAwait(false);

            throw new LambdaRuntimeException(runtimeError);
        }
Esempio n. 6
0
        public async Task <HttpOperationResult <TResponse> > GetAsync <TResponse>(string uri, object payload = null, string apiKey = null, Func <string, Task <TResponse> > deserializer = null)
        {
            CheckDisposed();

            var requestUri = await CreateRequestUriAsync(uri, payload);

            var httpClient = GetHttpClient(apiKey);

            try
            {
                using (var message = await httpClient.GetAsync(requestUri))
                {
                    var responseJson = await message.Content.ReadAsStringAsync();

                    try
                    {
                        var response = deserializer == null
                                                        ? await _jsonSerializer.DeserializeAsync <TResponse>(responseJson)
                                                        : await deserializer.Invoke(responseJson);

                        return(new HttpOperationResult <TResponse>(message.StatusCode, response));
                    }
                    catch
                    {
                        return(new HttpOperationResult <TResponse>(HttpStatusCode.NotFound, default));
                    }
                }
            }
            finally
            {
                if (apiKey != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Esempio n. 7
0
 protected async Task <T> GetResponse <T>(HttpResponseMessage response)
 {
     return(await _jsonSerializer.DeserializeAsync <T>(response.Content));
 }
Esempio n. 8
0
        public void Intercept(IInvocation invocation)
        {
            if (_cache == null)
            {
                invocation.Proceed();
                return;
            }
            var cacheAttribute = GetCacheAttribute(invocation);

            if (cacheAttribute == null)
            {
                invocation.Proceed();
                return;
            }
            var delegateType = GetDelegateType(invocation);

            if (cacheAttribute.CacheAction == CacheAction.Remove)
            {
                switch (delegateType)
                {
                case MethodType.AsyncAction:
                    invocation.ReturnValue = ExcuteRemoveAsync(invocation, cacheAttribute);
                    break;

                case MethodType.Synchronous:
                case MethodType.SynchronousVoid:
                    ExcuteRemove(invocation, cacheAttribute);
                    break;

                case MethodType.AsyncFunction:
                    ExecuteHandleAsyncWithResultUsingReflection(invocation, cacheAttribute);
                    break;
                }
            }
            else
            {
                if (delegateType == MethodType.AsyncAction || MethodType.SynchronousVoid == delegateType)
                {
                    invocation.Proceed();
                    return;
                }
                if (_cache.SimpleIsExistCache(cacheAttribute.CacheName))
                {
                    var cacheValue = _cache.SimpleGetCache <string>(cacheAttribute.CacheName);
                    if (delegateType == MethodType.Synchronous)
                    {
                        invocation.ReturnValue = _jsonSerializer.Deserialize(cacheValue, invocation.Method.ReturnType);
                    }
                    else
                    {
                        invocation.ReturnValue = _jsonSerializer.DeserializeAsync(cacheValue, invocation.Method.ReturnType.GetGenericArguments()[0]);
                    }
                }
                else
                {
                    if (delegateType == MethodType.Synchronous)
                    {
                        invocation.Proceed();
                        AddCache(invocation.ReturnValue, cacheAttribute);
                    }
                    else
                    {
                        ExecuteHandleAsyncWithResultUsingReflection(invocation, cacheAttribute);
                    }
                }
            }
        }
        public async Task <IHubMessage?> RehydrateAsync(Stream messageStream, Type messageType)
        {
            var message = await _jsonSerializer.DeserializeAsync(messageStream, messageType).ConfigureAwait(false);

            return(message as IHubMessage);
        }
Esempio n. 10
0
        /// <inheritdoc />
        public async ValueTask <TResponse?> DeserializeAsync <TResponse>(HttpResponseMessage responseMessage)
        {
            var resultStream = await responseMessage.Content.ReadAsStreamAsync();

            return(await _jsonSerializer.DeserializeAsync <TResponse>(resultStream));
        }
Esempio n. 11
0
        /// <summary>
        /// 异步反序列化
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="serializedObject">字节数组</param>
        /// <returns>对象</returns>
        public virtual Task <T> DeserializeAsync <T>(byte[] serializedObject)
        {
            var jsonValue = serializedObject.ToStr();

            return(_jsonSerializer.DeserializeAsync <T>(jsonValue));
        }