/// <summary> /// create new QueueClient /// </summary> /// <param name="queueName">Name of the queue</param> /// <returns>QueueClient for given queue</returns> public QueueClient createQueueClientforQueue(string queueName) { if (messageFactory == null) { messageFactory = MessagingFactory.Create(connBuilder.GetAbsoluteRuntimeEndpoints(), localUserTokenProvider); } return(messageFactory.CreateQueueClient(queueName)); }
public async Task Run(string connectionString) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); var listenAddress = new UriBuilder(connectionStringBuilder.GetAbsoluteRuntimeEndpoints()[0]) { Path = connectionStringBuilder.EntityPath ?? "relay" }.ToString(); TokenProvider tokenProvider = null; if (connectionStringBuilder.SharedAccessKeyName != null && connectionStringBuilder.SharedAccessKey != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey); } else if (connectionStringBuilder.SharedAccessSignature != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessSignature); } else if (connectionStringBuilder.SharedSecretIssuerName != null && connectionStringBuilder.SharedSecretIssuerSecret != null) { tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret); } if (tokenProvider != null) { using (var host = new ServiceHost(this)) { host.AddServiceEndpoint( GetType(), new NetTcpRelayBinding { IsDynamic = true }, listenAddress) .EndpointBehaviors.Add( new TransportClientEndpointBehavior(tokenProvider)); host.Open(); Console.WriteLine("Service listening at address {0}", listenAddress); Console.WriteLine("Press [Enter] to close the listener and exit."); Console.ReadLine(); host.Close(); } } }
public async Task Run(string connectionString) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); var sendAddress = new UriBuilder(connectionStringBuilder.GetAbsoluteRuntimeEndpoints()[0]) { Path = connectionStringBuilder.EntityPath ?? "relay" }.ToString(); TokenProvider tokenProvider = null; if (connectionStringBuilder.SharedAccessKeyName != null && connectionStringBuilder.SharedAccessKey != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey); } else if (connectionStringBuilder.SharedAccessSignature != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessSignature); } else if (connectionStringBuilder.SharedSecretIssuerName != null && connectionStringBuilder.SharedSecretIssuerSecret != null) { tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret); } if (tokenProvider != null) { var cf = new ChannelFactory <IClient>( new NetTcpRelayBinding { IsDynamic = true }, sendAddress); cf.Endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior(tokenProvider)); using (var client = cf.CreateChannel()) { for (int i = 1; i <= 25; i++) { var result = await client.Echo(DateTime.UtcNow.ToString()); Console.WriteLine("Round {0}, Echo: {1}", i, result); } client.Close(); } } Console.WriteLine("Press [Enter] to exit."); Console.ReadLine(); }
public async Task Run(string connectionString) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); var listenAddress = new UriBuilder(connectionStringBuilder.GetAbsoluteRuntimeEndpoints()[0]) { Path = connectionStringBuilder.EntityPath ?? "relay" }.ToString(); TokenProvider tokenProvider = null; if (connectionStringBuilder.SharedAccessKeyName != null && connectionStringBuilder.SharedAccessKey != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey); } else if (connectionStringBuilder.SharedAccessSignature != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessSignature); } else if (connectionStringBuilder.SharedSecretIssuerName != null && connectionStringBuilder.SharedSecretIssuerSecret != null) { tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret); } if (tokenProvider != null) { using (var host = new ServiceHost(this)) { host.AddServiceEndpoint( GetType(), new NetTcpRelayBinding {IsDynamic = true}, listenAddress) .EndpointBehaviors.Add( new TransportClientEndpointBehavior(tokenProvider)); host.Open(); Console.WriteLine("Service listening at address {0}", listenAddress); Console.WriteLine("Press [Enter] to close the listener and exit."); Console.ReadLine(); host.Close(); } } }
/// <summary> /// Create new client for servicebus connection. This method is slow! /// </summary> /// <param name="connectionString">Connection string</param> /// <param name="operationTimeoutForClients">Operation timeout for clients</param> /// <returns>Object that can handle messaging to the service bus</returns> internal static MessagingFactory CreateMessagingFactoryWithTimeout(string connectionString, TimeSpan operationTimeoutForClients) { var connBuilder = new ServiceBusConnectionStringBuilder(connectionString); var factorySettings = new MessagingFactorySettings { OperationTimeout = operationTimeoutForClients }; if (connBuilder.SharedAccessKey != null) { factorySettings.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connBuilder.SharedAccessKeyName, connBuilder.SharedAccessKey); } else if (connBuilder.SharedSecretIssuerName != null) { factorySettings.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connBuilder.SharedSecretIssuerName, connBuilder.SharedSecretIssuerSecret); } else if (connBuilder.OAuthUsername != null) { if (!string.IsNullOrEmpty(connBuilder.OAuthDomain)) { TokenProvider.CreateOAuthTokenProvider(connBuilder.StsEndpoints, new NetworkCredential(connBuilder.OAuthUsername, connBuilder.OAuthPassword, connBuilder.OAuthDomain)); } else { TokenProvider.CreateOAuthTokenProvider(connBuilder.StsEndpoints, new NetworkCredential(connBuilder.OAuthUsername, connBuilder.OAuthPassword)); } } else if (connBuilder.StsEndpoints.Count > 0) { factorySettings.TokenProvider = TokenProvider.CreateWindowsTokenProvider(connBuilder.StsEndpoints); } factorySettings.EnableAdditionalClientTimeout = true; MessagingFactory messageFactory = MessagingFactory.Create(connBuilder.GetAbsoluteRuntimeEndpoints(), factorySettings); messageFactory.RetryPolicy = RetryPolicy.Default; return(messageFactory); }
public async Task Run(string connectionString) { var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); var sendAddress = new UriBuilder(connectionStringBuilder.GetAbsoluteRuntimeEndpoints()[0]) { Path = connectionStringBuilder.EntityPath ?? "relay" }.ToString(); TokenProvider tokenProvider = null; if (connectionStringBuilder.SharedAccessKeyName != null && connectionStringBuilder.SharedAccessKey != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey); } else if (connectionStringBuilder.SharedAccessSignature != null) { tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessSignature); } else if (connectionStringBuilder.SharedSecretIssuerName != null && connectionStringBuilder.SharedSecretIssuerSecret != null) { tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(connectionStringBuilder.SharedSecretIssuerName, connectionStringBuilder.SharedSecretIssuerSecret); } if (tokenProvider != null) { var cf = new ChannelFactory<IClient>( new NetTcpRelayBinding {IsDynamic = true}, sendAddress); cf.Endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior(tokenProvider)); using (var client = cf.CreateChannel()) { for (int i = 1; i <= 25; i++) { var result = await client.Echo(DateTime.UtcNow.ToString()); Console.WriteLine("Round {0}, Echo: {1}", i, result); } client.Close(); } } Console.WriteLine("Press [Enter] to exit."); Console.ReadLine(); }
public async Task Run(string connectionString) { try { // Send messages to queue which does not require session Console.Title = "Client"; var sbb = new ServiceBusConnectionStringBuilder(connectionString); // Create sender to Order Service using (var factory = new ChannelFactory <IOnewayServiceChannel>("client")) { factory.Endpoint.Address = new EndpointAddress( new Uri(sbb.GetAbsoluteRuntimeEndpoints()[0], BasicQueueName)); factory.Endpoint.EndpointBehaviors.Add( new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sbb.SharedAccessKeyName, sbb.SharedAccessKey))); using (var clientChannel = factory.CreateChannel()) { int numberOfMessages = random.Next(10, 30); Console.WriteLine("Sending {0} messages to {1}...", numberOfMessages, BasicQueueName); // Send messages to queue for (var i = 0; i < numberOfMessages; i++) { var data = Guid.NewGuid().ToString(); clientChannel.Process(data); Console.WriteLine("{0}: Message [{1}].", "Send", data); } // Close sender clientChannel.Close(); } factory.Close(); } } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception); throw; } Console.WriteLine("\nSender complete."); }
public INamespaceManager Create(string @namespace) { var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); var connectionString = @namespace; if (!ConnectionString.IsConnectionString(connectionString)) { connectionString = namespacesDefinition.GetConnectionString(connectionString); } NamespaceManager manager; if (settingsFactory != null) { var s = settingsFactory(connectionString); var builder = new ServiceBusConnectionStringBuilder(connectionString); manager = new NamespaceManager(builder.GetAbsoluteRuntimeEndpoints(), s); } else { if (tokenProviderFactory != null) { var s = new NamespaceManagerSettings() { TokenProvider = tokenProviderFactory(connectionString) }; var builder = new ServiceBusConnectionStringBuilder(connectionString); manager = new NamespaceManager(builder.GetAbsoluteRuntimeEndpoints(), s); } else { manager = NamespaceManager.CreateFromConnectionString(connectionString); } if (settings.HasExplicitValue(WellKnownConfigurationKeys.Connectivity.NamespaceManagers.RetryPolicy)) { manager.Settings.RetryPolicy = settings.Get <RetryPolicy>(WellKnownConfigurationKeys.Connectivity.NamespaceManagers.RetryPolicy); } } return(new NamespaceManagerAdapter(manager)); }
public INamespaceManagerInternal Create(string @namespace) { var connectionString = @namespace; if (!ConnectionStringInternal.IsConnectionString(connectionString)) { connectionString = namespacesDefinition.GetConnectionString(connectionString); } NamespaceManager manager; if (settingsFactory != null) { var s = settingsFactory(connectionString); var builder = new ServiceBusConnectionStringBuilder(connectionString); manager = new NamespaceManager(builder.GetAbsoluteRuntimeEndpoints(), s); } else { if (tokenProviderFactory != null) { var s = new NamespaceManagerSettings { TokenProvider = tokenProviderFactory(connectionString) }; var builder = new ServiceBusConnectionStringBuilder(connectionString); manager = new NamespaceManager(builder.GetAbsoluteRuntimeEndpoints(), s); } else { manager = NamespaceManager.CreateFromConnectionString(connectionString); } if (retryPolicy != null) { manager.Settings.RetryPolicy = retryPolicy; } } return(new NamespaceManagerAdapterInternal(manager, connectionString)); }
public async Task Run(string connectionString) { var sbb = new ServiceBusConnectionStringBuilder(connectionString); try { // Create sender to Sequence Service using (var sendChannelFactory = new ChannelFactory <ISequenceServiceChannel>("sequenceSendClient")) { sendChannelFactory.Endpoint.Address = new EndpointAddress( new Uri(sbb.GetAbsoluteRuntimeEndpoints()[0], SessionQueueName)); sendChannelFactory.Endpoint.EndpointBehaviors.Add( new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sbb.SharedAccessKeyName, sbb.SharedAccessKey))); using (var clientChannel = sendChannelFactory.CreateChannel()) { for (int j = 0; j < 3; j++) { var contextId = Guid.NewGuid().ToString(); // Send messages var sequenceLength = new Random().Next(5, 10); for (var i = 0; i < sequenceLength; i++) { // Generating a random sequence item var sequenceItem = new SequenceItem( string.Format("{0:00000}", new Random().Next(0, 10000)), new Random().Next(1, 100)); // set the operation context for the subsequent call, this MUST be a new context OperationContext.Current = new OperationContext(clientChannel) { OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty { SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5) } } } }; // Correlating ServiceBus SessionId to ContextId await clientChannel.SubmitSequenceItemAsync(sequenceItem); Console.WriteLine("Sequence: {0} [{1}] - ContextId {2}.", sequenceItem.ItemId, sequenceItem.Quantity, contextId); } // set the operation context for the subsequent call, this MUST be a new context OperationContext.Current = new OperationContext(clientChannel) { OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty { SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5) } } } }; await clientChannel.TerminateSequenceAsync(); } clientChannel.Close(); } // Close sender sendChannelFactory.Close(); } } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception); } }
public async Task Run(string connectionString) { var sbb = new ServiceBusConnectionStringBuilder(connectionString); // Create MessageReceiver for queue which requires session Console.WriteLine("Ready to receive messages from {0}...", SessionQueueName); // Creating the service host object as defined in config using (var serviceHost = new ServiceHost(typeof(SequenceProcessingService), new Uri(sbb.GetAbsoluteRuntimeEndpoints()[0], SessionQueueName))) { var authBehavior = new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sbb.SharedAccessKeyName, sbb.SharedAccessKey)); serviceHost.Description.Behaviors.Add(new ErrorServiceBehavior()); foreach (var ep in serviceHost.Description.Endpoints) { ep.EndpointBehaviors.Add(authBehavior); } // Subscribe to the faulted event. serviceHost.Faulted += serviceHost_Faulted; // Start service serviceHost.Open(); Console.WriteLine("\nPress [Enter] to close ServiceHost."); await Task.WhenAny( Task.Run(() => Console.ReadKey()), Task.Delay(TimeSpan.FromSeconds(10)) ); // Close the service serviceHost.Close(); } }
public async Task Run(string connectionString) { try { var sbb = new ServiceBusConnectionStringBuilder(connectionString); Console.Title = "Service"; Console.WriteLine("Ready to receive messages from {0}...", BasicQueueName); // Creating the service host object as defined in config using (var serviceHost = new ServiceHost(typeof(OnewayService), new Uri(sbb.GetAbsoluteRuntimeEndpoints()[0], BasicQueueName))) { var authBehavior = new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sbb.SharedAccessKeyName, sbb.SharedAccessKey)); foreach (var ep in serviceHost.Description.Endpoints) { ep.EndpointBehaviors.Add(authBehavior); } // Subscribe to the faulted event. serviceHost.Faulted += serviceHost_Faulted; // Start service serviceHost.Open(); Console.WriteLine("\nPress [Enter] to Close the ServiceHost."); await Task.WhenAny( Task.Run(() => Console.ReadKey()), Task.Delay(TimeSpan.FromSeconds(60)) ); // Close the service serviceHost.Close(); } } catch (Exception exception) { Console.WriteLine("Exception occurred: {0}", exception); throw; } }