private static async Task ReceiveMessageFromAzureBus(ClientRequest request) { try { //new CreateConnection(request.ConnectionString, request.TopicName, request.SubscriptionName); new TopicReceiver().RegisterOnMessageHandlerAndReceiveMessages(null); await subscriptionClient.CloseAsync(); } catch (Exception ex) { throw; } }
public override async Task StopAsync(CancellationToken stoppingToken) { await _client.CloseAsync(); _logger.LogInformation( "Azure Service Bus connection was closed!"); }
static async Task MainAsync() { const int numberOfMessages = 10; topicClient = new TopicClient(ServiceBusConnectionString, TopicName); firstSubscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, FirstSubscriptionName); secondSubscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SecondSubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press any key to exit after receiving all the messages."); Console.WriteLine("======================================================"); // Register Subscription's MessageHandler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); // Send Messages await SendMessagesAsync(numberOfMessages); Console.ReadKey(); await firstSubscriptionClient.CloseAsync(); await secondSubscriptionClient.CloseAsync(); await topicClient.CloseAsync(); }
static async Task Main(string[] args) { var builder = new ConfigurationBuilder(); // tell the builder to look for the appsettings.json file builder .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddUserSecrets <Program>(); Configuration = builder.Build(); // Create a Subscription Client here subscriptionClient = new SubscriptionClient(Configuration["ConnectionString"], TopicName, SubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); // Register subscription message handler and receive messages in a loop RegisterMessageHandler(); Console.Read(); // Close the subscription here await subscriptionClient.CloseAsync(); }
static async Task MainAsync() { subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity. // Set it according to how many messages the application wants to process in parallel. MaxConcurrentCalls = 1, // Indicates whether MessagePump should automatically complete the messages after returning from User Callback. // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below. AutoComplete = false }; // Register the function that processes messages. subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions); Console.ReadKey(); await subscriptionClient.CloseAsync(); }
private static async Task MainAsync() { _subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); RegisterMessageHandler(); Console.ReadKey(); await _subscriptionClient.CloseAsync(); }
/// <summary> /// Closes the connection to the service bus and notifies the observers. /// </summary> /// <returns>Awaitable result of CloseAsync.</returns> public async ValueTask DisposeAsync() { if (_subscriptionClient != null) { await _subscriptionClient.CloseAsync(); } }
public async void TryReconnect() { if (MessagesListedBySession.Count == 0 && _sessionsInitializedCount >= _concurrentSessions) { _sessionsInitializedCount = 0; try { await subscriptionClient.CloseAsync(); } catch (Exception ex) { if (!(logger is null)) { logger.LogWarning(ex.Message); } } subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); var sessionOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { AutoComplete = false, MaxConcurrentSessions = _concurrentSessions, MaxAutoRenewDuration = TimeSpan.FromSeconds(10) }; subscriptionClient.PrefetchCount = 250; subscriptionClient.RegisterSessionHandler(OnMessage, sessionOptions); } }
public async Task CloseAsync(CancellationToken cancellationToken) { if (_log.IsDebugEnabled) { _log.DebugFormat("Closing client: {0}", InputAddress); } try { if (_subscriptionClient != null && !_subscriptionClient.IsClosedOrClosing) { await _subscriptionClient.CloseAsync().ConfigureAwait(false); } if (_log.IsDebugEnabled) { _log.DebugFormat("Closed client: {0}", InputAddress); } } catch (Exception exception) { if (_log.IsWarnEnabled) { _log.Warn($"Exception closing the client: {InputAddress}", exception); } } }
protected override Task <NimbusMessage> Fetch(CancellationToken cancellationToken) { return(Task.Run(async() => { await _receiveSemaphore.WaitAsync(_pollInterval, cancellationToken); if (cancellationToken.IsCancellationRequested) { await _subscriptionClient.CloseAsync(); return null; } if (_messages.Count == 0) { return null; } var message = _messages.Take(); var nimbusMessage = await _brokeredMessageFactory.BuildNimbusMessage(message); nimbusMessage.Properties[MessagePropertyKeys.RedeliveryToSubscriptionName] = _subscriptionName; return nimbusMessage; }, cancellationToken).ConfigureAwaitFalse()); }
private async Task Stop() { if (_subscriptionClient != null && !_subscriptionClient.IsClosedOrClosing) { await _subscriptionClient.CloseAsync(); } }
public async ValueTask DisposeAsync() { await _subscriptionClient.CloseAsync().ConfigureAwait(false); _messageResult.TrySetCanceled(); Dispose(); }
public override async Task StopAsync(CancellationToken stoppingToken) { await _client.CloseAsync(); _logger.LogInformation( "Conexao com o Azure Service Bus fechada!"); }
static async Task <int> Main(string[] args) { Console.WriteLine("Starting..."); if (String.IsNullOrEmpty(ServiceBusConnectionString)) { Console.WriteLine($"Please set {ENV_CONN_STR} in your environment"); return(1); } if (String.IsNullOrEmpty(SubscriptionName)) { Console.WriteLine($"Please set {ENV_SUBSCRIPTION} in your environment"); return(1); } subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); // Register subscription message handler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); await subscriptionClient.CloseAsync(); return(0); }
static async Task MainAsync() { string ServiceBusConnectionString = "Endpoint=sb://licenseplatepublisher.servicebus.windows.net/;SharedAccessKeyName=ConsumeReads;SharedAccessKey=VNcJZVQAVMazTAfrssP6Irzlg/pKwbwfnOqMXqROtCQ="; string TopicName = "licenseplateread"; string SubscriptionName = "fbS8qDfztJHYbf2G"; subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); updateVoitures_Recherche(); ServiceBusConnectionString = "Endpoint=sb://licenseplatepublisher.servicebus.windows.net/;SharedAccessKeyName=listeneronly;SharedAccessKey=w+ifeMSBq1AQkedLCpMa8ut5c6bJzJxqHuX9Jx2XGOk="; TopicName = "wantedplatelistupdate"; SubscriptionName = "8wQLDabncqUFjdTk"; subscriptionClient_notif = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); //liste voitures recherches // Console.WriteLine($"Received message: UserInfo:{resp}"); // Register subscription message handler and receive messages in a loop. RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); await subscriptionClient.CloseAsync(); await subscriptionClient_notif.CloseAsync(); }
static async Task Main(string[] args) { if (args.Length != 3) { Console.WriteLine("Usage: dotnet Events.CreditCardProcessor.dll [Connection-String] [Topic-Name] [Subscription-Name]"); return; } var connectionString = args[0]; var topicName = args[1]; var subscriptionName = args[2]; _client = new SubscriptionClient(connectionString, topicName, subscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); // Register the queue message handler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); await _client.CloseAsync(); }
static async Task MainAsync() { topicClient = new TopicClient(ServiceBusConnectionString, TopicName); empCreateFilterSubscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, "EmployeeCreate"); empDeleteFilterSubscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, "EmployeeDelete"); // Receive messages from 'allMessagesSubscriptionName'. Should receive all 9 messages await ReceiveMsg(empCreateFilterSubscriptionClient); // Receive messages from 'sqlFilterOnlySubscriptionName'. Should receive all messages with Color = 'Red' i.e 3 messages await ReceiveMsg(empDeleteFilterSubscriptionClient); Console.WriteLine("========================================================="); Console.WriteLine("Completed Receiving all messages... Press any key to exit"); Console.WriteLine("========================================================="); Console.ReadKey(); await empCreateFilterSubscriptionClient.CloseAsync(); await empDeleteFilterSubscriptionClient.CloseAsync(); await topicClient.CloseAsync(); }
static void Main(string[] args) { subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); // Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc. var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { // Maximum number of concurrent calls to the callback // ProcessMessagesAsync(), set to 1 for simplicity. // Set it according to how many messages the application wants to // process in parallel. MaxConcurrentCalls = 1, // Indicates whether the message pump should automatically complete // the messages after returning from user callback. // False below indicates the user callback handles the complete // operation as in ProcessMessagesAsync(). AutoComplete = false }; // Register the function that processes messages. subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions); Console.ReadKey(); subscriptionClient.CloseAsync(); }
static void Main(string[] args) { string sbConnectionString = "Endpoint=sb://mobilerecharge.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=KVb9ubc9XaV0dT/1dMj/JGvVvUZ64U21IBI="; string sbTopic = "offers"; string sbSubscription = "akki5677"; try { subscriptionClient = new SubscriptionClient(sbConnectionString, sbTopic, sbSubscription); var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false }; subscriptionClient.RegisterMessageHandler(ReceiveMessagesAsync, messageHandlerOptions); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadKey(); subscriptionClient.CloseAsync(); } }
public async Task ReceiveMessageAsync(string topic = null, string subscription = null) { // Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc. var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity. // Set it according to how many messages the application wants to process in parallel. MaxConcurrentCalls = 1, // Indicates whether the message pump should automatically complete the messages after returning from user callback. // False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync(). AutoComplete = false }; if (string.IsNullOrEmpty(topic)) { // Register the message handler function _queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions); Console.Read(); await _queueClient.CloseAsync(); } else { _subscriptionClient = new SubscriptionClient(_config["serviceBus"], topic, subscription); _subscriptionClient.RegisterMessageHandler(ProcessTopicMessagesAsync, messageHandlerOptions); Console.ReadLine(); await _subscriptionClient.CloseAsync(); } }
public async void RunTaskLoop(CancellationToken cancellationToken) { var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 20, AutoComplete = false }; _subClient.RegisterMessageHandler(HandleMessageAsync, messageHandlerOptions); while (!cancellationToken.IsCancellationRequested) { try { if (DateTimeOffset.Now - _apiClient.LastLogin > TokenValidity) { await _apiClient.LoginAsync(); } await Task.Delay(1000, cancellationToken); } catch (OperationCanceledException) { break; } } await _subClient.CloseAsync(); }
private static async Task MainAsync() { const int numberOfMessages = 10; _topicClient = new TopicClient(ServiceBusConnectionString, TopicName); await SendMessagesAsync(numberOfMessages); _subscriptionClient1 = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); _subscriptionClient2 = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press any key to exit after receiving all the messages."); Console.WriteLine("======================================================"); RegisterOnMessageHandlerAndReceiveMessages1(); RegisterOnMessageHandlerAndReceiveMessages2(); Console.ReadKey(); await _subscriptionClient1.CloseAsync(); await _subscriptionClient2.CloseAsync(); await _topicClient.CloseAsync(); }
public static async Task Main(string[] args) { const int numberOfMessages = 10; topicClient = new TopicClient(ServiceBusConnectionString, TopicName); subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after sending all the messages."); Console.WriteLine("======================================================"); // Send messages. // await SendMessagesAsync(numberOfMessages); Console.ReadKey(); await topicClient.CloseAsync(); // Register subscription message handler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); await subscriptionClient.CloseAsync(); }
static void Main(string[] args) { string sbConnectionString = "Endpoint=sb://mobilerechargeservicebus.servicebus.windows.net/;SharedAccessKeyName=TopicAccessPolicy;SharedAccessKey=2c65KcZEHGlyzrzxgkuf8ku4gfv5FdugVbvP7Hz5yBc="; string sbTopic = "rechargetopic"; string sbSubscription = "jitu007"; try { subscriptionClient = new SubscriptionClient(sbConnectionString, sbTopic, sbSubscription); var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false }; subscriptionClient.RegisterMessageHandler(ReceiveMessagesAsync, messageHandlerOptions); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadKey(); subscriptionClient.CloseAsync(); } }
async static Task Main(string[] args) { _logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); _logger.Information("Testando o consumo de mensagens com Azure Service Bus"); _logger.Information("Carregando configurações..."); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile($"appsettings.json"); var configuration = builder.Build(); string nomeTopic = configuration["AzureServiceBus:Topic"]; string subscription = configuration["AzureServiceBus:Subscription"]; _subscriptionClient = new SubscriptionClient( configuration["AzureServiceBus:ConnectionString"], nomeTopic, subscription); _logger.Information($"Topic = {nomeTopic}"); _logger.Information($"Subscription = {nomeTopic}"); _conexaoRedis = ConnectionMultiplexer.Connect(configuration["BaseCotacoes"]); _logger.Information("Aguardando mensagens..."); _logger.Information("Pressione Enter para encerrar"); RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadLine(); await _subscriptionClient.CloseAsync(); _logger.Warning("Encerrando o processamento de mensagens!"); }
static async Task Main(string[] args) { if (UseQueue) { queueClient = new QueueClient(ServiceBusConnectionString, QueueName) { PrefetchCount = 0 }; } else { subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName) { PrefetchCount = 0 }; } string type = UseQueue ? "QUEUE" : "SUBSCRIPTION"; Console.WriteLine("======================================================"); Console.WriteLine($"LISTENING ({type}) . Press ENTER key to exit"); Console.WriteLine("======================================================"); RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); if (UseQueue) { await queueClient.CloseAsync(); } else { await subscriptionClient.CloseAsync(); } }
public async Task Subscribe() { try { _subscriptionMesssage = GetSubscriptions(); if (_subscriptionMesssage != null) { _subscriptionClient = new SubscriptionClient(_subscriptionMesssage.PrimaryConnectionString , _subscriptionMesssage.TopicName, _subscriptionMesssage.SubscriptionName); // Console.WriteLine("======================================================"); // Console.WriteLine("Press ENTER key to exit after receiving all the messages."); // Console.WriteLine("======================================================"); // Register subscription message handler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); } } catch (Exception ex) { _logger.LogError("Error - Subscription {Details}", ex); } finally { Console.ReadKey(); await _subscriptionClient.CloseAsync(); } }
static async Task MainAsync() { try { subscriptionClient = new SubscriptionClient(serviceBusConnectionString, topicName, subscriptionName); //var builder = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); ////subscriptionClient = new SubscriptionClient(serviceBusConnectionString, topicName, subscriptionClient); //subscriptionClient = new SubscriptionClient(builder, subscriptionName); //subscriptionClient = new SubscriptionClient() Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); // Register subscription message handler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); await subscriptionClient.CloseAsync(); } catch (Exception ex) { int p = 90; } }
static async Task MainAsync() { const int numberOfMessages = 10; topicClient = new TopicClient(ServiceBusConnectionString, TopicName); subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); // Ensure default rule exists await subscriptionClient.RemoveRuleAsync(RuleDescription.DefaultRuleName); await subscriptionClient.AddRuleAsync(new RuleDescription(RuleDescription.DefaultRuleName, new TrueFilter())); Console.WriteLine("======================================================"); Console.WriteLine("Press any key to exit after receiving all the messages."); Console.WriteLine("======================================================"); // Register Subscription's MessageHandler and receive messages in a loop RegisterOnMessageHandlerAndReceiveMessages(); // Send Messages await SendMessagesAsync(numberOfMessages); Console.ReadKey(); await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); }
public async void TryReconnect() { if (MessagesListedByGroup.Count == 0 && _sessionsInitializedCount >= _concurrentSessions) { _sessionsInitializedCount = 0; _processedSessionsDictionary = new ConcurrentDictionary <string, short>(); try { await subscriptionClient.CloseAsync(); } catch (Exception ex) { if (!(logger is null)) { logger.LogWarning(ex.Message); } } subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); var sessionOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { AutoComplete = false, MaxConcurrentCalls = _concurrentSessions, MaxAutoRenewDuration = TimeSpan.FromMinutes(_messageLockMinutes) //MessageWaitTimeout = TimeSpan.FromSeconds(30) }; subscriptionClient.PrefetchCount = 0; subscriptionClient.RegisterMessageHandler(OnMessage, sessionOptions); } }