예제 #1
0
        public IHaveFulfilledSubscriptionRequirements WithMessageHandler <T>(IHandlerResolver handlerResolver) where T : Message
        {
            var thing = _subscriptionConfig.SubscriptionType == SubscriptionType.PointToPoint
                ? PointToPointHandler <T>()
                : TopicHandler <T>();

            Bus.SerialisationRegister.AddSerialiser <T>(_serialisationFactory.GetSerialiser <T>());

            var resolutionContext = new HandlerResolutionContext(_subscriptionConfig.QueueName);
            var proposedHandler   = handlerResolver.ResolveHandler <T>(resolutionContext);

            if (proposedHandler == null)
            {
                throw new HandlerNotRegisteredWithContainerException($"There is no handler for '{typeof(T).Name}' messages.");
            }

            foreach (var region in Bus.Config.Regions)
            {
                Bus.AddMessageHandler(region, _subscriptionConfig.QueueName, () => handlerResolver.ResolveHandler <T>(resolutionContext));
            }

            _log.LogInformation($"Added a message handler - Topic: {_subscriptionConfig.Topic}, QueueName: {_subscriptionConfig.QueueName}, HandlerName: IHandler<{typeof(T)}>");

            return(thing);
        }
예제 #2
0
        public IHaveFulfilledSubscriptionRequirements WithMessageHandler <T>(IHandlerResolver handlerResolver) where T : Message
        {
            if (_serializationFactory == null)
            {
                throw new InvalidOperationException($"No {nameof(IMessageSerializationFactory)} has been configured.");
            }

            var thing = _subscriptionConfig.SubscriptionType == SubscriptionType.PointToPoint
                ? PointToPointHandler <T>()
                : TopicHandler <T>();

            Bus.SerializationRegister.AddSerializer <T>(_serializationFactory.GetSerializer <T>());

            var resolutionContext = new HandlerResolutionContext(_subscriptionConfig.QueueName);
            var proposedHandler   = handlerResolver.ResolveHandler <T>(resolutionContext);

            if (proposedHandler == null)
            {
                throw new HandlerNotRegisteredWithContainerException($"There is no handler for '{typeof(T)}' messages.");
            }

            foreach (var region in Bus.Config.Regions)
            {
                Bus.AddMessageHandler(region, _subscriptionConfig.QueueName, () => handlerResolver.ResolveHandler <T>(resolutionContext));
            }

            _log.LogInformation(
                "Added a message handler for message type for '{MessageType}' on topic '{TopicName}' and queue '{QueueName}'.",
                typeof(T),
                _subscriptionConfig.Topic,
                _subscriptionConfig.QueueName);

            return(thing);
        }
예제 #3
0
        public IHaveFulfilledSubscriptionRequirements WithMessageHandler <T>(IHandlerResolver handlerResolver) where T : Message
        {
            if (handlerResolver is null)
            {
                throw new ArgumentNullException(nameof(handlerResolver));
            }

            _subscriptionConfig.TopicName = GetOrUseTopicNamingConvention <T>(_subscriptionConfig.TopicName);
            _subscriptionConfig.QueueName = GetOrUseQueueNamingConvention <T>(_subscriptionConfig.QueueName);
            _subscriptionConfig.SubscriptionGroupName ??= _subscriptionConfig.QueueName;

            var thing = _subscriptionConfig.SubscriptionType == SubscriptionType.PointToPoint
                ? PointToPointHandler <T>()
                : TopicHandler <T>();

            var resolutionContext = new HandlerResolutionContext(_subscriptionConfig.QueueName);
            var proposedHandler   = handlerResolver.ResolveHandler <T>(resolutionContext);

            if (proposedHandler == null)
            {
                throw new HandlerNotRegisteredWithContainerException($"There is no handler for '{typeof(T)}' messages.");
            }

            Bus.AddMessageHandler(_subscriptionConfig.QueueName, () => handlerResolver.ResolveHandler <T>(resolutionContext));

            _log.LogInformation(
                "Added a message handler for message type for '{MessageType}' on topic '{TopicName}' and queue '{QueueName}'.",
                typeof(T),
                _subscriptionConfig.TopicName,
                _subscriptionConfig.QueueName);

            return(thing);
        }
        /// <inheritdoc />
        public IHandlerAsync <T> ResolveHandler <T>(HandlerResolutionContext context)
        {
            bool logAtDebug = Logger.IsEnabled(LogLevel.Debug);

            if (logAtDebug)
            {
                Logger.LogDebug(
                    "Resolving handler for message type {MessageType} for queue {QueueName}.",
                    typeof(T).FullName,
                    context.QueueName);
            }

            var handlers = ServiceProvider.GetServices <IHandlerAsync <T> >().ToArray();

            if (handlers.Length == 0)
            {
                throw new InvalidOperationException($"No handler for message type {typeof(T).FullName} is registered.");
            }
            else if (handlers.Length > 1)
            {
                if (logAtDebug)
                {
                    Logger.LogDebug(
                        "Resolved handler types for message type {MessageType} for queue {QueueName}: {ResolvedHandlerTypes}",
                        typeof(T).FullName,
                        context.QueueName,
                        string.Join(", ", handlers.Select((p) => p.GetType().FullName)));
                }

                throw new NotSupportedException($"{handlers.Length} handlers for message type {typeof(T).FullName} are registered. Only one handler is supported per message type.");
            }

            var handler = handlers[0];

            if (logAtDebug)
            {
                Logger.LogDebug(
                    "Resolved handler of type {ResolvedHandlerType} for queue {QueueName}.",
                    handler.GetType().FullName,
                    context.QueueName);
            }

            return(handler);
        }
예제 #5
0
 /// <inheritdoc />
 public IHandlerAsync <T> ResolveHandler <T>(HandlerResolutionContext context)
 => Context.GetInstance <IHandlerAsync <T> >();
예제 #6
0
 /// <inheritdoc />
 public IHandlerAsync <T> ResolveHandler <T>(HandlerResolutionContext context)
 => ServiceProvider.GetRequiredService <IHandlerAsync <T> >();