|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.CommandLine; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Foundatio.AzureStorage.Samples; |
| 7 | +using Foundatio.Queues; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +// Azure Storage Emulator connection string |
| 11 | +const string EmulatorConnectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://localhost:10000/devstoreaccount1;QueueEndpoint=http://localhost:10001/devstoreaccount1;"; |
| 12 | + |
| 13 | +// Define options |
| 14 | +var connectionStringOption = new Option<string>("--connection-string", "-c") |
| 15 | +{ |
| 16 | + Description = "Azure Storage connection string (defaults to emulator)" |
| 17 | +}; |
| 18 | + |
| 19 | +var queueOption = new Option<string>("--queue", "-q") |
| 20 | +{ |
| 21 | + Description = "Queue name", |
| 22 | + DefaultValueFactory = _ => "sample-queue" |
| 23 | +}; |
| 24 | + |
| 25 | +var messageOption = new Option<string>("--message", "-m") |
| 26 | +{ |
| 27 | + Description = "Message to send", |
| 28 | + DefaultValueFactory = _ => "Hello World" |
| 29 | +}; |
| 30 | + |
| 31 | +var correlationIdOption = new Option<string>("--correlation-id") |
| 32 | +{ |
| 33 | + Description = "Correlation ID for the message" |
| 34 | +}; |
| 35 | + |
| 36 | +var propertiesOption = new Option<string[]>("--property") |
| 37 | +{ |
| 38 | + Description = "Custom properties in key=value format", |
| 39 | + DefaultValueFactory = _ => Array.Empty<string>() |
| 40 | +}; |
| 41 | + |
| 42 | +var modeOption = new Option<AzureStorageQueueCompatibilityMode>("--mode") |
| 43 | +{ |
| 44 | + Description = "Compatibility mode (Default or Legacy)", |
| 45 | + DefaultValueFactory = _ => AzureStorageQueueCompatibilityMode.Legacy |
| 46 | +}; |
| 47 | + |
| 48 | +var countOption = new Option<int>("--count") |
| 49 | +{ |
| 50 | + Description = "Number of messages to send", |
| 51 | + DefaultValueFactory = _ => 1 |
| 52 | +}; |
| 53 | + |
| 54 | +// Create root command |
| 55 | +var rootCommand = new RootCommand("Azure Storage Queue Enqueue Sample"); |
| 56 | +rootCommand.Options.Add(connectionStringOption); |
| 57 | +rootCommand.Options.Add(queueOption); |
| 58 | +rootCommand.Options.Add(messageOption); |
| 59 | +rootCommand.Options.Add(correlationIdOption); |
| 60 | +rootCommand.Options.Add(propertiesOption); |
| 61 | +rootCommand.Options.Add(modeOption); |
| 62 | +rootCommand.Options.Add(countOption); |
| 63 | + |
| 64 | +// Set handler |
| 65 | +rootCommand.SetAction(async parseResult => |
| 66 | +{ |
| 67 | + var connectionString = parseResult.GetValue(connectionStringOption) ?? |
| 68 | + Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING") ?? |
| 69 | + EmulatorConnectionString; |
| 70 | + |
| 71 | + var queueName = parseResult.GetValue(queueOption); |
| 72 | + var message = parseResult.GetValue(messageOption); |
| 73 | + var correlationId = parseResult.GetValue(correlationIdOption); |
| 74 | + var properties = parseResult.GetValue(propertiesOption); |
| 75 | + var mode = parseResult.GetValue(modeOption); |
| 76 | + var count = parseResult.GetValue(countOption); |
| 77 | + |
| 78 | + Console.WriteLine($"Using connection: {(connectionString == EmulatorConnectionString ? "Azure Storage Emulator" : "Custom connection string")}"); |
| 79 | + Console.WriteLine($"Mode: {mode}"); |
| 80 | + Console.WriteLine(); |
| 81 | + |
| 82 | + await EnqueueMessages(connectionString, queueName, message, correlationId, properties, mode, count); |
| 83 | + return 0; |
| 84 | +}); |
| 85 | + |
| 86 | +// Parse and invoke |
| 87 | +return await rootCommand.Parse(args).InvokeAsync(); |
| 88 | + |
| 89 | +static async Task EnqueueMessages(string connectionString, string queueName, string message, string correlationId, string[] properties, AzureStorageQueueCompatibilityMode mode, int count) |
| 90 | +{ |
| 91 | + using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information)); |
| 92 | + var logger = loggerFactory.CreateLogger("Enqueue"); |
| 93 | + |
| 94 | + logger.LogInformation("Creating queue with mode: {Mode}", mode); |
| 95 | + |
| 96 | + using var queue = new AzureStorageQueue<SampleMessage>(options => options |
| 97 | + .ConnectionString(connectionString) |
| 98 | + .Name(queueName) |
| 99 | + .CompatibilityMode(mode) |
| 100 | + .LoggerFactory(loggerFactory)); |
| 101 | + |
| 102 | + var queueProperties = new Dictionary<string, string>(); |
| 103 | + if (properties != null) |
| 104 | + { |
| 105 | + foreach (var prop in properties) |
| 106 | + { |
| 107 | + var parts = prop.Split('=', 2); |
| 108 | + if (parts.Length == 2) |
| 109 | + { |
| 110 | + queueProperties[parts[0]] = parts[1]; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + for (int i = 0; i < count; i++) |
| 116 | + { |
| 117 | + var sampleMessage = new SampleMessage |
| 118 | + { |
| 119 | + Message = count > 1 ? $"{message} #{i + 1}" : message, |
| 120 | + Source = "Enqueue Sample" |
| 121 | + }; |
| 122 | + |
| 123 | + var entryOptions = new QueueEntryOptions |
| 124 | + { |
| 125 | + CorrelationId = correlationId, |
| 126 | + Properties = queueProperties.Count > 0 ? queueProperties : null |
| 127 | + }; |
| 128 | + |
| 129 | + var messageId = await queue.EnqueueAsync(sampleMessage, entryOptions); |
| 130 | + |
| 131 | + logger.LogInformation("Enqueued message {MessageId}: '{Message}' with CorrelationId: '{CorrelationId}' Properties: [{Properties}]", |
| 132 | + messageId, sampleMessage.Message, correlationId ?? "<none>", |
| 133 | + string.Join(", ", queueProperties.Select(p => $"{p.Key}={p.Value}"))); |
| 134 | + } |
| 135 | + |
| 136 | + logger.LogInformation("Successfully enqueued {Count} message(s)", count); |
| 137 | +} |
0 commit comments