/// <summary>
        /// Gets the session state.
        /// </summary>
        ///
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
        ///
        /// <returns>The session state as <see cref="BinaryData"/>.</returns>
        /// <exception cref="ServiceBusException">
        ///   The lock for the session has expired.
        ///   The <see cref="ServiceBusException.Reason" /> will be set to <see cref="ServiceBusFailureReason.SessionLockLost"/> in this case.
        /// </exception>
        public virtual async Task <BinaryData> GetSessionStateAsync(CancellationToken cancellationToken = default)
        {
            Argument.AssertNotDisposed(IsClosed, nameof(ServiceBusSessionReceiver));
            _connection.ThrowIfClosed();
            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();
            Logger.GetSessionStateStart(Identifier, SessionId);
            using DiagnosticScope scope = ScopeFactory.CreateScope(
                      DiagnosticProperty.GetSessionStateActivityName,
                      DiagnosticProperty.ClientKind);
            scope.Start();

            BinaryData sessionState;

            try
            {
                sessionState = await InnerReceiver.GetStateAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                Logger.GetSessionStateException(Identifier, exception.ToString());
                scope.Failed(exception);
                throw;
            }

            Logger.GetSessionStateComplete(Identifier);
            return(sessionState);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceBusReceiver"/> class.
        /// </summary>
        ///
        /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with the Service Bus service.</param>
        /// <param name="entityPath"></param>
        /// <param name="isSessionEntity"></param>
        /// <param name="options">A set of options to apply when configuring the consumer.</param>
        /// <param name="sessionId"></param>
        ///
        internal ServiceBusReceiver(
            ServiceBusConnection connection,
            string entityPath,
            bool isSessionEntity,
            ServiceBusReceiverOptions options,
            string sessionId = default)
        {
            Argument.AssertNotNull(connection, nameof(connection));
            Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
            Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
            connection.ThrowIfClosed();

            options           = options?.Clone() ?? new ServiceBusReceiverOptions();
            Identifier        = DiagnosticUtilities.GenerateIdentifier(entityPath);
            _connection       = connection;
            _retryPolicy      = connection.RetryOptions.ToRetryPolicy();
            ReceiveMode       = options.ReceiveMode;
            PrefetchCount     = options.PrefetchCount;
            EntityPath        = entityPath;
            IsSessionReceiver = isSessionEntity;
            InnerReceiver     = _connection.CreateTransportReceiver(
                entityPath: EntityPath,
                retryPolicy: _retryPolicy,
                receiveMode: ReceiveMode,
                prefetchCount: (uint)PrefetchCount,
                identifier: Identifier,
                sessionId: sessionId,
                isSessionReceiver: IsSessionReceiver);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="ServiceBusSender"/> class.
        /// </summary>
        /// <param name="entityPath">The entity path to send the message to.</param>
        /// <param name="options">The set of <see cref="ServiceBusSenderOptions"/> to use for configuring
        /// this <see cref="ServiceBusSender"/>.</param>
        /// <param name="connection">The connection for the sender.</param>
        ///
        internal ServiceBusSender(
            string entityPath,
            ServiceBusSenderOptions options,
            ServiceBusConnection connection)
        {
            Logger.ClientCreateStart(typeof(ServiceBusSender), connection?.FullyQualifiedNamespace, entityPath);
            try
            {
                Argument.AssertNotNull(connection, nameof(connection));
                Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
                Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
                connection.ThrowIfClosed();

                options       = options?.Clone() ?? new ServiceBusSenderOptions();
                EntityPath    = entityPath;
                ViaEntityPath = options.ViaQueueOrTopicName;
                Identifier    = DiagnosticUtilities.GenerateIdentifier(EntityPath);
                _connection   = connection;
                _retryPolicy  = _connection.RetryOptions.ToRetryPolicy();
                _innerSender  = _connection.CreateTransportSender(
                    entityPath,
                    ViaEntityPath,
                    _retryPolicy,
                    Identifier);
                _scopeFactory = new EntityScopeFactory(EntityPath, FullyQualifiedNamespace);
            }
            catch (Exception ex)
            {
                Logger.ClientCreateException(typeof(ServiceBusSender), connection?.FullyQualifiedNamespace, entityPath, ex);
                throw;
            }
            Logger.ClientCreateComplete(typeof(ServiceBusSender), Identifier);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="ServiceBusProcessor"/> class.
        /// </summary>
        ///
        /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with the Service Bus service.</param>
        /// <param name="entityPath">The queue name or subscription path to process messages from.</param>
        /// <param name="isSessionEntity">Whether or not the processor is associated with a session entity.</param>
        /// <param name="plugins">The set of plugins to apply to incoming messages.</param>
        /// <param name="options">The set of options to use when configuring the processor.</param>
        /// <param name="sessionIds">An optional set of session Ids to limit processing to.
        /// Only applies if isSessionEntity is true.</param>
        /// <param name="maxConcurrentSessions">The max number of sessions that can be processed concurrently.
        /// Only applies if isSessionEntity is true.</param>
        /// <param name="maxConcurrentCallsPerSession">The max number of concurrent calls per session.
        /// Only applies if isSessionEntity is true.</param>
        internal ServiceBusProcessor(
            ServiceBusConnection connection,
            string entityPath,
            bool isSessionEntity,
            IList <ServiceBusPlugin> plugins,
            ServiceBusProcessorOptions options,
            string[] sessionIds              = default,
            int maxConcurrentSessions        = default,
            int maxConcurrentCallsPerSession = default)
        {
            Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
            Argument.AssertNotNull(connection, nameof(connection));
            Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
            connection.ThrowIfClosed();

            _options    = options?.Clone() ?? new ServiceBusProcessorOptions();
            _connection = connection;
            EntityPath  = entityPath;
            Identifier  = DiagnosticUtilities.GenerateIdentifier(EntityPath);

            ReceiveMode   = _options.ReceiveMode;
            PrefetchCount = _options.PrefetchCount;
            MaxAutoLockRenewalDuration   = _options.MaxAutoLockRenewalDuration;
            MaxConcurrentCalls           = _options.MaxConcurrentCalls;
            MaxConcurrentSessions        = maxConcurrentSessions;
            MaxConcurrentCallsPerSession = maxConcurrentCallsPerSession;
            _sessionIds = sessionIds ?? Array.Empty <string>();

            int maxCalls = isSessionEntity ?
                           (_sessionIds.Length > 0 ?
                            Math.Min(_sessionIds.Length, MaxConcurrentSessions) :
                            MaxConcurrentSessions) * MaxConcurrentCallsPerSession :
                           MaxConcurrentCalls;

            MessageHandlerSemaphore = new SemaphoreSlim(
                maxCalls,
                maxCalls);
            var maxAcceptSessions = Math.Min(maxCalls, 2 * Environment.ProcessorCount);

            MaxConcurrentAcceptSessionsSemaphore = new SemaphoreSlim(
                maxAcceptSessions,
                maxAcceptSessions);

            MaxReceiveWaitTime = _options.MaxReceiveWaitTime;
            AutoComplete       = _options.AutoComplete;

            EntityPath         = entityPath;
            IsSessionProcessor = isSessionEntity;
            _scopeFactory      = new EntityScopeFactory(EntityPath, FullyQualifiedNamespace);
            _plugins           = plugins;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceBusReceiver"/> class.
        /// </summary>
        ///
        /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with the Service Bus service.</param>
        /// <param name="entityPath"></param>
        /// <param name="isSessionEntity"></param>
        /// <param name="plugins">The plugins to apply to incoming messages.</param>
        /// <param name="options">A set of options to apply when configuring the consumer.</param>
        /// <param name="sessionId">An optional session Id to scope the receiver to. If not specified,
        /// the next available session returned from the service will be used.</param>
        ///
        internal ServiceBusReceiver(
            ServiceBusConnection connection,
            string entityPath,
            bool isSessionEntity,
            IList <ServiceBusPlugin> plugins,
            ServiceBusReceiverOptions options,
            string sessionId = default)
        {
            Type type = GetType();

            Logger.ClientCreateStart(type, connection?.FullyQualifiedNamespace, entityPath);
            try
            {
                Argument.AssertNotNull(connection, nameof(connection));
                Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
                Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
                connection.ThrowIfClosed();
                options           = options?.Clone() ?? new ServiceBusReceiverOptions();
                Identifier        = DiagnosticUtilities.GenerateIdentifier(entityPath);
                _connection       = connection;
                _retryPolicy      = connection.RetryOptions.ToRetryPolicy();
                ReceiveMode       = options.ReceiveMode;
                PrefetchCount     = options.PrefetchCount;
                EntityPath        = entityPath;
                IsSessionReceiver = isSessionEntity;
                _innerReceiver    = _connection.CreateTransportReceiver(
                    entityPath: EntityPath,
                    retryPolicy: _retryPolicy,
                    receiveMode: ReceiveMode,
                    prefetchCount: (uint)PrefetchCount,
                    identifier: Identifier,
                    sessionId: sessionId,
                    isSessionReceiver: IsSessionReceiver);
                _scopeFactory = new EntityScopeFactory(EntityPath, FullyQualifiedNamespace);
                _plugins      = plugins;
                if (!isSessionEntity)
                {
                    // don't log client completion for session receiver here as it is not complete until
                    // the link is opened.
                    Logger.ClientCreateComplete(type, Identifier);
                }
            }
            catch (Exception ex)
            {
                Logger.ClientCreateException(type, connection?.FullyQualifiedNamespace, entityPath, ex);
                throw;
            }
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="ServiceBusRuleManager"/> class.
        /// </summary>
        ///
        /// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with the Service Bus service.</param>
        /// <param name="subscriptionPath">The path of the Service Bus subscription to which the rule manager is bound.</param>
        ///
        internal ServiceBusRuleManager(
            ServiceBusConnection connection,
            string subscriptionPath)
        {
            Argument.AssertNotNull(connection, nameof(connection));
            Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
            Argument.AssertNotNullOrWhiteSpace(subscriptionPath, nameof(subscriptionPath));
            connection.ThrowIfClosed();

            Identifier       = DiagnosticUtilities.GenerateIdentifier(subscriptionPath);
            _connection      = connection;
            SubscriptionPath = subscriptionPath;
            InnerRuleManager = _connection.CreateTransportRuleManager(
                subscriptionPath: SubscriptionPath,
                retryPolicy: connection.RetryOptions.ToRetryPolicy());
        }
Ejemplo n.º 7
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="ServiceBusSender"/> class.
        /// </summary>
        /// <param name="entityPath">The entity path to send the message to.</param>
        /// <param name="connection">The connection for the sender.</param>
        ///
        internal ServiceBusSender(
            string entityPath,
            ServiceBusConnection connection)
        {
            Argument.AssertNotNull(connection, nameof(connection));
            Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
            Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
            connection.ThrowIfClosed();

            EntityPath   = entityPath;
            Identifier   = DiagnosticUtilities.GenerateIdentifier(EntityPath);
            _connection  = connection;
            _retryPolicy = _connection.RetryOptions.ToRetryPolicy();
            _innerSender = _connection.CreateTransportSender(
                entityPath,
                _retryPolicy);
        }