Created
April 13, 2017 20:34
-
-
Save jpda/bea09693a2de6c7498fdfd0292d56bd6 to your computer and use it in GitHub Desktop.
Azure Service Bus client explosion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.ServiceBus; | |
using Microsoft.Azure.ServiceBus.Filters; | |
namespace ConsoleApp2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var a = new Thing(); | |
a.Go(); | |
Console.WriteLine("All finished"); | |
Console.ReadLine(); | |
} | |
} | |
public class Thing | |
{ | |
string connectionString = "Endpoint=sb://<sbxonstring>"; | |
string topicName = "topic1"; | |
string subscriptionName = "sub1"; | |
public void Go() | |
{ | |
for (var i = 0; i < 10; i++) | |
{ | |
var salt = new string(DateTime.UtcNow.Ticks.ToString().ToList().Skip(13).ToArray()); | |
foreach (var ruleName in new[] { $"OtherRule-{salt}", $"SomeRule-{salt}" }) | |
{ | |
Console.WriteLine($"===== {ruleName} ====="); | |
RemoveRule(ruleName).Wait(); | |
AddRule(ruleName).Wait(); | |
} | |
} | |
} | |
public async Task RemoveRule(string ruleName) | |
{ | |
Console.ForegroundColor = ConsoleColor.White; | |
var client = new SubscriptionClient(connectionString, topicName, subscriptionName, ReceiveMode.PeekLock, new NoRetry()); | |
Console.WriteLine($"Attempting to remove rule named {ruleName}..."); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
try | |
{ | |
await client.RemoveRuleAsync(ruleName); | |
Console.WriteLine($"Removed existing rule named {ruleName}, pausing..."); | |
} | |
catch (Exception ex) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine($"No rule named {ruleName} exists, this should kill the client..."); | |
//Console.WriteLine($"{ex.Message}"); | |
} | |
Console.ForegroundColor = ConsoleColor.DarkRed; | |
Console.WriteLine("Closing client and waiting 1 seconds..."); | |
await client.CloseAsync(); | |
} | |
public async Task AddRule(string ruleName) | |
{ | |
Console.WriteLine($"Attempting to add rule named {ruleName}..."); | |
var client = new SubscriptionClient(connectionString, topicName, subscriptionName, ReceiveMode.PeekLock, new NoRetry()); | |
try | |
{ | |
await client.AddRuleAsync(ruleName, new TrueFilter()); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"Successfully added rule named {ruleName}"); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
catch (Exception e) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(e.Message); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
finally | |
{ | |
await client.CloseAsync(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment