public static object DeserializeToType(this ISerializerFactory serializerFactory, BasicDeliverEventArgs args, Type t) { if (string.IsNullOrEmpty(args.BasicProperties?.ContentType)) { throw new Exception("No content type set on message:" + args); } var contentType = args.BasicProperties.ContentType; var serializer = serializerFactory.For(contentType); return(serializer.DeserializeObject(args.Body, t)); }
public static TResult DeserializeTo <TResult>(this ISerializerFactory serializerFactory, BasicDeliverEventArgs args) where TResult : class { if (string.IsNullOrEmpty(args.BasicProperties?.ContentType)) { throw new Exception("No content type set on message:" + args); } var contentType = args.BasicProperties.ContentType; var serializer = serializerFactory.For(contentType); return(serializer.Deserialize <TResult>(args.Body)); }
public static byte[] Serialize(this ISerializerFactory serializerFactory, object o, string contentType) { var serializer = serializerFactory.For(contentType); return(serializer.Serialize(o)); }
public void Start(ICollection <Type> msgTypes, ICollection <string> contentTypes) { ValidateTypeFactoriesProvided(msgTypes); ValidateSerializersProvided(contentTypes); var msgsToTrack = contentTypes .SelectMany(a => msgTypes.Select(b => new Tuple <string, Type>(a, b))) .ToList(); _isRunning = true; while (_isRunning) { foreach (var t in msgsToTrack) { if (_publisherChannel == null || _publisherChannel.IsClosed) { _publisherChannel = _modelFactory.CreateModel(); } if (_consumerChannel == null || _consumerChannel.IsClosed) { _consumerChannel = _modelFactory.CreateModel(); } var serializer = _serializerFactory.For(t.Item1); var msg = _typeFactory[t.Item2](); var resetEvent = new ManualResetEventSlim(); var wasReceived = false; var correlationId = Guid.NewGuid(); var routingKey = Guid.NewGuid().ToString(); var consumer = new EventingBasicConsumer(_consumerChannel); var roundTripTimer = new Stopwatch(); var deserializationTimer = new Stopwatch(); consumer.Received += (obj, evtArgs) => { if (correlationId == new Guid(evtArgs.BasicProperties.CorrelationId)) { deserializationTimer.Start(); serializer.DeserializeObject(evtArgs.Body, t.Item2); deserializationTimer.Stop(); roundTripTimer.Stop(); wasReceived = true; resetEvent.Set(); } }; var queueConfig = new QueueConfig($"WhiteRabbit.Hopper.{routingKey}", new RoutingConfig($"WhiteRabbit.Hopper.{routingKey}"), _exchange, false, true, true, null); _consumerChannel.DeclareAndBindQueue(queueConfig); _consumerChannel.BasicConsume($"WhiteRabbit.Hopper.{routingKey}", true, consumer); var props = new BasicProperties { ContentType = t.Item1, CorrelationId = correlationId.ToString(), MessageId = Guid.NewGuid().ToString(), Type = t.Item2.AssemblyQualifiedName, Expiration = _messageTimeout.TotalMilliseconds.ToString(CultureInfo.InvariantCulture) }; roundTripTimer.Start(); var serializationTimer = Stopwatch.StartNew(); var payload = serializer.Serialize(msg); serializationTimer.Stop(); _publisherChannel.BasicPublish(_exchange.Name, $"WhiteRabbit.Hopper.{routingKey}", props, payload); resetEvent.Wait(_messageTimeout); if (wasReceived) { _logAction($"[MessageType={t.Item2.Name}] [ContentType={t.Item1}] [MessageText=Serialization completed in {serializationTimer.Elapsed}]"); _logAction($"[MessageType={t.Item2.Name}] [ContentType={t.Item1}] [MessageText=De-serialization completed in {deserializationTimer.Elapsed}]"); _logAction($"[MessageType={t.Item2.Name}] [ContentType={t.Item1}] [MessageText=Round trip completed in {roundTripTimer.Elapsed}]"); } else { _logAction($"{t.Item2.Name} was not received within the timeout {_messageTimeout}"); } } Task.Delay(_delayBetweenRuns).Wait(); } }