static IEnumerable <Type> GetMessageTypes <TMessage>()
        {
            if (TypeMetadataCache <TMessage> .IsValidMessageType)
            {
                yield return(typeof(TMessage));
            }

            foreach (var baseInterface in GetImplementedInterfaces(typeof(TMessage)))
            {
                yield return(baseInterface);
            }

            var baseType = typeof(TMessage).GetTypeInfo().BaseType;

            while (baseType != null && TypeMetadataCache.IsValidMessageType(baseType))
            {
                yield return(baseType);

                foreach (var baseInterface in GetImplementedInterfaces(baseType))
                {
                    yield return(baseInterface);
                }

                baseType = baseType.GetTypeInfo().BaseType;
            }
        }
示例#2
0
        public IEnumerable <IMessageInterfaceType> GetMessageTypes()
        {
            var typeInfo = typeof(T).GetTypeInfo();

            if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(IJobConsumer <>))
            {
                var interfaceType = new JobInterfaceType(typeof(T).GetGenericArguments()[0], typeof(T));
                if (TypeMetadataCache.IsValidMessageType(interfaceType.MessageType))
                {
                    yield return(interfaceType);
                }
            }

            IEnumerable <IMessageInterfaceType> types = typeof(T).GetInterfaces()
                                                        .Where(x => x.GetTypeInfo().IsGenericType)
                                                        .Where(x => x.GetGenericTypeDefinition() == typeof(IJobConsumer <>))
                                                        .Select(x => new JobInterfaceType(x.GetGenericArguments()[0], typeof(T)))
                                                        .Where(x => TypeMetadataCache.IsValidMessageType(x.MessageType))
                                                        .Where(x => !x.MessageType.ClosesType(typeof(Batch <>)));

            foreach (var type in types)
            {
                yield return(type);
            }
        }
        public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
        {
            var typeInfo = typeToConvert.GetTypeInfo();

            if (_converterFactory.TryGetValue(typeInfo, out Func <JsonConverter> converterFactory))
            {
                return(converterFactory());
            }

            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
            {
                var interfaceType = typeInfo.GetGenericTypeDefinition();

                if (_openTypeFactory.TryGetValue(interfaceType, out var concreteType))
                {
                    Type[] arguments = typeInfo.GetGenericArguments();

                    if (arguments.Length == 1 && !arguments[0].IsGenericParameter)
                    {
                        interfaceType = interfaceType.MakeGenericType(arguments[0]);
                        concreteType  = concreteType.MakeGenericType(arguments[0]);

                        return((JsonConverter)Activator.CreateInstance(typeof(TypeMappingJsonConverter <,>).MakeGenericType(interfaceType, concreteType)));
                    }
                }
            }

            if (typeToConvert.IsInterfaceOrConcreteClass() && TypeMetadataCache.IsValidMessageType(typeToConvert) && !typeToConvert.IsValueTypeOrObject())
            {
                return((JsonConverter)Activator.CreateInstance(
                           typeof(InterfaceJsonConverter <,>).MakeGenericType(typeToConvert, TypeMetadataCache.GetImplementationType(typeToConvert))));
            }

            throw new MassTransitException($"Unsupported type for json serialization {TypeMetadataCache.GetShortName(typeInfo)}");
        }
        public override bool CanConvert(Type typeToConvert)
        {
            var typeInfo = typeToConvert.GetTypeInfo();

            if (!typeInfo.IsInterface)
            {
                return(false);
            }

            if (_converterFactory.TryGetValue(typeInfo, out Func <JsonConverter> _))
            {
                return(true);
            }

            if (_openTypeFactory.TryGetValue(typeInfo, out _))
            {
                return(true);
            }

            if (typeToConvert.IsInterfaceOrConcreteClass() && TypeMetadataCache.IsValidMessageType(typeToConvert) && !typeToConvert.IsValueTypeOrObject())
            {
                return(true);
            }

            return(false);
        }
示例#5
0
        protected override IConverter ValueFactory(Type objectType)
        {
            if (objectType.GetTypeInfo().IsInterface&& TypeMetadataCache.IsValidMessageType(objectType))
            {
                return((IConverter)Activator.CreateInstance(typeof(CachedConverter <>).MakeGenericType(objectType)));
            }

            return(new Unsupported());
        }
