/// <summary>
        /// Creates a new Azure Service Bus client that facilitates receive operations.
        /// </summary>
        /// <typeparam name="TMessage">
        /// The type of the message that the client handles.
        /// </typeparam>
        /// <param name="entityType">
        /// The type of the entity.
        /// </param>
        /// <returns>
        /// A new Azure Service Bus client that facilitates receive operations.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="entityType" /> is equal to <see cref="MessagingEntityType.Unspecified" />.
        /// </exception>
        /// <exception cref="MessageSubscriptionException">
        /// An exception was raised while creating the client.
        /// </exception>
        public IReceiverClient CreateReceiveClient <TMessage>(MessagingEntityType entityType)
            where TMessage : class
        {
            switch (entityType.RejectIf().IsEqualToValue(MessagingEntityType.Unspecified, nameof(entityType)).TargetArgument)
            {
            case MessagingEntityType.Queue:

                try
                {
                    return(CreateQueueClient <TMessage>());
                }
                catch (Exception exception)
                {
                    throw new MessageSubscriptionException(typeof(TMessage), exception);
                }

            case MessagingEntityType.Topic:

                try
                {
                    return(CreateSubscriptionClient <TMessage>());
                }
                catch (Exception exception)
                {
                    throw new MessageSubscriptionException(typeof(TMessage), exception);
                }

            default:

                throw new InvalidOperationException($"The specified entity type, {entityType}, is not supported.");
            }
        }
Esempio n. 2
0
 internal HeartbeatScheduleItem(Int32 intervalInSeconds, MessagingEntityType entityType, String label)
     : base()
 {
     EntityType        = entityType.RejectIf().IsEqualToValue(MessagingEntityType.Unspecified, nameof(entityType));
     IntervalInSeconds = intervalInSeconds.RejectIf().IsLessThanOrEqualTo(0, nameof(intervalInSeconds));
     Label             = label;
 }
        private static String GetEntityPath <TMessage>(MessagingEntityType entityType)
            where TMessage : class
        {
            switch (entityType.RejectIf().IsEqualToValue(MessagingEntityType.Unspecified, nameof(entityType)).TargetArgument)
            {
            case MessagingEntityType.Queue:

                return(AzureServiceBusClientFactory.GetQueuePath <TMessage>());

            case MessagingEntityType.Topic:

                return(AzureServiceBusClientFactory.GetTopicPath <TMessage>());

            default:

                throw new InvalidOperationException($"The specified entity type, {entityType}, is not supported.");
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Asynchronously publishes the specified message to a bus.
        /// </summary>
        /// <typeparam name="TMessage">
        /// The type of the message to publish.
        /// </typeparam>
        /// <param name="message">
        /// The message to publish.
        /// </param>
        /// <param name="entityType">
        /// The targeted entity type.
        /// </param>
        /// <returns>
        /// A task representing the asynchronous operation.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="message" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="entityType" /> is equal to <see cref="MessagingEntityType.Unspecified" />.
        /// </exception>
        /// <exception cref="MessagePublishingException">
        /// An exception was raised while attempting to publish <paramref name="message" />.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        public async Task PublishAsync <TMessage>(TMessage message, MessagingEntityType entityType)
            where TMessage : class, IMessage
        {
            message    = message.RejectIf().IsNull(nameof(message)).TargetArgument;
            entityType = entityType.RejectIf().IsEqualToValue(MessagingEntityType.Unspecified, nameof(entityType));

            try
            {
                using (var controlToken = StateControl.Enter())
                {
                    RejectIfDisposed();
                    await PublishAsync(message, entityType, controlToken).ConfigureAwait(false);
                }
            }
            catch (MessagePublishingException)
            {
                throw;
            }
            catch (Exception exception)
            {
                throw new MessagePublishingException(typeof(TMessage), exception);
            }
        }
        /// <summary>
        /// Registers the specified message handler with the bus.
        /// </summary>
        /// <typeparam name="TMessage">
        /// The type of the message.
        /// </typeparam>
        /// <param name="messageHandler">
        /// An action that handles a message.
        /// </param>
        /// <param name="entityType">
        /// The targeted entity type.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="messageHandler" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="entityType" /> is equal to <see cref="MessagingEntityType.Unspecified" />.
        /// </exception>
        /// <exception cref="MessageSubscriptionException">
        /// An exception was raised while attempting to register <paramref name="messageHandler" />.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        public void RegisterHandler <TMessage>(Action <TMessage> messageHandler, MessagingEntityType entityType)
            where TMessage : class, IMessage
        {
            messageHandler = messageHandler.RejectIf().IsNull(nameof(messageHandler)).TargetArgument;
            entityType     = entityType.RejectIf().IsEqualToValue(MessagingEntityType.Unspecified, nameof(entityType)).TargetArgument;

            try
            {
                using (var controlToken = StateControl.Enter())
                {
                    RejectIfDisposed();
                    RegisterHandler(messageHandler, entityType, controlToken);
                }
            }
            catch (MessageSubscriptionException)
            {
                throw;
            }
            catch (Exception exception)
            {
                throw new MessageSubscriptionException(typeof(TMessage), exception);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageHandler{TMessage}" /> class.
 /// </summary>
 /// <param name="mediator">
 /// A processing intermediary that is used to process sub-commands.
 /// </param>
 /// <param name="role">
 /// The role of the handler.
 /// </param>
 /// <param name="entityType">
 /// The targeted entity type.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="mediator" /> is <see langword="null" />.
 /// </exception>
 /// <exception cref="ArgumentOutOfRangeException">
 /// <paramref name="role" /> is equal to <see cref="MessageHandlerRole.Unspecified" /> -or- <paramref name="entityType" /> is
 /// equal to <see cref="MessagingEntityType.Unspecified" />.
 /// </exception>
 protected MessageHandler(ICommandMediator mediator, MessageHandlerRole role, MessagingEntityType entityType)
     : base(mediator)
 {
     EntityType = entityType.RejectIf().IsEqualToValue(MessagingEntityType.Unspecified, nameof(entityType));
     Role       = role.RejectIf().IsEqualToValue(MessageHandlerRole.Unspecified, nameof(role));
 }