< Summary

Class:Azure.Messaging.ServiceBus.ServiceBusClient
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Client\ServiceBusClient.cs
Covered lines:77
Uncovered lines:94
Coverable lines:171
Total lines:655
Line coverage:45% (77 of 171)
Covered branches:21
Total branches:24
Branch coverage:87.5% (21 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_FullyQualifiedNamespace()-100%100%
get_IsDisposed()-0%100%
get_TransportType()-0%100%
get_Identifier()-100%100%
get_Logger()-0%100%
get_Plugins()-0%100%
DisposeAsync()-0%100%
.ctor()-0%100%
get_Connection()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-50%100%
.ctor(...)-100%100%
CreateSender(...)-100%100%
CreateSender(...)-100%100%
CreateReceiver(...)-100%100%
CreateReceiver(...)-100%100%
CreateReceiver(...)-0%100%
CreateReceiver(...)-0%100%
CreateSessionReceiverAsync()-0%100%
CreateSessionReceiverAsync()-0%100%
CreateDeadLetterReceiver(...)-0%100%
CreateDeadLetterReceiver(...)-0%100%
CreateProcessor(...)-0%100%
CreateProcessor(...)-100%100%
CreateProcessor(...)-0%100%
CreateProcessor(...)-0%100%
CreateSessionProcessor(...)-100%50%
CreateSessionProcessor(...)-0%0%
CreateRuleManager(...)-0%100%
ValidateEntityName(...)-100%100%
ValidateSendViaEntityName(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Client\ServiceBusClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Threading;
 8using System.Threading.Tasks;
 9using Azure.Core;
 10using Azure.Messaging.ServiceBus.Diagnostics;
 11using Azure.Messaging.ServiceBus.Plugins;
 12
 13namespace Azure.Messaging.ServiceBus
 14{
 15    /// <summary>
 16    /// The <see cref="ServiceBusClient"/> is the top-level client through which all Service Bus
 17    /// entities can be interacted with. Any lower level types retrieved from here, such
 18    /// as <see cref="ServiceBusSender"/> and <see cref="ServiceBusReceiver"/> will share the
 19    /// same AMQP connection. Disposing the <see cref="ServiceBusClient"/> will cause the AMQP
 20    /// connection to close.
 21    /// </summary>
 22    public class ServiceBusClient : IAsyncDisposable
 23    {
 24
 25        private readonly ServiceBusClientOptions _options;
 26
 27        /// <summary>
 28        ///   The fully qualified Service Bus namespace that the connection is associated with.  This is likely
 29        ///   to be similar to <c>{yournamespace}.servicebus.windows.net</c>.
 30        /// </summary>
 31        ///
 7832        public string FullyQualifiedNamespace => Connection.FullyQualifiedNamespace;
 33
 34        /// <summary>
 35        ///   Indicates whether or not this <see cref="ServiceBusClient"/> has been disposed.
 36        /// </summary>
 37        ///
 38        /// <value>
 39        ///   <c>true</c> if the client is disposed; otherwise, <c>false</c>.
 40        /// </value>
 041        public bool IsDisposed { get; private set; } = false;
 42
 43        /// <summary>
 44        /// The transport type used for this <see cref="ServiceBusClient"/>.
 45        /// </summary>
 046        public ServiceBusTransportType TransportType { get; }
 47
 48        /// <summary>
 49        ///   A unique name used to identify this <see cref="ServiceBusClient"/>.
 50        /// </summary>
 4451        internal string Identifier { get; }
 52
 53        /// <summary>
 54        ///   The instance of <see cref="ServiceBusEventSource" /> which can be mocked for testing.
 55        /// </summary>
 56        ///
 057        internal ServiceBusEventSource Logger { get; set; } = ServiceBusEventSource.Log;
 58
 59        /// <summary>
 60        /// The list of plugins for the client.
 61        /// </summary>
 062        internal IList<ServiceBusPlugin> Plugins { get; set; } = new List<ServiceBusPlugin>();
 63
 64        /// <summary>
 65        ///   Performs the task needed to clean up resources used by the <see cref="ServiceBusConnection" />,
 66        ///   including ensuring that the connection itself has been closed.
 67        /// </summary>
 68        ///
 69        /// <returns>A task to be resolved on when the operation has completed.</returns>
 70        [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju
 71        public virtual async ValueTask DisposeAsync()
 72        {
 073            Logger.ClientDisposeStart(typeof(ServiceBusClient), Identifier);
 074            IsDisposed = true;
 75            try
 76            {
 077                await Connection.CloseAsync(CancellationToken.None).ConfigureAwait(false);
 078            }
 079            catch (Exception ex)
 80            {
 081                Logger.ClientDisposeException(typeof(ServiceBusClient), Identifier, ex);
 082                throw;
 83            }
 84            finally
 85            {
 086                Logger.ClientDisposeComplete(typeof(ServiceBusClient), Identifier);
 87            }
 088        }
 89
 90        /// <summary>
 91        /// Can be used for mocking.
 92        /// </summary>
 093        protected ServiceBusClient()
 94        {
 095        }
 96
 97        /// <summary>
 98        /// The connection that is used for the client.
 99        /// </summary>
 132100        internal ServiceBusConnection Connection { get; }
 101
 102        /// <summary>
 103        /// Initializes a new instance of the <see cref="ServiceBusClient"/> class.
 104        /// </summary>
 105        ///
 106        /// <param name="connectionString">The connection string to use for connecting to the
 107        /// Service Bus namespace.</param>
 108        ///
 109        /// <remarks>
 110        /// If the connection string specifies a specific entity name, any subsequent calls to
 111        /// <see cref="CreateSender(string)"/>, <see cref="CreateReceiver(string)"/>,
 112        /// <see cref="CreateProcessor(string)"/> etc. must still specify the same entity name.
 113        /// </remarks>
 114        public ServiceBusClient(string connectionString) :
 38115            this(connectionString, new ServiceBusClientOptions())
 116        {
 28117        }
 118
 119        /// <summary>
 120        /// Initializes a new instance of the <see cref="ServiceBusClient"/> class.
 121        /// </summary>
 122        ///
 123        /// <param name="connectionString">The connection string to use for connecting to the
 124        /// Service Bus namespace.</param>
 125        /// <param name="options">The set of <see cref="ServiceBusClientOptions"/> to use for
 126        /// configuring this <see cref="ServiceBusClient"/>.</param>
 127        ///
 128        /// <remarks>
 129        /// If the connection string specifies a specific entity name, any subsequent calls to
 130        /// <see cref="CreateSender(string)"/>, <see cref="CreateReceiver(string)"/>,
 131        /// <see cref="CreateProcessor(string)"/> etc. must still specify the same entity name.
 132        /// </remarks>
 54133        public ServiceBusClient(string connectionString, ServiceBusClientOptions options)
 134        {
 54135            _options = options?.Clone() ?? new ServiceBusClientOptions();
 54136            Connection = new ServiceBusConnection(connectionString, _options);
 38137            Logger.ClientCreateStart(typeof(ServiceBusClient), FullyQualifiedNamespace);
 38138            Identifier = DiagnosticUtilities.GenerateIdentifier(FullyQualifiedNamespace);
 38139            Plugins = _options.Plugins;
 38140            Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier);
 38141        }
 142
 143        /// <summary>
 144        /// Initializes a new instance of the <see cref="ServiceBusClient"/> class.
 145        /// </summary>
 146        ///
 147        /// <param name="fullyQualifiedNamespace">The fully qualified Service Bus namespace to connect to.
 148        /// This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
 149        /// <param name="credential">The Azure managed identity credential to use for authorization.  Access controls ma
 150        public ServiceBusClient(string fullyQualifiedNamespace, TokenCredential credential) :
 8151            this(fullyQualifiedNamespace, credential, new ServiceBusClientOptions())
 152        {
 0153        }
 154
 155        /// <summary>
 156        /// Initializes a new instance of the <see cref="ServiceBusClient"/> class.
 157        /// </summary>
 158        ///
 159        /// <param name="fullyQualifiedNamespace">The fully qualified Service Bus namespace to connect to.
 160        /// This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
 161        /// <param name="credential">The Azure managed identity credential to use for authorization.  Access controls ma
 162        /// <param name="options">The set of <see cref="ServiceBusClientOptions"/> to use for configuring this <see cref
 14163        public ServiceBusClient(
 14164            string fullyQualifiedNamespace,
 14165            TokenCredential credential,
 14166            ServiceBusClientOptions options)
 167        {
 14168            _options = options?.Clone() ?? new ServiceBusClientOptions();
 14169            Logger.ClientCreateStart(typeof(ServiceBusClient), fullyQualifiedNamespace);
 14170            Identifier = DiagnosticUtilities.GenerateIdentifier(fullyQualifiedNamespace);
 14171            Connection = new ServiceBusConnection(
 14172                fullyQualifiedNamespace,
 14173                credential,
 14174                _options);
 4175            Plugins = _options.Plugins;
 4176            Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier);
 4177        }
 178
 179        /// <summary>
 180        /// Creates a <see cref="ServiceBusSender"/> instance that can be used for sending messages to a specific
 181        /// queue or topic.
 182        /// </summary>
 183        ///
 184        /// <param name="queueOrTopicName">The queue or topic to create a <see cref="ServiceBusSender"/>
 185        /// for.</param>
 186        ///
 187        /// <returns>A <see cref="ServiceBusSender"/> scoped to the specified queue or topic.</returns>
 188        public ServiceBusSender CreateSender(string queueOrTopicName)
 189        {
 6190            ValidateEntityName(queueOrTopicName);
 191
 4192            return new ServiceBusSender(
 4193                entityPath: queueOrTopicName,
 4194                options: new ServiceBusSenderOptions(),
 4195                connection: Connection,
 4196                plugins: Plugins);
 197        }
 198
 199        /// <summary>
 200        /// Creates a <see cref="ServiceBusSender"/> instance that can be used for sending messages to a specific
 201        /// queue or topic.
 202        /// </summary>
 203        ///
 204        /// <param name="queueOrTopicName">The queue or topic to create a <see cref="ServiceBusSender"/> for.</param>
 205        /// <param name="options">The set of <see cref="ServiceBusSenderOptions"/> to use for configuring
 206        /// this <see cref="ServiceBusSender"/>.</param>
 207        ///
 208        /// <returns>A <see cref="ServiceBusSender"/> scoped to the specified queue or topic.</returns>
 209        public ServiceBusSender CreateSender(string queueOrTopicName, ServiceBusSenderOptions options)
 210        {
 12211            ValidateSendViaEntityName(queueOrTopicName, options?.ViaQueueOrTopicName);
 212
 8213            return new ServiceBusSender(
 8214                entityPath: queueOrTopicName,
 8215                options: options,
 8216                connection: Connection,
 8217                plugins: Plugins);
 218        }
 219
 220        /// <summary>
 221        /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving and settling messages
 222        /// from a specific queue. It uses <see cref="ReceiveMode"/> to specify how messages are received. Defaults to P
 223        /// If you want to change the <see cref="ReceiveMode"/>, use <see cref="CreateReceiver(string, ServiceBusReceive
 224        /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusReceiverOptions"/>.
 225        /// </summary>
 226        ///
 227        /// <param name="queueName">The queue to create a <see cref="ServiceBusReceiver"/> for.</param>
 228        /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified queue.</returns>
 229        public ServiceBusReceiver CreateReceiver(string queueName)
 230        {
 2231            ValidateEntityName(queueName);
 232
 2233            return new ServiceBusReceiver(
 2234                connection: Connection,
 2235                entityPath: queueName,
 2236                isSessionEntity: false,
 2237                plugins: Plugins,
 2238                options: new ServiceBusReceiverOptions());
 239        }
 240
 241        /// <summary>
 242        /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving and settling messages
 243        /// from a specific queue. It uses <see cref="ReceiveMode"/> to specify how messages are received. Defaults to P
 244        /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusReceiverOptions"/>.
 245        /// </summary>
 246        ///
 247        /// <param name="queueName">The queue to create a <see cref="ServiceBusReceiver"/> for.</param>
 248        /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the
 249        /// <see cref="ServiceBusReceiver"/>.</param>
 250        ///
 251        /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified queue.</returns>
 252        public ServiceBusReceiver CreateReceiver(
 253            string queueName,
 254            ServiceBusReceiverOptions options)
 255        {
 2256            ValidateEntityName(queueName);
 257
 2258            return new ServiceBusReceiver(
 2259                connection: Connection,
 2260                entityPath: queueName,
 2261                isSessionEntity: false,
 2262                plugins: Plugins,
 2263                options: options);
 264        }
 265
 266        /// <summary>
 267        /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving and
 268        /// settling messages from a specific subscription. It uses <see cref="ReceiveMode"/> to specify
 269        /// how messages are received. Defaults to PeekLock mode. If you want to change the <see cref="ReceiveMode"/>,
 270        /// use <see cref="CreateReceiver(string, string, ServiceBusReceiverOptions)"/> method.
 271        /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusReceiverOptions"/>.
 272        /// </summary>
 273        ///
 274        /// <param name="topicName">The topic to create a <see cref="ServiceBusReceiver"/> for.</param>
 275        /// <param name="subscriptionName">The subscription specific to the specified topic to create
 276        /// a <see cref="ServiceBusReceiver"/> for.</param>
 277        ///
 278        /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified subscription and topic.</returns>
 279        public ServiceBusReceiver CreateReceiver(
 280            string topicName,
 281            string subscriptionName)
 282        {
 0283            ValidateEntityName(topicName);
 284
 0285            return new ServiceBusReceiver(
 0286                connection: Connection,
 0287                entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName),
 0288                isSessionEntity: false,
 0289                plugins: Plugins,
 0290                options: new ServiceBusReceiverOptions());
 291        }
 292
 293        /// <summary>
 294        /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for
 295        /// receiving and settling messages from a specific subscription. It uses <see cref="ReceiveMode"/> to specify
 296        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 297        /// </summary>
 298        ///
 299        /// <param name="topicName">The topic to create a <see cref="ServiceBusReceiver"/> for.</param>
 300        /// <param name="subscriptionName">The subscription specific to the specified topic to create
 301        /// a <see cref="ServiceBusReceiver"/> for.</param>
 302        /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the
 303        /// <see cref="ServiceBusReceiver"/>.</param>
 304        ///
 305        /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified subscription and topic.</returns>
 306        public ServiceBusReceiver CreateReceiver(
 307            string topicName,
 308            string subscriptionName,
 309            ServiceBusReceiverOptions options)
 310        {
 0311            ValidateEntityName(topicName);
 312
 0313            return new ServiceBusReceiver(
 0314                connection: Connection,
 0315                entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName),
 0316                isSessionEntity: false,
 0317                plugins: Plugins,
 0318                options: options);
 319        }
 320
 321        /// <summary>
 322        /// Creates a <see cref="ServiceBusSessionReceiver"/> instance that can be used for receiving
 323        /// and settling messages from a specific session-enabled queue. It uses <see cref="ReceiveMode"/> to specify
 324        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 325        /// </summary>
 326        ///
 327        /// <param name="queueName">The session-enabled queue to create a <see cref="ServiceBusSessionReceiver"/> for.</
 328        /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the
 329        /// <see cref="ServiceBusSessionReceiver"/>.</param>
 330        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t
 331        ///
 332        /// <remarks>Because this is establishing a session lock, this method performs a service call. If the
 333        /// sessionId parameter is not specified, and there are no available messages in the queue, this will
 334        /// throw a <see cref="ServiceBusException"/> with <see cref="ServiceBusException.Reason"/> of <see cref="Servic
 335        /// </remarks>
 336        ///
 337        /// <returns>A <see cref="ServiceBusSessionReceiver"/> scoped to the specified queue and a specific session.</re
 338        public virtual async Task<ServiceBusSessionReceiver> CreateSessionReceiverAsync(
 339            string queueName,
 340            ServiceBusSessionReceiverOptions options = default,
 341            CancellationToken cancellationToken = default)
 342        {
 0343            ValidateEntityName(queueName);
 344
 0345            return await ServiceBusSessionReceiver.CreateSessionReceiverAsync(
 0346                entityPath: queueName,
 0347                connection: Connection,
 0348                plugins: Plugins,
 0349                options: options,
 0350                cancellationToken: cancellationToken).ConfigureAwait(false);
 0351        }
 352
 353        /// <summary>
 354        /// Creates a <see cref="ServiceBusSessionReceiver"/> instance that can be used for receiving
 355        /// and settling messages from a specific session-enabled subscription. It uses <see cref="ReceiveMode"/> to spe
 356        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 357        /// </summary>
 358        ///
 359        /// <param name="topicName">The topic to create a <see cref="ServiceBusSessionReceiver"/> for.</param>
 360        /// <param name="subscriptionName">The session-enabled subscription to create a <see cref="ServiceBusSessionRece
 361        /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the
 362        /// <see cref="ServiceBusSessionReceiver"/>.</param>
 363        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t
 364        ///
 365        /// <remarks>Because this is establishing a session lock, this method performs a service call. If the
 366        /// sessionId parameter is not specified, and there are no available messages in the queue, this will
 367        /// throw a <see cref="ServiceBusException"/> with <see cref="ServiceBusException.Reason"/> of <see cref="Servic
 368        /// </remarks>
 369        ///
 370        /// <returns>A <see cref="ServiceBusSessionReceiver"/> scoped to the specified queue and a specific session.</re
 371        public virtual async Task<ServiceBusSessionReceiver> CreateSessionReceiverAsync(
 372            string topicName,
 373            string subscriptionName,
 374            ServiceBusSessionReceiverOptions options = default,
 375            CancellationToken cancellationToken = default)
 376        {
 0377            ValidateEntityName(topicName);
 378
 0379            return await ServiceBusSessionReceiver.CreateSessionReceiverAsync(
 0380                entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName),
 0381                connection: Connection,
 0382                plugins: Plugins,
 0383                options: options,
 0384                cancellationToken: cancellationToken).ConfigureAwait(false);
 0385        }
 386
 387        /// <summary>
 388        /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving from the
 389        /// dead letter queue for the specified queue. It uses <see cref="ReceiveMode"/> to specify
 390        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 391        /// </summary>
 392        ///
 393        /// <param name="queueName">The queue to create a <see cref="ServiceBusReceiver"/> for.</param>
 394        /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the
 395        /// <see cref="ServiceBusReceiver"/>.</param>
 396        ///
 397        /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the dead letter queue of the specified
 398        /// queue.</returns>
 399        public ServiceBusReceiver CreateDeadLetterReceiver(
 400            string queueName,
 401            ServiceBusReceiverOptions options = default)
 402        {
 0403            ValidateEntityName(queueName);
 404
 0405            return new ServiceBusReceiver(
 0406                connection: Connection,
 0407                entityPath: EntityNameFormatter.FormatDeadLetterPath(queueName),
 0408                isSessionEntity: false,
 0409                plugins: Plugins,
 0410                options: options);
 411        }
 412
 413        /// <summary>
 414        /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving from the
 415        /// dead letter queue for the specified subscription. It uses <see cref="ReceiveMode"/> to specify
 416        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 417        /// </summary>
 418        ///
 419        /// <param name="topicName">The topic to create a <see cref="ServiceBusReceiver"/> for.</param>
 420        /// <param name="subscriptionName">The subscription to create a <see cref="ServiceBusReceiver"/> for.</param>
 421        /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the
 422        /// <see cref="ServiceBusReceiver"/>.</param>
 423        ///
 424        /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the dead letter queue of the specified
 425        /// queue.</returns>
 426        public ServiceBusReceiver CreateDeadLetterReceiver(
 427            string topicName,
 428            string subscriptionName,
 429            ServiceBusReceiverOptions options = default)
 430        {
 0431            ValidateEntityName(topicName);
 432
 0433            return new ServiceBusReceiver(
 0434                connection: Connection,
 0435                entityPath: EntityNameFormatter.FormatDeadLetterPath(
 0436                    EntityNameFormatter.FormatSubscriptionPath(
 0437                        topicName,
 0438                        subscriptionName)),
 0439                isSessionEntity: false,
 0440                plugins: Plugins,
 0441                options: options);
 442        }
 443
 444        /// <summary>
 445        /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using
 446        /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify
 447        /// how messages are received. Defaults to PeekLock mode. If you want to change the <see cref="ReceiveMode"/>,
 448        /// use <see cref="CreateProcessor(string, ServiceBusProcessorOptions)"/> method.
 449        /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusProcessorOptions"/> type.
 450        /// </summary>
 451        ///
 452        /// <param name="queueName">The queue to create a <see cref="ServiceBusProcessor"/> for.</param>
 453        ///
 454        /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified queue.</returns>
 455        public ServiceBusProcessor CreateProcessor(string queueName)
 456        {
 0457            ValidateEntityName(queueName);
 458
 0459            return new ServiceBusProcessor(
 0460                entityPath: queueName,
 0461                connection: Connection,
 0462                isSessionEntity: false,
 0463                plugins: Plugins,
 0464                options: new ServiceBusProcessorOptions());
 465        }
 466
 467        /// <summary>
 468        /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using
 469        /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify
 470        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 471        /// </summary>
 472        ///
 473        /// <param name="queueName">The queue to create a <see cref="ServiceBusProcessor"/> for.</param>
 474        /// <param name="options">The set of <see cref="ServiceBusProcessorOptions"/> to use for configuring the
 475        /// <see cref="ServiceBusProcessor"/>.</param>
 476        ///
 477        /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified queue.</returns>
 478        public ServiceBusProcessor CreateProcessor(
 479            string queueName,
 480            ServiceBusProcessorOptions options)
 481        {
 2482            ValidateEntityName(queueName);
 483
 2484            return new ServiceBusProcessor(
 2485                entityPath: queueName,
 2486                connection: Connection,
 2487                isSessionEntity: false,
 2488                plugins: Plugins,
 2489                options: options);
 490        }
 491
 492        /// <summary>
 493        /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using
 494        /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify
 495        /// how messages are received. Defaults to PeekLock mode. If you want to change the <see cref="ReceiveMode"/>,
 496        /// use <see cref="CreateProcessor(string, string, ServiceBusProcessorOptions)"/> method.
 497        /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusProcessorOptions"/> type.
 498        /// </summary>
 499        ///
 500        /// <param name="topicName">The topic to create a <see cref="ServiceBusProcessor"/> for.</param>
 501        /// <param name="subscriptionName">The subcription to create a <see cref="ServiceBusProcessor"/> for.</param>
 502        ///
 503        /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified topic and subscription.</returns>
 504        public ServiceBusProcessor CreateProcessor(
 505            string topicName,
 506            string subscriptionName)
 507        {
 0508            ValidateEntityName(topicName);
 509
 0510            return new ServiceBusProcessor(
 0511                entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName),
 0512                connection: Connection,
 0513                isSessionEntity: false,
 0514                plugins: Plugins,
 0515                options: new ServiceBusProcessorOptions());
 516        }
 517
 518        /// <summary>
 519        /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using
 520        /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify
 521        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 522        /// </summary>
 523        ///
 524        /// <param name="topicName">The topic to create a <see cref="ServiceBusProcessor"/> for.</param>
 525        /// <param name="subscriptionName">The subcription to create a <see cref="ServiceBusProcessor"/> for.</param>
 526        /// <param name="options">The set of <see cref="ServiceBusProcessorOptions"/> to use for configuring the
 527        /// <see cref="ServiceBusProcessor"/>.</param>
 528        ///
 529        /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified topic and subscription.</returns>
 530        public ServiceBusProcessor CreateProcessor(
 531            string topicName,
 532            string subscriptionName,
 533            ServiceBusProcessorOptions options)
 534        {
 0535            ValidateEntityName(topicName);
 536
 0537            return new ServiceBusProcessor(
 0538                entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName),
 0539                connection: Connection,
 0540                isSessionEntity: false,
 0541                plugins: Plugins,
 0542                options: options);
 543        }
 544
 545        /// <summary>
 546        /// Creates a <see cref="ServiceBusSessionProcessor"/> instance that can be used to process session messages usi
 547        /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify
 548        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 549        /// </summary>
 550        ///
 551        /// <param name="queueName">The queue to create a <see cref="ServiceBusSessionProcessor"/> for.</param>
 552        /// <param name="options">The set of <see cref="ServiceBusProcessorOptions"/> to use for configuring the
 553        /// <see cref="ServiceBusSessionProcessor"/>.</param>
 554        /// <returns>A <see cref="ServiceBusSessionProcessor"/> scoped to the specified queue.</returns>
 555        public ServiceBusSessionProcessor CreateSessionProcessor(
 556            string queueName,
 557            ServiceBusSessionProcessorOptions options = default)
 558        {
 2559            ValidateEntityName(queueName);
 560
 2561            return new ServiceBusSessionProcessor(
 2562                entityPath: queueName,
 2563                connection: Connection,
 2564                plugins: Plugins,
 2565                options: options ?? new ServiceBusSessionProcessorOptions());
 566        }
 567
 568        /// <summary>
 569        /// Creates a <see cref="ServiceBusSessionProcessor"/> instance that can be used to process
 570        /// messages using event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify
 571        /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser
 572        /// </summary>
 573        ///
 574        /// <param name="topicName">The topic to create a <see cref="ServiceBusSessionProcessor"/> for.</param>
 575        /// <param name="subscriptionName">The subcription to create a <see cref="ServiceBusSessionProcessor"/> for.</pa
 576        /// <param name="options">The set of <see cref="ServiceBusSessionProcessor"/> to use for configuring the
 577        /// <see cref="ServiceBusSessionProcessor"/>.</param>
 578        ///
 579        /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified topic and subscription.</returns>
 580        public ServiceBusSessionProcessor CreateSessionProcessor(
 581            string topicName,
 582            string subscriptionName,
 583            ServiceBusSessionProcessorOptions options = default)
 584        {
 0585            ValidateEntityName(topicName);
 586
 0587            return new ServiceBusSessionProcessor(
 0588                entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName),
 0589                connection: Connection,
 0590                plugins: Plugins,
 0591                options: options ?? new ServiceBusSessionProcessorOptions());
 592        }
 593
 594        /// <summary>
 595        /// The <see cref="ServiceBusRuleManager"/> is used to manage the rules for a subscription.
 596        /// </summary>
 597        ///
 598        /// <param name="topicName">The topic to create a <see cref="ServiceBusRuleManager"/> for.</param>
 599        /// <param name="subscriptionName">The subscription specific to the specified topic to create
 600        /// a <see cref="ServiceBusRuleManager"/> for.</param>
 601        ///
 602        /// <returns>A <see cref="ServiceBusRuleManager"/> scoped to the specified subscription and topic.</returns>
 603        internal ServiceBusRuleManager CreateRuleManager(string topicName, string subscriptionName)
 604        {
 0605            ValidateEntityName(topicName);
 606
 0607            return new ServiceBusRuleManager(
 0608                connection: Connection,
 0609                subscriptionPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName));
 610        }
 611
 612        /// <summary>
 613        /// Validates that the specified entity name matches the entity path in the Connection,
 614        /// if an entity path is specified in the connection.
 615        /// </summary>
 616        ///
 617        /// <param name="entityName">Entity name to validate</param>
 618        private void ValidateEntityName(string entityName)
 619        {
 620            // The entity name may only be specified in one of the possible forms, either as part of the
 621            // connection string or as a stand-alone parameter, but not both.  If specified in both to the same
 622            // value, then do not consider this a failure.
 623
 22624            if (!string.IsNullOrEmpty(Connection.EntityPath) && !string.Equals(entityName, Connection.EntityPath, String
 625            {
 4626                throw new ArgumentException(Resources.OnlyOneEntityNameMayBeSpecified);
 627            }
 18628        }
 629
 630        /// <summary>
 631        /// Validates that the specified entity name matches the entity path in the Connection,
 632        /// if an entity path is specified in the connection.
 633        /// </summary>
 634        /// <param name="entityName">Entity name to validate</param>
 635        ///
 636        /// <param name="sendViaEntityName">The send via entity name to validate</param>
 637        private void ValidateSendViaEntityName(string entityName, string sendViaEntityName)
 638        {
 12639            if (sendViaEntityName == null ||
 12640                string.Equals(sendViaEntityName, entityName, StringComparison.InvariantCultureIgnoreCase))
 641            {
 8642                ValidateEntityName(sendViaEntityName);
 6643                return;
 644            }
 645
 646            // we've established they are not equal, so anything specified in connection string will cause
 647            // a mismatch with one of the entities.
 648
 4649            if (!string.IsNullOrEmpty(Connection.EntityPath))
 650            {
 2651                throw new ArgumentException(Resources.SendViaCannotBeUsedWithEntityInConnectionString);
 652            }
 2653        }
 654    }
 655}