private void QueueTask(Job x)
        {
            Interlocked.Increment(ref _processingQueueSize);
            _queueSize.Increment();

            if (_processingQueueSize % 10 == 0 || Logger.IsDebugEnabled)
            {
                var eventType = _mapper.GetMappedTypeFor(x.Event.GetType());
                var msg       = String.Format("Queueing event {0} at position {1}.  Size of queue: {2}/{3}", eventType.FullName, x.Position, _processingQueueSize, _maxQueueSize);
                if (_processingQueueSize % 10 == 0)
                {
                    Logger.Info(msg);
                }
                else
                {
                    Logger.Debug(msg);
                }
            }

            _processor.Queue(async() =>
            {
                await Process(x.Event, x.Descriptor, x.Position);
                _queueSize.Decrement();
                Interlocked.Decrement(ref _processingQueueSize);
            });
        }
Esempio n. 2
0
 public Type GetMappedTypeFor(Type t)
 {
     if (t.Assembly.GetName().Name.StartsWith("ServiceControl.Plugin"))
     {
         return(Type.GetType($"{t.FullName}{assemblyName}"));
     }
     return(messageMapper.GetMappedTypeFor(t));
 }
Esempio n. 3
0
        public void Class_abstract_generic_with_only_properties_generic_should_not_be_mapped()
        {
            var genericClassType = typeof(GenericAbstractCommand <>);

            mapper.Initialize(new[] { genericClassType });
            Assert.Null(mapper.GetMappedTypeFor(genericClassType));
        }
Esempio n. 4
0
        public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
        {
            var mappedType = _messageMapper.GetMappedTypeFor(serializedType);

            assemblyName = mappedType.Assembly.GetName().Name;
            typeName     = mappedType.FullName;
        }
 private IncomingTransportMessage TryDeserializeTransportMessage(ServiceBrokerMessage message)
 {
     using (var stream = new MemoryStream(message.Body))
     {
         try
         {
             var  payload = FastXmlTransportMessageSerializer.Deserialize(stream);
             var  enclosedMessageTypes = payload.Headers[StandardHeaders.EnclosedMessageTypes] as string ?? "";
             Type messageType          = null;
             foreach (var typeName in enclosedMessageTypes.Split(','))
             {
                 messageType = messageMapper.GetMappedTypeFor(typeName);
                 if (messageType != null)
                 {
                     break;
                 }
             }
             if (enclosedMessageTypes == null)
             {
                 var errorMessage = "Cannot decode type: " + enclosedMessageTypes;
                 logger.Error(errorMessage);
                 throw new CannotDeserializeMessageException(errorMessage);
             }
             var decodedMessage = messageEncoder.Decode(Encoding.UTF8.GetBytes(payload.Body), messageType);
             return(new IncomingTransportMessage(message.ConversationHandle.ToString(), payload.Headers, decodedMessage));
         }
         catch (Exception e)
         {
             logger.Error(e, "Cannot deserialize transport message for message {0} from queue {1}.", message.ConversationHandle, ServiceBrokerQueue);
             throw new CannotDeserializeMessageException(e);
         }
     }
 }
Esempio n. 6
0
        public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
        {
            var mappedType = _messageMapper.GetMappedTypeFor(serializedType) ?? serializedType;

            assemblyName = null;
            typeName     = mappedType.AssemblyQualifiedName;
        }
Esempio n. 7
0
        public Type GetMappedTypeFor(Type type)
        {
            while (!Bus.BusOnline)
            {
                Thread.Sleep(100);
            }

            return(_mapper.GetMappedTypeFor(type));
        }
