internal static async Task <SendingAmqpLink> OpenSendingAmqpLinkAsync( DeviceIdentity deviceIdentity, AmqpSession amqpSession, byte?senderSettleMode, byte?receiverSettleMode, string deviceTemplate, string moduleTemplate, string linkSuffix, string CorrelationId, TimeSpan timeout ) { if (Logging.IsEnabled) { Logging.Enter(typeof(AmqpLinkHelper), deviceIdentity, $"{nameof(OpenSendingAmqpLinkAsync)}"); } AmqpLinkSettings amqpLinkSettings = new AmqpLinkSettings { LinkName = CommonResources.GetNewStringGuid(linkSuffix), Role = false, InitialDeliveryCount = 0, Target = new Target() { Address = BuildLinkAddress(deviceIdentity, deviceTemplate, moduleTemplate) }, Source = new Source() { Address = deviceIdentity.IotHubConnectionString.DeviceId } }; amqpLinkSettings.SndSettleMode = senderSettleMode; amqpLinkSettings.RcvSettleMode = receiverSettleMode; amqpLinkSettings.AddProperty(IotHubAmqpProperty.TimeoutName, timeout.TotalMilliseconds); amqpLinkSettings.AddProperty(IotHubAmqpProperty.ClientVersion, deviceIdentity.ProductInfo.ToString()); amqpLinkSettings.AddProperty(IotHubAmqpProperty.ApiVersion, ClientApiVersionHelper.ApiVersionString); if (CorrelationId != null) { amqpLinkSettings.AddProperty(IotHubAmqpProperty.ChannelCorrelationId, CorrelationId); } SendingAmqpLink sendingLink = new SendingAmqpLink(amqpLinkSettings); sendingLink.AttachTo(amqpSession); await sendingLink.OpenAsync(timeout).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(typeof(AmqpLinkHelper), deviceIdentity, $"{nameof(OpenSendingAmqpLinkAsync)}"); } return(sendingLink); }
async Task RunClientAsync(string address) { AmqpConnectionFactory factory = new AmqpConnectionFactory(); factory.Settings.TransportProviders.Add( new TlsTransportProvider( new TlsTransportSettings() { CertificateValidationCallback = (a, b, c, d) => true }, AmqpVersion.V100)); AmqpConnection connection = await factory.OpenConnectionAsync(address); AmqpSession session = connection.CreateSession(new AmqpSessionSettings()); await session.OpenAsync(TimeSpan.FromSeconds(20)); SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend)); await sLink.OpenAsync(TimeSpan.FromSeconds(20)); AmqpMessage message = AmqpMessage.Create(new AmqpValue() { Value = "AmqpConnectionFactoryTest" }); Outcome outcome = await sLink.SendMessageAsync(message, EmptyBinary, NullBinary, TimeSpan.FromSeconds(10)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnDispose, 10)); await rLink.OpenAsync(TimeSpan.FromSeconds(20)); var receivedMessage = await rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); Assert.NotNull(receivedMessage); outcome = await rLink.DisposeMessageAsync(receivedMessage.DeliveryTag, new Accepted(), false, TimeSpan.FromSeconds(20)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); await connection.CloseAsync(TimeSpan.FromSeconds(20)); }
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; } }
private static async Task <AmqpIoTSendingLink> OpenSendingAmqpLinkAsync( DeviceIdentity deviceIdentity, AmqpSession amqpSession, byte?senderSettleMode, byte?receiverSettleMode, string deviceTemplate, string moduleTemplate, string linkSuffix, string CorrelationId, TimeSpan timeout ) { if (Logging.IsEnabled) { Logging.Enter(typeof(AmqpIoTSession), deviceIdentity, $"{nameof(OpenSendingAmqpLinkAsync)}"); } AmqpLinkSettings amqpLinkSettings = new AmqpLinkSettings { LinkName = linkSuffix, Role = false, InitialDeliveryCount = 0, Target = new Target() { Address = BuildLinkAddress(deviceIdentity, deviceTemplate, moduleTemplate) }, Source = new Source() { Address = deviceIdentity.IotHubConnectionString.DeviceId } }; amqpLinkSettings.SndSettleMode = senderSettleMode; amqpLinkSettings.RcvSettleMode = receiverSettleMode; amqpLinkSettings.AddProperty(AmqpIoTErrorAdapter.TimeoutName, timeout.TotalMilliseconds); amqpLinkSettings.AddProperty(AmqpIoTErrorAdapter.ClientVersion, deviceIdentity.ProductInfo.ToString()); amqpLinkSettings.AddProperty(AmqpIoTErrorAdapter.ApiVersion, ClientApiVersionHelper.ApiVersionString); if (CorrelationId != null) { amqpLinkSettings.AddProperty(AmqpIoTErrorAdapter.ChannelCorrelationId, CorrelationId); } if (!deviceIdentity.AmqpTransportSettings.AuthenticationChain.IsNullOrWhiteSpace()) { amqpLinkSettings.AddProperty(AmqpIoTErrorAdapter.AuthChain, deviceIdentity.AmqpTransportSettings.AuthenticationChain); } try { SendingAmqpLink sendingLink = new SendingAmqpLink(amqpLinkSettings); sendingLink.AttachTo(amqpSession); await sendingLink.OpenAsync(timeout).ConfigureAwait(false); return(new AmqpIoTSendingLink(sendingLink)); } catch (Exception e) when(!e.IsFatal()) { Exception ex = AmqpIoTExceptionAdapter.ConvertToIoTHubException(e, amqpSession); if (ReferenceEquals(e, ex)) { throw; } else { if (ex is AmqpIoTResourceException) { amqpSession.SafeClose(); throw new IotHubCommunicationException(ex.Message, ex); } throw ex; } } finally { if (Logging.IsEnabled) { Logging.Exit(typeof(AmqpIoTSession), deviceIdentity, $"{nameof(OpenSendingAmqpLinkAsync)}"); } } }
private static async Task <AmqpIotSendingLink> OpenSendingAmqpLinkAsync( DeviceIdentity deviceIdentity, AmqpSession amqpSession, byte?senderSettleMode, byte?receiverSettleMode, string deviceTemplate, string moduleTemplate, string linkSuffix, string correlationId, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(typeof(AmqpIotSession), deviceIdentity, nameof(OpenSendingAmqpLinkAsync)); } AmqpLinkSettings amqpLinkSettings = new AmqpLinkSettings { LinkName = linkSuffix, Role = false, InitialDeliveryCount = 0, Target = new Target { Address = BuildLinkAddress(deviceIdentity, deviceTemplate, moduleTemplate) }, Source = new Source { Address = deviceIdentity.IotHubConnectionString.DeviceId }, SndSettleMode = senderSettleMode, RcvSettleMode = receiverSettleMode, }; amqpLinkSettings.AddProperty(AmqpIotErrorAdapter.TimeoutName, timeout.TotalMilliseconds); amqpLinkSettings.AddProperty(AmqpIotConstants.ClientVersion, deviceIdentity.ProductInfo.ToString()); if (correlationId != null) { amqpLinkSettings.AddProperty(AmqpIotConstants.ChannelCorrelationId, correlationId); } if (!deviceIdentity.AmqpTransportSettings.AuthenticationChain.IsNullOrWhiteSpace()) { amqpLinkSettings.AddProperty(AmqpIotConstants.AuthChain, deviceIdentity.AmqpTransportSettings.AuthenticationChain); } // This check is added to enable the device or module client to available plug and play features. For devices or modules that pass in the model Id, // the SDK will enable plug and play features by setting the modelId to Amqp link settings. if (!string.IsNullOrWhiteSpace(deviceIdentity.Options?.ModelId)) { amqpLinkSettings.AddProperty(AmqpIotConstants.ModelId, deviceIdentity.Options.ModelId); } amqpLinkSettings.AddProperty(AmqpIotConstants.ApiVersion, ClientApiVersionHelper.ApiVersionString); try { var sendingLink = new SendingAmqpLink(amqpLinkSettings); sendingLink.AttachTo(amqpSession); await sendingLink.OpenAsync(timeout).ConfigureAwait(false); return(new AmqpIotSendingLink(sendingLink)); } catch (Exception e) when(!e.IsFatal()) { Exception ex = AmqpIotExceptionAdapter.ConvertToIotHubException(e, amqpSession); if (ReferenceEquals(e, ex)) { throw; } else { if (ex is AmqpIotResourceException) { amqpSession.SafeClose(); throw new IotHubCommunicationException(ex.Message, ex); } throw ex; } } finally { if (Logging.IsEnabled) { Logging.Exit(typeof(AmqpIotSession), deviceIdentity, nameof(OpenSendingAmqpLinkAsync)); } } }