Пример #1
0
        private IEnumerable <ConsumerExecutorDescriptor> GetSubscribeAttributesDescription(TypeInfo typeInfo, TypeInfo serviceTypeInfo = null)
        {
            SubscribeAttribute topicClassAttribute = typeInfo.GetCustomAttribute <SubscribeAttribute>(inherit: true);

            foreach (MethodInfo method in typeInfo.GetRuntimeMethods())
            {
                IEnumerable <SubscribeAttribute> topicMethodAttributes = method.GetCustomAttributes <SubscribeAttribute>(inherit: true);
                if (topicClassAttribute == null)
                {
                    topicMethodAttributes = topicMethodAttributes.Where((SubscribeAttribute x) => !x.IsPartial);
                }
                if (!topicMethodAttributes.Any())
                {
                    continue;
                }
                foreach (SubscribeAttribute attr in topicMethodAttributes)
                {
                    SetSubscribeAttribute(attr);
                    List <ParameterDescriptor> parameters = (from parameter in method.GetParameters()
                                                             select new ParameterDescriptor
                    {
                        Name = parameter.Name,
                        ParameterType = parameter.ParameterType,
                        IsFromCap = parameter.GetCustomAttributes(typeof(FromCapAttribute)).Any()
                    }).ToList();
                    var subAttr = new CapSubscribeAttribute(attr.Name)
                    {
                        Group = attr.Group
                    };
                    yield return(InitDescriptor(subAttr, method, typeInfo, serviceTypeInfo, parameters, subAttr));
                }
            }
        }
        internal static ISubscriber BuildMessageHandler(IServiceProvider services, Type type)
        {
            var handler = (IMessageHandler <RabbitMQDelivery>)ActivatorUtilities.CreateInstance(services, type);

            if (handler is ISubscriber subscriber)
            {
                return(subscriber);
            }

            var acknowledgeMode      = AcknowledgeModeAttribute.FromType(type);
            var exceptionMode        = ExceptionModeAttribute.FromType(type);
            var subscription         = type.GetCustomAttribute <SubscribeAttribute>(true);
            var asyncContextAccessor = services.GetRequiredService <AsyncContextAccessor>();

            if (subscription == null)
            {
                var exchangeBinding = type.GetCustomAttribute <ExchangeBindingAttribute>(true);
                if (exchangeBinding != null)
                {
                    return(new ExchangeBindingSubscriber(
                               exchangeBinding.Exchange,
                               exchangeBinding.RoutingKey,
                               acknowledgeMode,
                               exceptionMode,
                               handler,
                               asyncContextAccessor));
                }

                subscription = SubscribeAttribute.FromType(type);
            }

            return(new QueueConsumerSubscriber(subscription.Queue, acknowledgeMode, exceptionMode, handler, asyncContextAccessor));
        }
Пример #3
0
		private void Subscribe(object handler, MethodInfo handlerMethod, SubscribeAttribute attr)
		{
			Type publishedType = GetPublishingType(handlerMethod, attr);
			var subs = EnsureSubscribers(publishedType);
			if (!subs.ContainsKey(handler))
			{
				subs.Add(handler, handlerMethod);
			}
		}
Пример #4
0
        protected override void OnCompleteFields(
            IDictionary <NameString, ObjectFieldDefinition> fields,
            ISet <MemberInfo> handledMembers)
        {
            HashSet <string> subscribeResolver = null;

            if (Definition.Fields.IsImplicitBinding())
            {
                FieldDescriptorUtilities.AddImplicitFields(
                    this,
                    Definition.FieldBindingType,
                    p =>
                {
                    var descriptor = ObjectFieldDescriptor.New(
                        Context, p, Definition.RuntimeType, Definition.FieldBindingType);
                    Fields.Add(descriptor);
                    return(descriptor.CreateDefinition());
                },
                    fields,
                    handledMembers,
                    include: IncludeField);
            }

            base.OnCompleteFields(fields, handledMembers);

            bool IncludeField(IReadOnlyList <MemberInfo> all, MemberInfo current)
            {
                if (subscribeResolver is null)
                {
                    subscribeResolver = new HashSet <string>();

                    foreach (MemberInfo member in all)
                    {
                        if (member.IsDefined(typeof(SubscribeAttribute)))
                        {
                            SubscribeAttribute attribute =
                                member.GetCustomAttribute <SubscribeAttribute>();
                            if (attribute.With is not null)
                            {
                                subscribeResolver.Add(attribute.With);
                            }
                        }
                    }
                }

                return(!subscribeResolver.Contains(current.Name));
            }
        }
Пример #5
0
		private Type GetPublishingType(MethodInfo handlerMethod, SubscribeAttribute attr)
		{
			if (attr.PublishedType != null)
			{
				return attr.PublishedType;
			}
			else
			{
				var paramInfo = handlerMethod.GetParameters();
				if (paramInfo.Length == 0)
				{
					throw new ArgumentException("Method cannot have zero parameters", "handlerMethod");
				}
				return paramInfo[0].ParameterType;
			}
		}
Пример #6
0
 private void SetSubscribeAttribute(SubscribeAttribute attribute)
 {
     attribute.Group = (attribute.Group ?? _capOptions.DefaultGroup) + "." + _capOptions.Version;
 }
Пример #7
0
 public MethodValue(MethodInfo method, SubscribeAttribute subscribe)
 {
     Method    = method;
     Subscribe = subscribe;
 }
Пример #8
0
        private static ISubscribeInfoFactoryProvider CreateSubscribeInfoFactoryProvider(MethodInfo m, SubscribeAttribute attr)
        {
            var ps     = m.GetParameters();
            var length = ps.Length;

            if (length > 2)
            {
                return(null);
            }
            var returnType = m.ReturnType;

            if (returnType == Types.Void)
            {
                switch (length)
                {
                case 0:
                    if (m.IsStatic)
                    {
                        return(new StaticActionSubscribeProvider(m, attr.Topic, attr.Mode));
                    }
                    return(new ActionSubscribeProvider(m, attr.Topic, attr.Mode));

                case 1:
                    if (m.IsStatic)
                    {
                        return(new StaticAction1SubscribeProvider(m, ps, attr.Topic, attr.Mode));
                    }
                    return(new Action1SubscribeProvider(m, ps, attr.Topic, attr.Mode));

                case 2:
                    if (m.IsStatic)
                    {
                        return(new StaticAction2SubscribeProvider(m, ps, attr.Topic, attr.Mode));
                    }
                    return(new Action2SubscribeProvider(m, ps, attr.Topic, attr.Mode));
                }
            }
            else
            {
                switch (length)
                {
                case 0:
                    if (m.IsStatic)
                    {
                        return(new StaticFuncSubscribeProvider(m, attr.Topic, attr.Mode));
                    }
                    return(new FuncSubscribeProvider(m, attr.Topic, attr.Mode));

                case 1:
                    if (m.IsStatic)
                    {
                        return(new StaticFunc1SubscribeProvider(m, ps, attr.Topic, attr.Mode));
                    }
                    return(new Func1SubscribeProvider(m, ps, attr.Topic, attr.Mode));

                case 2:
                    if (m.IsStatic)
                    {
                        return(new StaticFunc2SubscribeProvider(m, ps, attr.Topic, attr.Mode));
                    }
                    return(new Func2SubscribeProvider(m, ps, attr.Topic, attr.Mode));
                }
            }

            return(null);
        }