/// <summary> /// Creates a new SessionClient from a specified connection string and entity path. /// </summary> /// <param name="connectionString">Namespace connection string used to communicate with Service Bus. Must not contain entity details.</param> /// <param name="entityPath">The path of the entity for this receiver. For Queues this will be the anme, but for Subscriptions this will be the full path.</param> /// <param name="receiveMode">The <see cref="ReceiveMode"/> used to specify how messages are received. Defaults to PeekLock mode.</param> /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with ServiceBus. Defaults to <see cref="RetryPolicy.Default"/></param> /// <param name="prefetchCount">The <see cref="PrefetchCount"/> that specifies the upper limit of messages the session object /// will actively receive regardless of whether a receive operation is pending. Defaults to 0.</param> /// <remarks>Creates a new connection to the entity, which is used for all the sessions objects accepted using this client.</remarks> public SessionClient( string connectionString, string entityPath, ReceiveMode receiveMode = ReceiveMode.PeekLock, RetryPolicy retryPolicy = null, int prefetchCount = DefaultPrefetchCount) : this(ClientEntity.GenerateClientId(nameof(SessionClient), entityPath), entityPath, null, receiveMode, prefetchCount, new ServiceBusNamespaceConnection(connectionString), null, retryPolicy, null) { if (string.IsNullOrWhiteSpace(connectionString)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(connectionString); } if (string.IsNullOrWhiteSpace(entityPath)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(entityPath); } this.ownsConnection = true; var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(this.ServiceBusConnection.SasKeyName, this.ServiceBusConnection.SasKey); this.CbsTokenProvider = new TokenProviderAdapter(tokenProvider, this.ServiceBusConnection.OperationTimeout); }
/// <summary> /// Gets a particular session object identified by <paramref name="sessionId"/> that can be used to receive messages for that sessionId. /// </summary> /// <param name="sessionId">The sessionId present in all its messages.</param> /// <param name="serverWaitTime">Amount of time for which the call should wait to fetch the next session.</param> /// <remarks>All plugins registered on <see cref="SessionClient"/> will be applied to each <see cref="MessageSession"/> that is accepted. /// Individual sessions can further register additional plugins.</remarks> /// <returns>A session object.</returns> public async Task <IMessageSession> AcceptMessageSessionAsync(string sessionId, TimeSpan serverWaitTime) { MessagingEventSource.Log.AmqpSessionClientAcceptMessageSessionStart( this.ClientId, this.EntityPath, this.ReceiveMode, this.PrefetchCount, sessionId); var session = new MessageSession( this.EntityPath, this.EntityType, this.ReceiveMode, this.ServiceBusConnection, this.CbsTokenProvider, this.RetryPolicy, this.PrefetchCount, sessionId, true); try { await this.RetryPolicy.RunOperation( async() => { await session.GetSessionReceiverLinkAsync(serverWaitTime).ConfigureAwait(false); }, serverWaitTime) .ConfigureAwait(false); } catch (Exception exception) { MessagingEventSource.Log.AmqpSessionClientAcceptMessageSessionException( this.ClientId, this.EntityPath, exception); await session.CloseAsync().ConfigureAwait(false); throw AmqpExceptionHelper.GetClientException(exception); } MessagingEventSource.Log.AmqpSessionClientAcceptMessageSessionStop( this.ClientId, this.EntityPath, session.SessionIdInternal); session.UpdateClientId(ClientEntity.GenerateClientId(nameof(MessageSession), $"{this.EntityPath}_{session.SessionId}")); // Register plugins on the message session. foreach (var serviceBusPlugin in this.RegisteredPlugins) { session.RegisterPlugin(serviceBusPlugin); } return(session); }
TopicClient(ServiceBusNamespaceConnection serviceBusConnection, string entityPath, RetryPolicy retryPolicy) : base(ClientEntity.GenerateClientId(nameof(TopicClient), entityPath), retryPolicy) { MessagingEventSource.Log.TopicClientCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = this.ServiceBusConnection.OperationTimeout; this.syncLock = new object(); this.TopicName = entityPath; this.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( serviceBusConnection.SasKeyName, serviceBusConnection.SasKey); this.CbsTokenProvider = new TokenProviderAdapter(this.TokenProvider, serviceBusConnection.OperationTimeout); MessagingEventSource.Log.TopicClientCreateStop(serviceBusConnection?.Endpoint.Authority, entityPath, this.ClientId); }
SubscriptionClient(ServiceBusNamespaceConnection serviceBusConnection, string topicPath, string subscriptionName, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(ClientEntity.GenerateClientId(nameof(SubscriptionClient), $"{topicPath}/{subscriptionName}"), retryPolicy) { MessagingEventSource.Log.SubscriptionClientCreateStart(serviceBusConnection?.Endpoint.Authority, topicPath, subscriptionName, receiveMode.ToString()); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = this.ServiceBusConnection.OperationTimeout; this.syncLock = new object(); this.TopicPath = topicPath; this.SubscriptionName = subscriptionName; this.Path = EntityNameHelper.FormatSubscriptionPath(this.TopicPath, this.SubscriptionName); this.ReceiveMode = receiveMode; this.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( serviceBusConnection.SasKeyName, serviceBusConnection.SasKey); this.CbsTokenProvider = new TokenProviderAdapter(this.TokenProvider, serviceBusConnection.OperationTimeout); MessagingEventSource.Log.SubscriptionClientCreateStop(serviceBusConnection.Endpoint.Authority, topicPath, subscriptionName, this.ClientId); }