private KeyValuePair <long, long> ReadWriteUsingFormatterSync(int count, ISerializer serializer, IDeserializer deserializer) { Stopwatch sw = new Stopwatch(); Stopwatch streamSw = new Stopwatch(); sw.Start(); for (int i = 0; i < count; i++) { UserModel model = new UserModel(1, "UserName", "Password"); using (MemoryStream stream = new MemoryStream()) { bool isSerializedTask = serializer.TrySerialize(model, new DefaultSerializerContext(typeof(UserModel), stream)); streamSw.Start(); stream.Seek(0, SeekOrigin.Begin); string json = null; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) json = reader.ReadToEnd(); stream.Seek(0, SeekOrigin.Begin); streamSw.Stop(); IDeserializerContext context = new DefaultDeserializerContext(typeof(UserModel)); bool isDeserializedTask = deserializer.TryDeserialize(stream, context); model = (UserModel)context.Output; } } sw.Stop(); return(new KeyValuePair <long, long>(sw.ElapsedMilliseconds, streamSw.ElapsedMilliseconds)); }
public async Task <object> SendAsync(object message, CancellationToken cancellationToken) { Ensure.NotNull(message, "message"); Type messageType = message.GetType(); if (routes.TryGet(messageType, out RouteDefinition route)) { HttpRequestMessage httpRequest = await CreateRequestAsync(message, route); HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest, cancellationToken); httpResponse.EnsureSuccessStatusCode(); if (route.ResponseDeserializer != null) { using (Stream httpResponseContent = await httpResponse.Content.ReadAsStreamAsync()) { var responseDeserializerContext = new DefaultDeserializerContext(); if (await route.ResponseDeserializer.TryDeserializeAsync(httpResponseContent, responseDeserializerContext)) { return(responseDeserializerContext.Output); } else { // TODO: Throw. } } } } // Throw. throw Ensure.Exception.NotImplemented(); }
public static Envelope<ICommand> DeserializeCommand(this IDeserializer formatter, Type commandType, string payload) { using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload))) { DefaultDeserializerContext context = new DefaultDeserializerContext(typeof(Envelope<>).MakeGenericType(commandType)); Task<bool> result = formatter.TryDeserializeAsync(stream, context); if (!result.IsCompleted) result.Wait(); if (result.Result) return (Envelope<ICommand>)context.Output; } throw Ensure.Exception.NotImplemented(); }
public static IEvent DeserializeEvent(this IDeserializer formatter, Type eventType, string payload) { using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload))) { DefaultDeserializerContext context = new DefaultDeserializerContext(eventType); Task<bool> result = formatter.TryDeserializeAsync(stream, context); if (!result.IsCompleted) result.Wait(); if (result.Result) return (IEvent)context.Output; } throw Ensure.Exception.NotImplemented(); }
public bool TryLoad <T>(string containerPath, out T model) where T : class { if (TryGetContainer(containerPath, false, out ApplicationDataContainer container)) { IFormatter formatter = formatterFactory.Create(new ApplicationDataCompositeStorage(container)); IDeserializerContext context = new DefaultDeserializerContext(typeof(T)); if (formatter.TryDeserialize(new MemoryStream(), context)) { model = (T)context.Output; return(true); } } model = null; return(false); }
private KeyValuePair <long, long> CompositeTypeFormatter(int count) { Converts.Repository .AddJsonEnumSearchHandler() .AddJsonPrimitivesSearchHandler() .AddJsonObjectSearchHandler(); ICompositeTypeProvider provider = new ReflectionCompositeTypeProvider(new ReflectionCompositeDelegateFactory()); CompositeType compositeType; provider.TryGet(typeof(UserModel), out compositeType); CompositeTypeFormatter formatter = new CompositeTypeFormatter(provider, new DefaultFactory <JsonCompositeStorage>()); Stopwatch sw = new Stopwatch(); Stopwatch streamSw = new Stopwatch(); sw.Start(); for (int i = 0; i < count; i++) { UserModel model = new UserModel(1, "UserName", "Password"); using (MemoryStream stream = new MemoryStream()) { Task <bool> isSerializedTask = formatter.TrySerializeAsync(model, new DefaultSerializerContext(typeof(UserModel), stream)); isSerializedTask.Wait(); streamSw.Start(); stream.Seek(0, SeekOrigin.Begin); string json = null; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) json = reader.ReadToEnd(); stream.Seek(0, SeekOrigin.Begin); streamSw.Stop(); IDeserializerContext context = new DefaultDeserializerContext(typeof(UserModel)); Task <bool> isDeserializedTask = formatter.TryDeserializeAsync(stream, context); isDeserializedTask.Wait(); model = (UserModel)context.Output; } } sw.Stop(); return(new KeyValuePair <long, long>(sw.ElapsedMilliseconds, streamSw.ElapsedMilliseconds)); }