Esempio n. 8
0
        public Task <long> WriteSnapshot(string stream, IFullEvent snapshot,
                                         IDictionary <string, string> commitHeaders)
        {
            Logger.Write(LogLevel.Debug, () => $"Writing snapshot to stream id [{stream}]");

            var settings = new JsonSerializerSettings
            {
                TypeNameHandling    = TypeNameHandling.Auto,
                SerializationBinder = new EventSerializationBinder(_mapper),
                //ContractResolver = new EventContractResolver(_mapper)
                Converters = new[] { new Newtonsoft.Json.Converters.StringEnumConverter() }
            };

            var descriptor = snapshot.Descriptor;

            descriptor.EventId       = snapshot.EventId ?? Guid.NewGuid();
            descriptor.CommitHeaders = commitHeaders ?? new Dictionary <string, string>();

            var mappedType = snapshot.Event.GetType();

            if (!mappedType.IsInterface)
            {
                mappedType = _mapper.GetMappedTypeFor(mappedType) ?? mappedType;
            }

            var @event = snapshot.Event.Serialize(settings).AsByteArray();

            if (_compress.HasFlag(Compression.Snapshots))
            {
                descriptor.Compressed = true;
                @event = @event.Compress();
            }
            var metadata = descriptor.Serialize(settings).AsByteArray();

            var data = new EventData(
                descriptor.EventId,
                mappedType.AssemblyQualifiedName,
                !descriptor.Compressed,
                @event,
                metadata
                );

            return(DoWrite(stream, new[] { data }));
        }
        public object GetConcreteInstance(bool useReasonableDefaults)
        {
            var concreteType     = _messageMapper.GetMappedTypeFor(_messageType);
            var concreteInstance = _messageMapper.CreateInstance(concreteType);

            if (useReasonableDefaults)
            {
                SetReasonableDefaults(concreteInstance, _messageType);
            }
            return(concreteInstance);
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var    mappedType = _messageMapper.GetMappedTypeFor(value.GetType());
            string typeName   = GetTypeName(mappedType);

            var jobj = JObject.FromObject(value);

            jobj.AddFirst(new JProperty("$type", typeName));

            jobj.WriteTo(writer);
        }
Esempio n. 11
0
        private IDictionary <string, Action <IEventSource, object> > GetCached(IEventSource eventsource, Type eventType)
        {
            // Wtf is going on here? Well allow me to explain
            // In our eventsources we have methods that look like:
            // private void Handle(Events.MyEvent e) {}
            // and we may also have
            // private void Conflict(Events.MyEvent e) {}
            // this little cache GetOrAdd is basically searching for those methods and returning an Action the caller
            // can use to execute the method
            var mappedType = _mapper.GetMappedTypeFor(eventType);
            IDictionary <string, Action <IEventSource, object> > handles;

            lock (Lock)
            {
                if (Cache.TryGetValue(eventType, out handles))
                {
                    return(handles);
                }
            }

            var methods = eventsource.GetType()
                          .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
                          .Where(
                m => (m.Name == "Handle" || m.Name == "Conflict") &&
                m.GetParameters().Length == 1 &&
                m.GetParameters().Single().ParameterType == mappedType &&
                m.ReturnParameter.ParameterType == typeof(void));
            //.Select(m => new { Method = m, MessageType = m.GetParameters().Single().ParameterType });

            var methodInfos = methods as MethodInfo[] ?? methods.ToArray();

            if (!methodInfos.Any())
            {
                return(null);
            }

            handles = methodInfos.ToDictionary(x => x.Name, x => (Action <IEventSource, object>)((es, m) =>
            {
                try
                {
                    x.Invoke(es, new[] { m });
                }
                catch (TargetInvocationException e)
                {
                    ExceptionDispatchInfo.Capture(e.InnerException).Throw();
                }
            }));

            lock (Lock)
            {
                return(Cache[eventType] = handles);
            }
        }
 Type GetMappedType(Type messageType)
 {
     if (messageType.IsInterface)
     {
         var mappedTypeFor = messageMapper.GetMappedTypeFor(messageType);
         if (mappedTypeFor != null)
         {
             return(mappedTypeFor);
         }
     }
     return(messageType);
 }
