internal SessionClient( string clientId, string entityPath, MessagingEntityType?entityType, ReceiveMode receiveMode, int prefetchCount, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy, IList <ServiceBusPlugin> registeredPlugins) : base(clientId, retryPolicy ?? RetryPolicy.Default) { this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = this.ServiceBusConnection.OperationTimeout; this.EntityPath = entityPath; this.EntityType = entityType; this.ReceiveMode = receiveMode; this.PrefetchCount = prefetchCount; this.CbsTokenProvider = cbsTokenProvider; // Register plugins on the message session. if (registeredPlugins != null) { foreach (var serviceBusPlugin in registeredPlugins) { this.RegisterPlugin(serviceBusPlugin); } } }
public Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout) { return(TaskHelpers.CreateTask( (c, s) => this.BeginSendToken( tokenProvider, namespaceAddress, audience, resource, requiredClaims, timeout, c, s), (a) => this.EndSendToken(a))); }
public ActiveClientLinkManager(string clientId, ICbsTokenProvider tokenProvider) { this.clientId = clientId; this.cbsTokenProvider = tokenProvider; this.sendReceiveLinkCBSTokenRenewalTimer = new Timer(OnRenewSendReceiveCBSToken, this, Timeout.Infinite, Timeout.Infinite); this.requestResponseLinkCBSTokenRenewalTimer = new Timer(OnRenewRequestResponseCBSToken, this, Timeout.Infinite, Timeout.Infinite); }
internal MessageReceiver( string entityPath, MessagingEntityType?entityType, ReceiveMode receiveMode, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy, int prefetchCount = DefaultPrefetchCount, string sessionId = null, bool isSessionReceiver = false) : base(nameof(MessageReceiver) + StringUtility.GetRandomString(), retryPolicy ?? RetryPolicy.Default) { this.ReceiveMode = receiveMode; this.OperationTimeout = serviceBusConnection.OperationTimeout; this.Path = entityPath; this.EntityType = entityType; this.ServiceBusConnection = serviceBusConnection; this.CbsTokenProvider = cbsTokenProvider; this.SessionId = sessionId; this.isSessionReceiver = isSessionReceiver; this.ReceiveLinkManager = new FaultTolerantAmqpObject <ReceivingAmqpLink>(this.CreateLinkAsync, this.CloseSession); this.RequestResponseLinkManager = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(this.CreateRequestResponseLinkAsync, this.CloseRequestResponseSession); this.requestResponseLockedMessages = new ConcurrentExpiringSet <Guid>(); this.PrefetchCount = prefetchCount; this.messageReceivePumpSyncLock = new object(); }
internal SessionClient( string clientTypeName, string entityPath, MessagingEntityType?entityType, ReceiveMode receiveMode, int prefetchCount, ServiceBusConnection serviceBusConnection, ITokenProvider tokenProvider, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy, IList <ServiceBusPlugin> registeredPlugins) : base(clientTypeName, entityPath, retryPolicy ?? RetryPolicy.Default) { this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.EntityPath = entityPath; this.EntityType = entityType; this.ReceiveMode = receiveMode; this.PrefetchCount = prefetchCount; tokenProvider = tokenProvider ?? this.ServiceBusConnection.CreateTokenProvider(); this.CbsTokenProvider = cbsTokenProvider ?? new TokenProviderAdapter(tokenProvider, this.ServiceBusConnection.OperationTimeout); this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); // Register plugins on the message session. if (registeredPlugins != null) { foreach (var serviceBusPlugin in registeredPlugins) { this.RegisterPlugin(serviceBusPlugin); } } }
public async Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, $"{nameof(SendTokenAsync)}"); } try { return(await _amqpCbsLink.SendTokenAsync(tokenProvider, namespaceAddress, audience, resource, requiredClaims, timeout).ConfigureAwait(false)); } catch (AmqpException e) when(!e.IsFatal()) { Exception ex = AmqpIoTExceptionAdapter.ConvertToIoTHubException(e); if (ReferenceEquals(e, ex)) { throw; } else { throw ex; } } finally { if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(SendTokenAsync)}"); } } }
protected AmqpLinkCreator(string entityPath, ServiceBusConnection serviceBusConnection, string[] requiredClaims, ICbsTokenProvider cbsTokenProvider, AmqpLinkSettings amqpLinkSettings) { this.entityPath = entityPath; this.serviceBusConnection = serviceBusConnection; this.requiredClaims = requiredClaims; this.cbsTokenProvider = cbsTokenProvider; this.amqpLinkSettings = amqpLinkSettings; }
public ActiveClientLinkManager(ClientEntity client, ICbsTokenProvider tokenProvider) { this.clientId = client.ClientId; this.retryPolicy = client.RetryPolicy ?? RetryPolicy.Default; this.cbsTokenProvider = tokenProvider; this.sendReceiveLinkCbsTokenRenewalTimer = new Timer(OnRenewSendReceiveCbsToken, this, Timeout.Infinite, Timeout.Infinite); this.requestResponseLinkCbsTokenRenewalTimer = new Timer(OnRenewRequestResponseCbsToken, this, Timeout.Infinite, Timeout.Infinite); }
protected AmqpLinkCreator(string entityPath, ServiceBusConnection serviceBusConnection, Uri endpointAddress, string[] requiredClaims, ICbsTokenProvider cbsTokenProvider, AmqpLinkSettings amqpLinkSettings, string clientId) { this.entityPath = entityPath; this.serviceBusConnection = serviceBusConnection; this.endpointAddress = endpointAddress; this.requiredClaims = requiredClaims; this.cbsTokenProvider = cbsTokenProvider; this.amqpLinkSettings = amqpLinkSettings; this.ClientId = clientId; }
public IAsyncResult BeginSendToken(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout, AsyncCallback callback, object state) { if (tokenProvider == null || namespaceAddress == null || audience == null || resource == null || requiredClaims == null) { throw Fx.Exception.ArgumentNull( tokenProvider == null ? "tokenProvider" : namespaceAddress == null ? "namespaceAddress" : audience == null ? "audience" : resource == null ? "resource" : "requiredClaims"); } return(new SendTokenAsyncResult(this, tokenProvider, namespaceAddress, audience, resource, requiredClaims, timeout, callback, state)); }
internal AmqpMessageReceiver( string entityName, MessagingEntityType?entityType, ReceiveMode mode, int prefetchCount, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy) : this(entityName, entityType, mode, prefetchCount, serviceBusConnection, cbsTokenProvider, null, retryPolicy) { }
public MessageSession( string entityPath, MessagingEntityType?entityType, ReceiveMode receiveMode, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy, int prefetchCount = Constants.DefaultClientPrefetchCount, string sessionId = null, bool isSessionReceiver = false) : base(entityPath, entityType, receiveMode, serviceBusConnection, cbsTokenProvider, retryPolicy, prefetchCount, sessionId, isSessionReceiver) { }
public AmqpSubscriptionClient( string path, ServiceBusConnection servicebusConnection, RetryPolicy retryPolicy, ICbsTokenProvider cbsTokenProvider, ReceiveMode mode = ReceiveMode.ReceiveAndDelete) { this.syncLock = new object(); this.Path = path; this.ServiceBusConnection = servicebusConnection; this.RetryPolicy = retryPolicy; this.CbsTokenProvider = cbsTokenProvider; this.ReceiveMode = mode; }
internal MessageSender( string entityPath, string transferDestinationPath, MessagingEntityType?entityType, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy) : base(nameof(MessageSender), entityPath, retryPolicy ?? RetryPolicy.Default) { MessagingEventSource.Log.MessageSenderCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath); if (string.IsNullOrWhiteSpace(entityPath)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(entityPath); } this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.Path = entityPath; this.SendingLinkDestination = entityPath; this.EntityType = entityType; this.ServiceBusConnection.ThrowIfClosed(); if (cbsTokenProvider != null) { this.CbsTokenProvider = cbsTokenProvider; } else if (this.ServiceBusConnection.TokenProvider != null) { this.CbsTokenProvider = new TokenProviderAdapter(this.ServiceBusConnection.TokenProvider, this.ServiceBusConnection.OperationTimeout); } else { throw new ArgumentNullException($"{nameof(ServiceBusConnection)} doesn't have a valid token provider"); } this.SendLinkManager = new FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateLinkAsync, CloseSession); this.RequestResponseLinkManager = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(this.CreateRequestResponseLinkAsync, CloseRequestResponseSession); this.clientLinkManager = new ActiveClientLinkManager(this, this.CbsTokenProvider); this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); if (!string.IsNullOrWhiteSpace(transferDestinationPath)) { this.isViaSender = true; this.TransferDestinationPath = transferDestinationPath; this.ViaEntityPath = entityPath; } MessagingEventSource.Log.MessageSenderCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.ClientId); }
public IAsyncResult BeginSendToken(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout, AsyncCallback callback, object state) { if (tokenProvider == null || namespaceAddress == null || audience == null || resource == null || requiredClaims == null) { throw new ArgumentNullException( tokenProvider == null ? "tokenProvider" : namespaceAddress == null ? "namespaceAddress" : audience == null ? "audience" : resource == null ? "resource" : "requiredClaims"); } if (this.connection.IsClosing()) { throw new ObjectDisposedException(CbsConstants.CbsAddress); } return(new SendTokenAsyncResult(this, tokenProvider, namespaceAddress, audience, resource, requiredClaims, timeout, callback, state)); }
internal SessionClient( string clientTypeName, string entityPath, MessagingEntityType?entityType, ReceiveMode receiveMode, int prefetchCount, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy, IList <ServiceBusPlugin> registeredPlugins) : base(clientTypeName, entityPath, retryPolicy ?? RetryPolicy.Default) { if (string.IsNullOrWhiteSpace(entityPath)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(entityPath); } this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.EntityPath = entityPath; this.EntityType = entityType; this.ReceiveMode = receiveMode; this.PrefetchCount = prefetchCount; this.ServiceBusConnection.ThrowIfClosed(); if (cbsTokenProvider != null) { this.CbsTokenProvider = cbsTokenProvider; } else if (this.ServiceBusConnection.TokenProvider != null) { this.CbsTokenProvider = new TokenProviderAdapter(this.ServiceBusConnection.TokenProvider, this.ServiceBusConnection.OperationTimeout); } else { throw new ArgumentNullException($"{nameof(ServiceBusConnection)} doesn't have a valid token provider"); } this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); // Register plugins on the message session. if (registeredPlugins != null) { foreach (var serviceBusPlugin in registeredPlugins) { this.RegisterPlugin(serviceBusPlugin); } } }
internal MessageSender( string entityPath, MessagingEntityType?entityType, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy) : base(nameof(MessageSender) + StringUtility.GetRandomString(), retryPolicy ?? RetryPolicy.Default) { this.OperationTimeout = serviceBusConnection.OperationTimeout; this.Path = entityPath; this.EntityType = entityType; this.ServiceBusConnection = serviceBusConnection; this.CbsTokenProvider = cbsTokenProvider; this.SendLinkManager = new FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateLinkAsync, this.CloseSession); this.RequestResponseLinkManager = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(this.CreateRequestResponseLinkAsync, this.CloseRequestResponseSession); }
public AmqpSessionClient( string clientId, string entityPath, MessagingEntityType entityType, ReceiveMode receiveMode, int prefetchCount, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy) { this.ClientId = clientId; this.EntityPath = entityPath; this.EntityType = entityType; this.ReceiveMode = receiveMode; this.PrefetchCount = prefetchCount; this.ServiceBusConnection = serviceBusConnection; this.CbsTokenProvider = cbsTokenProvider; this.RetryPolicy = retryPolicy; }
internal MessageSender( string entityPath, MessagingEntityType?entityType, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy) : base(ClientEntity.GenerateClientId(nameof(MessageSender), entityPath), retryPolicy ?? RetryPolicy.Default) { MessagingEventSource.Log.MessageSenderCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = serviceBusConnection.OperationTimeout; this.Path = entityPath; this.EntityType = entityType; this.CbsTokenProvider = cbsTokenProvider; this.SendLinkManager = new FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateLinkAsync, this.CloseSession); this.RequestResponseLinkManager = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(this.CreateRequestResponseLinkAsync, this.CloseRequestResponseSession); MessagingEventSource.Log.MessageSenderCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.ClientId); }
public SendTokenAsyncResult( AmqpCbsLink cbsLink, ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout, AsyncCallback callback, object state) : base(timeout, callback, state) { this.cbsLink = cbsLink; this.namespaceAddress = namespaceAddress; this.audience = audience; this.resource = resource; this.requiredClaims = requiredClaims; this.tokenProvider = tokenProvider; this.Start(); }
public async Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, $"{nameof(SendTokenAsync)}"); } try { return(await _amqpCbsLink.SendTokenAsync(tokenProvider, namespaceAddress, audience, resource, requiredClaims, timeout).ConfigureAwait(false)); } catch (AmqpException ex) { throw new IotHubCommunicationException("AmqpIoTCbsLink.SendTokenAsync error", ex.InnerException); } finally { if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(SendTokenAsync)}"); } } }
internal MessageSender( string entityPath, MessagingEntityType?entityType, ServiceBusConnection serviceBusConnection, ITokenProvider tokenProvider, ICbsTokenProvider cbsTokenProvider, RetryPolicy retryPolicy) : base(nameof(MessageSender), entityPath, retryPolicy ?? RetryPolicy.Default) { MessagingEventSource.Log.MessageSenderCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.Path = entityPath; this.EntityType = entityType; tokenProvider = tokenProvider ?? this.ServiceBusConnection.CreateTokenProvider(); this.CbsTokenProvider = cbsTokenProvider ?? new TokenProviderAdapter(tokenProvider, this.ServiceBusConnection.OperationTimeout); this.SendLinkManager = new FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateLinkAsync, CloseSession); this.RequestResponseLinkManager = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(this.CreateRequestResponseLinkAsync, CloseRequestResponseSession); this.clientLinkManager = new ActiveClientLinkManager(this, this.CbsTokenProvider); this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); MessagingEventSource.Log.MessageSenderCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.ClientId); }
internal AmqpMessageReceiver( string entityName, MessagingEntityType?entityType, ReceiveMode mode, int prefetchCount, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider, string sessionId, RetryPolicy retryPolicy, bool isSessionReceiver = false) : base(mode, serviceBusConnection.OperationTimeout, retryPolicy) { this.entityName = entityName; this.EntityType = entityType; this.ServiceBusConnection = serviceBusConnection; this.CbsTokenProvider = cbsTokenProvider; this.sessionId = sessionId; this.isSessionReceiver = isSessionReceiver; this.ReceiveLinkManager = new FaultTolerantAmqpObject <ReceivingAmqpLink>(this.CreateLinkAsync, this.CloseSession); this.RequestResponseLinkManager = new FaultTolerantAmqpObject <RequestResponseAmqpLink>(this.CreateRequestResponseLinkAsync, this.CloseRequestResponseSession); this.requestResponseLockedMessages = new ConcurrentExpiringSet <Guid>(); this.PrefetchCount = prefetchCount; }
public AmqpMessageReceiver(string entityName, MessagingEntityType entityType, ReceiveMode mode, int prefetchCount, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider) : this(entityName, entityType, mode, prefetchCount, serviceBusConnection, cbsTokenProvider, null) { }
async Task <ReceivingAmqpLink> CreateLinkAsync(TimeSpan timeout) { var amqpEventHubClient = ((AmqpEventHubClient)this.EventHubClient); var timeoutHelper = new TimeoutHelper(timeout); AmqpConnection connection = await amqpEventHubClient.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Authenticate over CBS var cbsLink = connection.Extensions.Find <AmqpCbsLink>(); ICbsTokenProvider cbsTokenProvider = amqpEventHubClient.CbsTokenProvider; Uri address = new Uri(amqpEventHubClient.ConnectionStringBuilder.Endpoint, this.Path); string audience = address.AbsoluteUri; string resource = address.AbsoluteUri; var expiresAt = await cbsLink.SendTokenAsync(cbsTokenProvider, address, audience, resource, new[] { ClaimConstants.Listen }, timeoutHelper.RemainingTime()).ConfigureAwait(false); AmqpSession session = null; try { // Create our Session var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; session = connection.CreateSession(sessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); FilterSet filterMap = null; var filters = this.CreateFilters(); if (filters != null && filters.Count > 0) { filterMap = new FilterSet(); foreach (var filter in filters) { filterMap.Add(filter.DescriptorName, filter); } } // Create our Link var linkSettings = new AmqpLinkSettings { Role = true, TotalLinkCredit = (uint)this.PrefetchCount, AutoSendFlow = this.PrefetchCount > 0 }; linkSettings.AddProperty(AmqpClientConstants.EntityTypeName, (int)MessagingEntityType.ConsumerGroup); linkSettings.Source = new Source { Address = address.AbsolutePath, FilterSet = filterMap }; linkSettings.Target = new Target { Address = this.ClientId }; linkSettings.SettleType = SettleMode.SettleOnSend; // Receiver metrics enabled? if (this.ReceiverRuntimeMetricEnabled) { linkSettings.DesiredCapabilities = new Multiple <AmqpSymbol>(new List <AmqpSymbol> { AmqpClientConstants.EnableReceiverRuntimeMetricName }); } if (this.Epoch.HasValue) { linkSettings.AddProperty(AmqpClientConstants.AttachEpoch, this.Epoch.Value); } if (!string.IsNullOrWhiteSpace(this.Identifier)) { linkSettings.AddProperty(AmqpClientConstants.ReceiverIdentifierName, this.Identifier); } var link = new ReceivingAmqpLink(linkSettings); linkSettings.LinkName = $"{amqpEventHubClient.ContainerId};{connection.Identifier}:{session.Identifier}:{link.Identifier}"; link.AttachTo(session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var activeClientLink = new ActiveClientLink( link, audience, // audience this.EventHubClient.ConnectionStringBuilder.Endpoint.AbsoluteUri, // endpointUri new[] { ClaimConstants.Listen }, true, expiresAt); this.clientLinkManager.SetActiveLink(activeClientLink); return(link); } catch { // Cleanup any session (and thus link) in case of exception. session?.SafeClose(); throw; } }
public AmqpSendReceiveLinkCreator(string entityPath, ServiceBusConnection serviceBusConnection, string[] requiredClaims, ICbsTokenProvider cbsTokenProvider, AmqpLinkSettings linkSettings) : base(entityPath, serviceBusConnection, requiredClaims, cbsTokenProvider, linkSettings) { }
/// <summary> /// Sends a security token for a resource for later access. /// </summary> /// <param name="tokenProvider">The provider for issuing security tokens.</param> /// <param name="namespaceAddress">The namespace (or tenant) name.</param> /// <param name="audience">The audience. In most cases it is the same as resource.</param> /// <param name="resource">The resource to access.</param> /// <param name="requiredClaims">The required claims to access the resource.</param> /// <param name="timeout">The operation timeout.</param> /// <returns></returns> public async Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout) { if (this.connection.IsClosing()) { throw new OperationCanceledException("Connection is closing or closed."); } CbsToken token = await tokenProvider.GetTokenAsync(namespaceAddress, resource, requiredClaims).ConfigureAwait(false); string tokenType = token.TokenType; if (tokenType == null) { throw new NotSupportedException(AmqpResources.AmqpUnsupportedTokenType); } RequestResponseAmqpLink requestResponseLink; if (!this.linkFactory.TryGetOpenedObject(out requestResponseLink)) { requestResponseLink = await this.linkFactory.GetOrCreateAsync(timeout).ConfigureAwait(false); } AmqpValue value = new AmqpValue(); value.Value = token.TokenValue; AmqpMessage putTokenRequest = AmqpMessage.Create(value); putTokenRequest.ApplicationProperties.Map[CbsConstants.Operation] = CbsConstants.PutToken.OperationValue; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Type] = tokenType; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Audience] = audience; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Expiration] = token.ExpiresAtUtc; AmqpMessage putTokenResponse = await requestResponseLink.RequestAsync(putTokenRequest, timeout).ConfigureAwait(false); int statusCode = (int)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusCode]; string statusDescription = (string)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusDescription]; if (statusCode == (int)AmqpResponseStatusCode.Accepted || statusCode == (int)AmqpResponseStatusCode.OK) { return(token.ExpiresAtUtc); } Exception exception; AmqpResponseStatusCode amqpResponseStatusCode = (AmqpResponseStatusCode)statusCode; switch (amqpResponseStatusCode) { case AmqpResponseStatusCode.BadRequest: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.NotFound: exception = new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Forbidden: exception = new AmqpException(AmqpErrorCode.TransferLimitExceeded, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Unauthorized: exception = new AmqpException(AmqpErrorCode.UnauthorizedAccess, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; default: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; } throw exception; }
private async Task <SendingAmqpLink> CreateLinkAsync(TimeSpan timeout) { var amqpEventHubClient = ((AmqpEventHubClient)EventHubClient); var timeoutHelper = new TimeoutHelper(timeout); AmqpConnection connection = await amqpEventHubClient.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Authenticate over CBS AmqpCbsLink cbsLink = connection.Extensions.Find <AmqpCbsLink>(); ICbsTokenProvider cbsTokenProvider = amqpEventHubClient.CbsTokenProvider; Uri address = new Uri(amqpEventHubClient.ConnectionStringBuilder.Endpoint, Path); string audience = address.AbsoluteUri; string resource = address.AbsoluteUri; DateTime expiresAt = await cbsLink.SendTokenAsync( cbsTokenProvider, address, audience, resource, new[] { ClaimConstants.Send }, timeoutHelper.RemainingTime()).ConfigureAwait(false); AmqpSession session = null; try { // Create our Session var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; session = connection.CreateSession(sessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Create our Link var linkSettings = new AmqpLinkSettings(); linkSettings.AddProperty(AmqpClientConstants.TimeoutName, (uint)timeoutHelper.RemainingTime().TotalMilliseconds); linkSettings.AddProperty(AmqpClientConstants.EntityTypeName, (int)MessagingEntityType.EventHub); linkSettings.Role = false; linkSettings.InitialDeliveryCount = 0; linkSettings.Target = new Target { Address = address.AbsolutePath }; linkSettings.Source = new Source { Address = ClientId }; var link = new SendingAmqpLink(linkSettings); linkSettings.LinkName = $"{amqpEventHubClient.ContainerId};{connection.Identifier}:{session.Identifier}:{link.Identifier}"; link.AttachTo(session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var activeClientLink = new ActiveClientLink( link, audience, // audience EventHubClient.ConnectionStringBuilder.Endpoint.AbsoluteUri, // endpointUri new[] { ClaimConstants.Send }, true, expiresAt); MaxMessageSize = (long)activeClientLink.Link.Settings.MaxMessageSize(); _clientLinkManager.SetActiveLink(activeClientLink); _linkCreated = true; return(link); } catch { // Cleanup any session (and thus link) in case of exception. session?.Abort(); throw; } }
public AmqpRequestResponseLinkCreator(string entityPath, ServiceBusConnection serviceBusConnection, Uri endpointAddress, string[] audience, string[] requiredClaims, ICbsTokenProvider cbsTokenProvider, AmqpLinkSettings linkSettings, string clientId) : base(entityPath, serviceBusConnection, endpointAddress, audience, requiredClaims, cbsTokenProvider, linkSettings, clientId) { this.entityPath = entityPath; }
internal AmqpMessageSender(string entityName, MessagingEntityType entityType, ServiceBusConnection serviceBusConnection, ICbsTokenProvider cbsTokenProvider) : base(serviceBusConnection.OperationTimeout) { this.Path = entityName; this.EntityType = entityType; this.ServiceBusConnection = serviceBusConnection; this.CbsTokenProvider = cbsTokenProvider; this.SendLinkManager = new FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateLinkAsync, this.CloseSession); }