public void Send(RawBusMessage message, ISerializer serializer, SendParams sendParams) { if (message.Data != null) { Type type = message.Data.GetType(); DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey()); if (message.Name == null) { message.Name = contractKey.Name; } if (message.Namespace == null) { message.Namespace = contractKey.Ns; } } var bytes = message.Data as byte[]; if (bytes == null) { bytes = message.Data != null?serializer.Serialize(message) : new byte[0]; } Send(bytes, serializer.ContentType, message.Name, message.Namespace, message, sendParams); }
private void ModelOnBasicReturn(object sender, BasicReturnEventArgs args) { DataContractKey dataContractKey = args.BasicProperties.GetDataContractKey(); Type dataType = _sendHelper.GetDataType(dataContractKey); if (dataType == null) { dataContractKey = DataContractKey.BinaryBlob; } object data; if (dataContractKey.Equals(DataContractKey.BinaryBlob)) { data = args.Body; } else { data = _configuration.Serializer.Deserialize(dataType, args.Body); } RawBusMessage message = _messageHelper.ConstructMessage(dataContractKey, args.BasicProperties, data); OnMessageReturn(args.ReplyCode, args.ReplyText, message); }
private void SetBusHeaders(Message message, DataContractKey contractKey) { SetBusHeader(message, MessagingConstants.HeaderNames.Name, contractKey.Name); SetBusHeader(message, MessagingConstants.HeaderNames.NameSpace, contractKey.Ns); SetBusHeader(message, MessagingConstants.HeaderNames.BusId, _busId); SetBusHeader(message, MessagingConstants.HeaderNames.SentTime, DateTime.Now); }
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body) { DataContractKey dataContractKey = properties.GetDataContractKey(); SerializedBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, body); try { _monitor(message); } catch (Exception ex) { RawBusMessage raw = new RawBusMessage { Data = message.Data, Namespace = message.Namespace, Name = message.Name, BusId = message.BusId, CorrelationId = message.CorrelationId, Sent = message.Sent }; foreach (var header in message.Headers) { raw.Headers.Add(header); } _errorSubscriber.MessageDispatchException(raw, ex); } }
public MessageSubscribtionInfo(DataContractKey contractKey, ICallHandler handler, XmlObjectSerializer serializer, bool receiveSelfPublish, IEnumerable <BusHeader> filterHeaders) { _handler = handler; _serializer = serializer; _filterInfo = new MessageFilterInfo(contractKey, receiveSelfPublish, filterHeaders); }
public bool Subscribe(Type dataType, ICallHandler handler, IEnumerable <BusHeader> filter) { DataContractKey key = dataType.GetDataContractKey(); MessageFilterInfo filterInfo = new MessageFilterInfo(key, filter ?? Enumerable.Empty <BusHeader>()); return(_registrationAction(dataType, filterInfo, handler)); }
public object Deserialize(DataContractKey dataContractKey, Type dataType, byte[] body) { if (dataContractKey.Equals(DataContractKey.BinaryBlob)) { return(body); } string sBody = _encoding.GetString(body); return(JsonConvert.DeserializeObject(sBody, dataType)); }
public void Send(SendParams sendParams) { sendParams.BusMessage.Sent = DateTime.Now; sendParams.BusMessage.BusId = sendParams.BusId; BasicProperties basicProperties = new BasicProperties { AppId = sendParams.BusMessage.BusId, Timestamp = sendParams.BusMessage.Sent.ToAmqpTimestamp(), ContentType = sendParams.Serializer.ContentType, Headers = new Dictionary <string, object>() }; byte[] bytes; if (sendParams.BusMessage.Data != null) { Type type = sendParams.BusMessage.Data.GetType(); DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey()); basicProperties.Type = contractKey.Name; basicProperties.Headers.Add(MessagingConstants.HeaderNames.Name, contractKey.Name); basicProperties.Headers.Add(MessagingConstants.HeaderNames.NameSpace, contractKey.Ns); bytes = sendParams.Serializer.Serialize(sendParams.BusMessage); } else { bytes = new byte[0]; } foreach (BusHeaderBase header in sendParams.BusMessage.Headers) { basicProperties.Headers.Add(header.Name, header.GetValue()); } if (sendParams.PersistentDelivery) { basicProperties.SetPersistent(true); } if (!string.IsNullOrEmpty(sendParams.ReplyTo)) { basicProperties.ReplyTo = sendParams.ReplyTo; } if (!string.IsNullOrEmpty(sendParams.CorrelationId)) { basicProperties.CorrelationId = sendParams.CorrelationId; } sendParams.Model.BasicPublish(sendParams.Exchange, sendParams.RoutingKey, sendParams.MandatoryDelivery, false, basicProperties, bytes); }
public BusMessage <T> ConstructMessage <T>(DataContractKey dataContractKey, IBasicProperties properties, T data) { BusMessage <T> message = new BusMessage <T> { Data = data, BusId = properties.AppId, Sent = properties.Timestamp.GetDateTime() }; ConstructHeaders(message, properties); return(message); }
public object Deserialize(DataContractKey contractKey, Type dataType, byte[] body) { DataContractSerializer serializer = new DataContractSerializer(dataType); using (MemoryStream memoryStream = new MemoryStream(body)) { using (Message message = _encoderFactory.Encoder.ReadMessage(memoryStream, int.MaxValue)) { using (XmlDictionaryReader bodyContents = message.GetReaderAtBodyContents()) { return(serializer.ReadObject(bodyContents)); } } } }
public RawBusMessage ConstructMessage(DataContractKey dataContractKey, IBasicProperties properties, object data) { RawBusMessage message = new RawBusMessage { Data = data, BusId = properties.AppId, Sent = properties.Timestamp.GetDateTime(), Name = dataContractKey.Name, Namespace = dataContractKey.Ns }; ConstructHeaders(message, properties); return(message); }
private void ModelOnBasicReturn(object sender, BasicReturnEventArgs args) { DataContractKey dataContractKey = args.BasicProperties.GetDataContractKey(); Type dataType = _nameMappings.Where(pair => pair.Value.Equals(dataContractKey)).Select(pair => pair.Key).FirstOrDefault(); if (dataType == null) { dataContractKey = DataContractKey.BinaryBlob; } object data = _configuration.Serializer.Deserialize(dataContractKey, dataType, args.Body); RawBusMessage message = _messageHelper.ConstructMessage(dataContractKey, args.BasicProperties, data); _configuration.ErrorHandler.DeliveryFailed(args.ReplyCode, args.ReplyText, message); }
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body) { DataContractKey dataContractKey = properties.GetDataContractKey(); string sBody = _encoding.GetString(body); RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)sBody); try { _monitor(rawBusMessage); } // ReSharper disable once EmptyGeneralCatchClause catch { } }
public BusMessage <T> ConstructMessage <T>(DataContractKey dataContractKey, IBasicProperties properties, T data) { BusMessage <T> message = new BusMessage <T> { Data = data, BusId = properties.AppId, Sent = properties.Timestamp.GetDateTime() }; if (properties.IsCorrelationIdPresent()) { message.CorrelationId = properties.CorrelationId; } ConstructHeaders(message, properties); return(message); }
public override Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body) { DataContractKey dataContractKey = properties.GetDataContractKey(); SerializedBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, body); try { _monitor(message); Model.BasicAck(deliveryTag, false); } catch (RejectMessageException) { // If reject message exception is thrown -> reject message without requeue it. // Message will be lost or transfered to dead letter exchange by broker Model.BasicNack(deliveryTag, false, false); } catch (Exception ex) { RawBusMessage raw = new RawBusMessage { Data = message.Data, Namespace = message.Namespace, Name = message.Name, BusId = message.BusId, CorrelationId = message.CorrelationId, Sent = message.Sent }; foreach (var header in message.Headers) { raw.Headers.Add(header); } bool requeue = _exceptionFilter.Filter(ex, raw, redelivered, deliveryTag); Model.BasicNack(deliveryTag, false, requeue); } return(Task.FromResult(0)); }
public SerializedBusMessage ConstructMessage(DataContractKey dataContractKey, IBasicProperties properties, byte[] data) { SerializedBusMessage message = new SerializedBusMessage { Data = data, BusId = properties.AppId, Sent = properties.Timestamp.GetDateTime(), Name = dataContractKey.Name, Namespace = dataContractKey.Ns, ContentType = properties.ContentType }; if (properties.IsCorrelationIdPresent()) { message.CorrelationId = properties.CorrelationId; } ConstructHeaders(message, properties); return(message); }
public Type GetDataType(DataContractKey dataContractKey) { return(_nameMappings.Where(pair => pair.Value.Equals(dataContractKey)).Select(pair => pair.Key).FirstOrDefault()); }
public BusMessage <TData> ReceiveBusMessage <TData>() { BasicGetResult result = _model.BasicGet(_queue, true); IBasicProperties basicProperties = result.BasicProperties; DataContractKey dataContractKey = basicProperties.GetDataContractKey(); var subscription = _nameMappings.Where(p => p.Value.ContractKey.Equals(dataContractKey)).Select( pair => new { DataType = pair.Key }).FirstOrDefault(); if (subscription == null) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body); _errorSubscriber.UnregisteredMessageArrived(rawBusMessage); return(null); } object data; if (!_serializers.ContainsKey(basicProperties.ContentType)) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body); _errorSubscriber.MessageDeserializeException(rawBusMessage, new Exception("Unsupported content type")); return(null); } try { data = _serializers[basicProperties.ContentType].Deserialize(dataContractKey, subscription.DataType, result.Body); } catch (Exception ex) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body); _errorSubscriber.MessageDeserializeException(rawBusMessage, ex); return(null); } BusMessage <TData> message = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (TData)data); if (!_receiveSelfPublish && _busId.Equals(message.BusId)) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body); _errorSubscriber.MessageFilteredOut(rawBusMessage); return(null); } return(message); }
private void ConsumeMessage(bool redelivered, ulong deliveryTag, IBasicProperties properties, byte[] body) { DataContractKey dataContractKey = properties.GetDataContractKey(); var subscription = _subscriptions.Where(p => p.Value.FilterInfo.ContractKey.Equals(dataContractKey)).Select( pair => new { DataType = pair.Key, pair.Value.Handler }).FirstOrDefault(); if (subscription == null) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.UnregisteredMessageArrived(rawBusMessage); return; } object data; if (!_serializers.ContainsKey(properties.ContentType)) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.MessageDeserializeException(rawBusMessage, new Exception("Unsupported content type")); return; } try { data = _serializers[properties.ContentType].Deserialize(dataContractKey, subscription.DataType, body); } catch (Exception ex) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.MessageDeserializeException(rawBusMessage, ex); return; } RawBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, data); if (!_receiveSelfPublish && _busId.Equals(message.BusId)) { _errorSubscriber.MessageFilteredOut(message); return; } try { HandleMessage(subscription.Handler, message, redelivered, deliveryTag); } catch (Exception ex) { _errorSubscriber.MessageDispatchException(message, ex); } }
public override void WriteStartElement(string prefix, string localName, string ns) { _key = new DataContractKey(localName, ns); }
public MessageFilterInfo(DataContractKey contractKey, IEnumerable <BusHeader> filterHeaders) { _contractKey = contractKey; _filterHeaders = new List <BusHeader>(filterHeaders); }
protected bool RegisterType(DataContractKey key, MessageSubscribtionInfo messageSubscribtionInfo) { return(_registeredTypes.TryAdd(key, messageSubscribtionInfo)); }
protected virtual async Task <bool> ConsumeMessage(bool redelivered, ulong deliveryTag, IBasicProperties properties, byte[] body) { DataContractKey dataContractKey = properties.GetDataContractKey(); var subscription = _subscriptions.Where(p => p.Value.FilterInfo.ContractKey.Equals(dataContractKey)).Select( pair => new { DataType = pair.Key, pair.Value.Handler }).FirstOrDefault(); if (subscription == null) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.UnregisteredMessageArrived(rawBusMessage); return(false); } object data; if (!_serializers.ContainsKey(properties.ContentType)) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.MessageDeserializeException(rawBusMessage, new Exception("Unsupported content type")); return(false); } ISerializer serializer; try { serializer = _serializers[properties.ContentType]; if (dataContractKey.Equals(DataContractKey.BinaryBlob)) { data = body; } else { data = serializer.Deserialize(subscription.DataType, body); } } catch (Exception ex) { RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body); _errorSubscriber.MessageDeserializeException(rawBusMessage, ex); return(false); } RawBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, data); if (!_receiveSelfPublish && _busId.Equals(message.BusId)) { _errorSubscriber.MessageFilteredOut(message); return(false); } _trace.MessageArrived(_busId, message, ConsumerTag); RawBusMessage reply; try { reply = await HandleMessage(subscription.Handler, message, redelivered, deliveryTag); } catch (RejectMessageException ex) { reply = new RawBusMessage { Data = ex.ReplyData }; reply.Headers.Add(new RejectedHeader()); } catch (Exception ex) { _errorSubscriber.MessageDispatchException(message, ex); reply = new RawBusMessage(); reply.Headers.Add(new ExceptionHeader { Message = ex.Message }); } if (!_neverReply && reply != null && properties.IsReplyToPresent()) { var sendParams = new SendParams { BusId = _busId, Model = Model, CorrelationId = properties.IsCorrelationIdPresent() ? properties.CorrelationId : "", Exchange = _replyExchange, RoutingKey = properties.ReplyTo, MandatoryDelivery = false, PersistentDelivery = false }; _sendHelper.Send(reply, serializer, sendParams); _trace.MessageSent(_busId, reply); } return(true); }