Esempio n. 13
0
        public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
        {
            var mappedType = serializedType;

            if (!serializedType.IsInterface)
            {
                mappedType = _mapper.GetMappedTypeFor(serializedType) ?? serializedType;
            }

            assemblyName = null;
            typeName     = mappedType.AssemblyQualifiedName;
        }
        public override Task Invoke(IIncomingLogicalMessageContext context, Func <Task> next)
        {
            var type = context.Message.MessageType;

            if (!context.Message.MessageType.IsInterface)
            {
                type = _mapper.GetMappedTypeFor(context.Message.MessageType);
            }

            if (_notRouted.Contains(type))
            {
                return(next());
            }

            var routeOn = GetRouteOn(type);

            if (routeOn == null)
            {
                _notRouted.Add(type);
                return(next());
            }

            // Got routed message, this is the first one so we are the lucky winner for the key
            // create connection to rabbit
            var rabbit = context.Builder.Build <IConnection>();

            var channel = rabbit.CreateModel();

            // bind InstanceSpecificQueue to header exchange
            var specs = new Dictionary <string, object>
            {
                { "x-match", "all" },
                { "Routing", routeOn.GetValue(context.Message.Instance).ToString() }
            };

            if (_routed.Contains($"{RoutingRoutingTopology.ExchangeName(type)}.routable.{specs["Routing"]}"))
            {
                Logger.Warn($"Received non-routed message {type} when we have routed it!");
                return(next());
            }

            _routed.Add(
                $"{RoutingRoutingTopology.ExchangeName(type)}.routable.{specs["Routing"]}");

            Logger.Debug($"Received routable message, binding to routing queue {RoutingRoutingTopology.ExchangeName(type)}.routable");
            channel.QueueBind(_instance, $"{RoutingRoutingTopology.ExchangeName(type)}.routable", string.Empty, specs);

            return(next());
        }
        private static List <string> GetNamespaces(IMessage[] messages, IMessageMapper mapper)
        {
            var result = new List <string>();

            foreach (IMessage m in messages)
            {
                string ns = mapper.GetMappedTypeFor(m.GetType()).Namespace;
                if (!result.Contains(ns))
                {
                    result.Add(ns);
                }
            }

            return(result);
        }
Esempio n. 16
0
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var mappedTypeFor = _messageMapper.GetMappedTypeFor(objectType);

            if (mappedTypeFor == null)
            {
                return(base.CreateObjectContract(objectType));
            }

            var jsonContract = base.CreateObjectContract(mappedTypeFor);

            jsonContract.DefaultCreator = () => _messageMapper.CreateInstance(mappedTypeFor);

            return(jsonContract);
        }
Esempio n. 17
0
        public async Task WriteEvents(String bucket, String stream, Int32 expectedVersion, IEnumerable <IWritableEvent> events, IDictionary <String, String> commitHeaders)
        {
            Logger.DebugFormat("Writing {0} events to stream id '{1}'.  Expected version: {2}", events.Count(), stream, expectedVersion);
            var streamId = String.Format("{0}.{1}", bucket, stream);

            if (_shouldCache)
            {
                _cache.Evict(streamId);
            }

            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All,
                Binder           = new EventSerializationBinder(_mapper)
            };

            var translatedEvents = events.Select(e =>
            {
                var descriptor = new EventDescriptor
                {
                    EntityType = e.Descriptor.EntityType,
                    Timestamp  = e.Descriptor.Timestamp,
                    Version    = e.Descriptor.Version,
                    Headers    = e.Descriptor.Headers.Merge(commitHeaders)
                };

                var mappedType = _mapper.GetMappedTypeFor(e.Event.GetType());


                return(new EventData(
                           e.EventId,
                           mappedType.AssemblyQualifiedName,
                           true,
                           e.Event.Serialize(settings).AsByteArray(),
                           descriptor.Serialize(settings).AsByteArray()
                           ));
            });

            try
            {
                await _client.AppendToStreamAsync(streamId, expectedVersion, translatedEvents);
            }
            catch (global::System.AggregateException e)
            {
                throw e.InnerException;
            }
        }