示例#6
0
        public Task <ISendEndpoint> GetPublishSendEndpoint(Type messageType)
        {
            if (!TypeMetadataCache.IsValidMessageType(messageType))
            {
                throw new MessageException(messageType, "Anonymous types are not valid message types");
            }

            var address = _nameFormatter.GetTopicAddress(_host, messageType);

            return(_sendEndpointProvider.GetSendEndpoint(address));
        }
示例#7
0
        public async Task <ISendEndpoint> GetPublishSendEndpoint(Type messageType)
        {
            if (!TypeMetadataCache.IsValidMessageType(messageType))
            {
                throw new MessageException(messageType, "Anonymous types are not valid message types");
            }

            ISendEndpoint[] result = await Task.WhenAll(_host.TransportAddresses.Select(x => _sendEndpointProvider.GetSendEndpoint(x)))
                                     .ConfigureAwait(false);

            return(new FanoutSendEndpoint(result));
        }
示例#8
0
        static IMessageFactory CreateMessageFactory(Type contractType)
        {
            if (!TypeMetadataCache.IsValidMessageType(contractType))
            {
                throw new ArgumentException(nameof(contractType));
            }

            Type[] parameterTypes = new Type[0];
            if (contractType.GetConstructor(parameterTypes) == null)
            {
                throw new ArgumentException("No default constructor available for message type", nameof(contractType));
            }

            return((IMessageFactory)Activator.CreateInstance(typeof(DynamicMessageFactory <>).MakeGenericType(contractType)));
        }
示例#9
0
        public bool TryGetMessage <T>(out ConsumeContext <T> message)
            where T : class
        {
            lock (_messageTypes)
            {
                object existing;
                if (_messageTypes.TryGetValue(typeof(T), out existing))
                {
                    message = existing as ConsumeContext <T>;
                    return(message != null);
                }

                if (typeof(T) == typeof(JToken))
                {
                    _messageTypes[typeof(T)] = message = new MessageConsumeContext <T>(this, _messageToken as T);
                    return(true);
                }

                string typeUrn = new MessageUrn(typeof(T)).ToString();

                if (_supportedTypes.Any(typeUrn.Equals))
                {
                    object obj;
                    Type   deserializeType = typeof(T);
                    if (deserializeType.IsInterface && TypeMetadataCache.IsValidMessageType(deserializeType))
                    {
                        deserializeType = TypeMetadataCache.GetImplementationType(deserializeType);
                    }

                    using (JsonReader jsonReader = _messageToken.CreateReader())
                    {
                        obj = _deserializer.Deserialize(jsonReader, deserializeType);
                    }

                    _messageTypes[typeof(T)] = message = new MessageConsumeContext <T>(this, (T)obj);
                    return(true);
                }

                _messageTypes[typeof(T)] = message = null;
                return(false);
            }
        }
        Task <ISendEndpoint> CreateSendEndpoint(Type messageType)
        {
            if (!TypeMetadataCache.IsValidMessageType(messageType))
            {
                throw new MessageException(messageType, "Anonymous types are not valid message types");
            }

            var sendSettings = _host.Settings.GetSendSettings(messageType);

            ExchangeBindingSettings[] bindings = TypeMetadataCache.GetMessageTypes(messageType)
                                                 .SelectMany(type => type.GetExchangeBindings(_host.Settings.MessageNameFormatter))
                                                 .Where(binding => !sendSettings.ExchangeName.Equals(binding.Exchange.ExchangeName))
                                                 .ToArray();

            var destinationAddress = _host.Settings.GetSendAddress(sendSettings);

            var modelCache = new RabbitMqModelCache(_host.ConnectionCache, _host.Supervisor, _modelSettings);

            var sendTransport = new RabbitMqSendTransport(modelCache, sendSettings, bindings);

            return(Task.FromResult <ISendEndpoint>(new SendEndpoint(sendTransport, _serializer, destinationAddress, _sourceAddress, SendPipe.Empty)));
        }
示例#11
0
 public override bool CanConvert(Type objectType)
 {
     return(objectType.GetTypeInfo().IsInterface&& TypeMetadataCache.IsValidMessageType(objectType));
 }