Ejemplo n.º 1
0
        public async Task <Resp> Call <Req, Resp>(string uri, Req request, int timeoutInMs, IServiceSerializer serializer)
        {
            using (var client = new HttpClient())
            {
                logger.Debug($"http request : {JsonConvert.SerializeObject(request, Formatting.None)}");
                var sw = new Stopwatch();
                sw.Start();

                // 设置超时时间
                var timeout = GetTimeout(timeoutInMs);
                logger.Debug($"Http client : {uri}, timeout : {timeout}");
                client.Timeout = new TimeSpan(0, 0, 0, timeout);

                ByteArrayContent content = new ByteArrayContent(serializer.Serialize <Req>(request));
                var response             = await client.PostAsync(uri, content);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new FrameworkException(ErrorCode.InvalidHttpResponse,
                                                 $"Access {uri} failed, responsed code : {response.StatusCode}");
                }

                var buffer = await response.Content.ReadAsByteArrayAsync();

                var resp = serializer.Deserialize <Resp>(buffer);
                logger.Debug($"Total elapsed : {sw.Elapsed.TotalSeconds.ToString("0.000")}s, " +
                             $"response : {Environment.NewLine}{JsonConvert.SerializeObject(resp, Formatting.None)}");

                return(resp);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T ReceiveCargo <T>(string truckId, SerializationOption serializationOption)
        {
            Log.Write("ReceiveCargo - Receiving Cargo of Type {0}", typeof(T).FullName);

            try
            {
                byte[]             byteStream        = File.ReadAllBytes(Path.Combine(FileSystemFactory.FileSystem.Path.GetTempPath(), truckId));
                IServiceSerializer serviceSerializer = CreateDeserializationStrategy(serializationOption);
                return(serviceSerializer != null?serviceSerializer.Deserialize <T>(byteStream) : default(T));
            }
            catch (Exception e)
            {
                Log.Write(e, $"ReceiveCargo - Error while deserializing type {typeof(T).FullName}");
                throw;
            }
        }
Ejemplo n.º 3
0
        public static object ReceiveCargo(string truckId, SerializationOption serializationOption, Type type)
        {
            Log.Write("ReceiveCargo - UnLoading Cargo for Typeless object");
            try
            {
                byte[] byteStream = File.ReadAllBytes(Path.Combine(FileSystemFactory.FileSystem.Path.GetTempPath(), truckId));

                IServiceSerializer serviceSerializer = CreateDeserializationStrategy(serializationOption);

                return(serviceSerializer?.Deserialize(byteStream, type));
            }
            catch (Exception e)
            {
                Log.Write(e, "ReceiveCargo - Error while deserializing");
                throw;
            }
        }
Ejemplo n.º 4
0
        public object Release(byte[] item, Type type)
        {
            IServiceSerializer serviceSerializer = FactorySerializer.CreateServiceSerializer(type);

            return(serviceSerializer.Deserialize(item, type));
        }
Ejemplo n.º 5
0
        public T Release <T>(byte[] item)
        {
            IServiceSerializer serviceSerializer = FactorySerializer.CreateServiceSerializer(typeof(T));

            return(serviceSerializer.Deserialize <T>(item));
        }
Ejemplo n.º 6
0
 public T Release <T>(byte[] item)
 {
     return(_serviceSerializer.Deserialize <T>(item));
 }