Esempio n. 18
0
        private object DecodeMessage(Message amqpMessage)
        {
            var  contentType          = amqpMessage.ApplicationProperties["LightRail.ContentType"] as string ?? "";
            var  enclosedMessageTypes = amqpMessage.ApplicationProperties["LightRail.EnclosedMessageTypes"] as string ?? "";
            Type messageType          = null;

            foreach (var typeName in enclosedMessageTypes.Split(','))
            {
                messageType = messageMapper.GetMappedTypeFor(typeName);
                if (messageType != null)
                {
                    break;
                }
            }
            var messageBodyBuffer = amqpMessage.Body as byte[];
            var message           = messageEncoder.Decode(messageBodyBuffer, messageType);

            return(message);
        }
Esempio n. 19
0
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var mappedTypeFor = objectType;

            if (mappedTypeFor.IsInterface)
            {
                mappedTypeFor = _mapper.GetMappedTypeFor(objectType);
            }

            if (mappedTypeFor == null)
            {
                return(base.CreateObjectContract(objectType));
            }

            var objectContract = base.CreateObjectContract(mappedTypeFor);

            objectContract.DefaultCreator = () => _mapper.CreateInstance(mappedTypeFor);

            return(objectContract);
        }
Esempio n. 20
0
        /// <summary>
        /// Creates a new <see cref="LogicalMessage"/> using the specified messageType, message instance and headers.
        /// </summary>
        /// <param name="messageType">The message type.</param>
        /// <param name="message">The message instance.</param>
        /// <param name="headers">The message headers.</param>
        /// <returns>A new <see cref="LogicalMessage"/>.</returns>
        public LogicalMessage Create(Type messageType, object message, Dictionary <string, string> headers)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (messageType == null)
            {
                throw new ArgumentNullException("messageType");
            }

            if (headers == null)
            {
                throw new ArgumentNullException("headers");
            }

            var realMessageType = messageMapper.GetMappedTypeFor(messageType);

            return(new LogicalMessage(messageMetadataRegistry.GetMessageMetadata(realMessageType), message, headers, this));
        }
Esempio n. 21
0
        private string GetDestinationServiceForMessage(Type messageType)
        {
            string destination;

            messageTypeToDestinationLocker.EnterReadLock();
            var destinationFound = messageTypeToDestinationLookup.TryGetValue(messageType, out destination);

            messageTypeToDestinationLocker.ExitReadLock();
            if (destinationFound)
            {
                return(destination);
            }

            if (!messageType.IsInterface)
            {
                var interfaces = messageType.GetInterfaces();
                foreach (var _interface in interfaces)
                {
                    messageTypeToDestinationLocker.EnterReadLock();
                    destinationFound = messageTypeToDestinationLookup.TryGetValue(_interface, out destination);
                    messageTypeToDestinationLocker.ExitReadLock();
                    if (destinationFound)
                    {
                        return(destination);
                    }
                }
            }

            if (messageMapper != null && !messageType.IsInterface)
            {
                var t = messageMapper.GetMappedTypeFor(messageType);
                if (t != null && t != messageType)
                {
                    return(GetDestinationServiceForMessage(t));
                }
            }

            return(destination);
        }
        public override Task Invoke(IOutgoingLogicalMessageContext context, Func <Task> next)
        {
            var type = _mapper.GetMappedTypeFor(context.Message.MessageType);

            if (_notRouted.Contains(type))
            {
                return(next());
            }

            var routeOn = GetRouteOn(type);

            if (routeOn == null)
            {
                _notRouted.Add(type);
                return(next());
            }

            Logger.Debug($"Outgoing message {type.FullName} is routable, adding routing header");
            // Add route to headers
            context.Headers["Routing"] = routeOn.GetValue(context.Message.Instance).ToString();

            return(next());
        }
        private static List <string> GetBaseTypes(IMessage[] messages, IMessageMapper mapper)
        {
            var result = new List <string>();

            foreach (IMessage m in messages)
            {
                Type t = mapper.GetMappedTypeFor(m.GetType());

                Type baseType = t.BaseType;
                while (baseType != typeof(object) && baseType != null)
                {
                    if (typeof(IMessage).IsAssignableFrom(baseType))
                    {
                        if (!result.Contains(baseType.FullName))
                        {
                            result.Add(baseType.FullName);
                        }
                    }

                    baseType = baseType.BaseType;
                }

                foreach (Type i in t.GetInterfaces())
                {
                    if (i != typeof(IMessage) && typeof(IMessage).IsAssignableFrom(i))
                    {
                        if (!result.Contains(i.FullName))
                        {
                            result.Add(i.FullName);
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 24
0
        public Action <IEventSource, Object> Resolve(IEventSource eventsource, Type eventType)
        {
            var mappedType = _mapper.GetMappedTypeFor(eventType);

            return(_cache.GetOrAdd(mappedType, (key) =>
            {
                var handleMethod = eventsource.GetType()
                                   .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
                                   .SingleOrDefault(
                    m => m.Name == "Handle" &&
                    m.GetParameters().Length == 1 &&
                    m.GetParameters().Single().ParameterType == mappedType &&
                    m.ReturnParameter.ParameterType == typeof(void));
                //.Select(m => new { Method = m, MessageType = m.GetParameters().Single().ParameterType });

                if (handleMethod == null)
                {
                    return null;
                }

                Action <IEventSource, Object> action = (es, m) => handleMethod.Invoke(es, new[] { m });
                return action;
            }));
        }
Esempio n. 25
0
        public async Task <int> WriteEvents(string stream, IEnumerable <IWritableEvent> events,
                                            IDictionary <string, string> commitHeaders, int?expectedVersion = null)
        {
            Logger.Write(LogLevel.Debug, () => $"Writing {events.Count()} events to stream id [{stream}].  Expected version: {expectedVersion}");

            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto,
                Binder           = new EventSerializationBinder(_mapper),
                //ContractResolver = new EventContractResolver(_mapper)
            };

            var translatedEvents = events.Select(e =>
            {
                var descriptor = new EventDescriptor
                {
                    EntityType = e.Descriptor.EntityType,
                    Timestamp  = e.Descriptor.Timestamp,
                    Version    = e.Descriptor.Version,
                    Headers    = commitHeaders != null ? commitHeaders.Merge(e.Descriptor.Headers) : e.Descriptor.Headers
                };

                var mappedType = e.Event.GetType();
                if (!mappedType.IsInterface)
                {
                    mappedType = _mapper.GetMappedTypeFor(mappedType) ?? mappedType;
                }


                var @event   = e.Event.Serialize(settings).AsByteArray();
                var metadata = descriptor.Serialize(settings).AsByteArray();
                if (_compress)
                {
                    @event   = @event.Compress();
                    metadata = metadata.Compress();
                }

                return(new EventData(
                           e.EventId ?? Guid.NewGuid(),
                           mappedType.AssemblyQualifiedName,
                           !_compress,
                           @event,
                           metadata
                           ));
            }).ToList();

            WrittenEvents.Update(events.Count());
            using (WriteTime.NewContext())
            {
                try
                {
                    var result = await
                                 _client.AppendToStreamAsync(stream, expectedVersion ?? ExpectedVersion.Any, translatedEvents)
                                 .ConfigureAwait(false);

                    return(result.NextExpectedVersion);
                }
                catch (WrongExpectedVersionException e)
                {
                    throw new VersionException(e.Message, e);
                }
                catch (CannotEstablishConnectionException e)
                {
                    throw new PersistenceException(e.Message, e);
                }
                catch (OperationTimedOutException e)
                {
                    throw new PersistenceException(e.Message, e);
                }
                catch (EventStoreConnectionException e)
                {
                    throw new PersistenceException(e.Message, e);
                }
            }
        }
 Type GetMappedType(Type serializedType)
 {
     return(messageMapper.GetMappedTypeFor(serializedType) ?? serializedType);
 }
Esempio n. 27
0
 public Type GetMappedTypeFor(Type type)
 {
     return(_mapper.GetMappedTypeFor(type));
 }
        private static List<string> GetNamespaces(IMessage[] messages, IMessageMapper mapper)
        {
            var result = new List<string>();

            foreach (IMessage m in messages)
            {
                string ns = mapper.GetMappedTypeFor(m.GetType()).Namespace;
                if (!result.Contains(ns))
                    result.Add(ns);
            }

            return result;
        }
        private static List<string> GetBaseTypes(IMessage[] messages, IMessageMapper mapper)
        {
            var result = new List<string>();

            foreach (IMessage m in messages)
            {
                Type t = mapper.GetMappedTypeFor(m.GetType());

                Type baseType = t.BaseType;
                while (baseType != typeof(object) && baseType != null)
                {
                    if (typeof(IMessage).IsAssignableFrom(baseType))
                        if (!result.Contains(baseType.FullName))
                            result.Add(baseType.FullName);

                    baseType = baseType.BaseType;
                }

                foreach (Type i in t.GetInterfaces())
                    if (i != typeof(IMessage) && typeof(IMessage).IsAssignableFrom(i))
                        if (!result.Contains(i.FullName))
                            result.Add(i.FullName);
            }

            return result;
        }
Esempio n. 30
0
        /// <summary>
        /// Deserializes the given stream to an array of messages which are returned.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public object[] Deserialize(Stream stream)
        {
            if (stream == null)
            {
                return(null);
            }

            prefixesToNamespaces = new Dictionary <string, string>();
            messageBaseTypes     = new List <Type>();
            var result = new List <object>();

            var doc = new XmlDocument {
                PreserveWhitespace = true
            };

            doc.Load(XmlReader.Create(stream, new XmlReaderSettings {
                CheckCharacters = false
            }));

            if (doc.DocumentElement == null)
            {
                return(result.ToArray());
            }

            foreach (XmlAttribute attr in doc.DocumentElement.Attributes)
            {
                if (attr.Name == "xmlns")
                {
                    defaultNameSpace = attr.Value.Substring(attr.Value.LastIndexOf("/") + 1);
                }
                else
                {
                    if (attr.Name.Contains("xmlns:"))
                    {
                        int    colonIndex = attr.Name.LastIndexOf(":");
                        string prefix     = attr.Name.Substring(colonIndex + 1);

                        if (prefix.Contains(BASETYPE))
                        {
                            Type baseType = mapper.GetMappedTypeFor(attr.Value);
                            if (baseType != null)
                            {
                                messageBaseTypes.Add(baseType);
                            }
                        }
                        else
                        {
                            prefixesToNamespaces[prefix] = attr.Value;
                        }
                    }
                }
            }

            if (doc.DocumentElement.Name.ToLower() != "messages")
            {
                object m = Process(doc.DocumentElement, null);

                if (m == null)
                {
                    throw new SerializationException("Could not deserialize message.");
                }

                result.Add(m);
            }
            else
            {
                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    if (node.NodeType == XmlNodeType.Whitespace)
                    {
                        continue;
                    }

                    object m = Process(node, null);

                    result.Add(m);
                }
            }

            defaultNameSpace = null;

            return(result.ToArray());
        }
Esempio n. 31
0
 string GetNamespace(object message)
 {
     //TODO: if the proxy type has the same NS as the real message type we don't need to look this up
     return(mapper.GetMappedTypeFor(message.GetType()).Namespace);
 }
Esempio n. 32
0
        public void Interfaces_with_only_properties_should_be_mapped()
        {
            mapper.Initialize(new[] { typeof(InterfaceWithProperties) });

            Assert.NotNull(mapper.GetMappedTypeFor(typeof(InterfaceWithProperties)));
        }