|
| 1 | +using System; |
| 2 | +using Azure.Identity; |
| 3 | +using Azure.Storage.Blobs; |
| 4 | +using Microsoft.Extensions.Azure; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Hosting; |
| 7 | +using NServiceBus; |
| 8 | +using NServiceBus.ClaimCheck.AzureBlobStorage; |
| 9 | + |
| 10 | +class Usage |
| 11 | +{ |
| 12 | + Usage(EndpointConfiguration endpointConfiguration) |
| 13 | + { |
| 14 | + #region AzureDataBus |
| 15 | + |
| 16 | + endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>(); |
| 17 | + |
| 18 | + #endregion |
| 19 | + |
| 20 | + #region AzureDataBusConfigureServiceClient |
| 21 | + |
| 22 | + var serviceClient = new BlobServiceClient("connectionString"); |
| 23 | + endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>() |
| 24 | + .UseBlobServiceClient(serviceClient); |
| 25 | + |
| 26 | + #endregion |
| 27 | + |
| 28 | + #region AzureDataBusInjectServiceClient |
| 29 | + |
| 30 | + endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>(); |
| 31 | + endpointConfiguration.RegisterComponents(services => services.AddSingleton<IProvideBlobServiceClient, CustomProvider>()); |
| 32 | + |
| 33 | + #endregion |
| 34 | + |
| 35 | + #region AzureDataBusConnectionAndContainer |
| 36 | + |
| 37 | + endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>() |
| 38 | + .ConnectionString("connectionString") |
| 39 | + .Container("containerName"); |
| 40 | + |
| 41 | + #endregion |
| 42 | + } |
| 43 | + |
| 44 | + void UsageManagedIdentity(EndpointConfiguration endpointConfiguration, IHostApplicationBuilder builder) |
| 45 | + { |
| 46 | + #region AzureDataBusManagedIdentityServiceClient |
| 47 | + |
| 48 | + var serviceClient = new BlobServiceClient(new Uri("https://<account-name>.blob.core.windows.net"), new DefaultAzureCredential()); |
| 49 | + endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>() |
| 50 | + .UseBlobServiceClient(serviceClient); |
| 51 | + |
| 52 | + #endregion |
| 53 | + |
| 54 | + #region AzureDataBusManagedIdentityExtensions |
| 55 | + |
| 56 | + builder.Services.AddAzureClients(azureClients => |
| 57 | + { |
| 58 | + azureClients.AddBlobServiceClient(new Uri("https://<account-name>.blob.core.windows.net")); |
| 59 | + azureClients.UseCredential(new DefaultAzureCredential()); |
| 60 | + }); |
| 61 | + builder.Services.AddSingleton<IProvideBlobServiceClient, CustomProvider>(); |
| 62 | + |
| 63 | + #endregion |
| 64 | + } |
| 65 | + |
| 66 | + #region CustomBlobServiceClientProvider |
| 67 | + |
| 68 | + public class CustomProvider : IProvideBlobServiceClient |
| 69 | + { |
| 70 | + // Leverage dependency injection to use a custom-configured BlobServiceClient |
| 71 | + public CustomProvider(BlobServiceClient serviceClient) |
| 72 | + { |
| 73 | + Client = serviceClient; |
| 74 | + } |
| 75 | + |
| 76 | + public BlobServiceClient Client { get; } |
| 77 | + } |
| 78 | + |
| 79 | + #endregion |
| 80 | + |
| 81 | + void Complex(EndpointConfiguration endpointConfiguration) |
| 82 | + { |
| 83 | + var azureStorageConnectionString = ""; |
| 84 | + var basePathWithinContainer = ""; |
| 85 | + var containerName = ""; |
| 86 | + var maxNumberOfRetryAttempts = 3; |
| 87 | + // number of parallel operations that may proceed. |
| 88 | + var numberOfIoThreads = 3; |
| 89 | + // number of blocks that may be simultaneously uploaded when uploading a blob that is greater than the value specified by the |
| 90 | + var backOffIntervalBetweenRetriesInSecs = 1000; |
| 91 | + var renewFiveMinutesBeforeTokenExpires = TimeSpan.FromMinutes(5); |
| 92 | + |
| 93 | + #region AzureDataBusSetup |
| 94 | + |
| 95 | + var dataBus = endpointConfiguration.UseClaimCheck<AzureClaimCheck, SystemJsonClaimCheckSerializer>(); |
| 96 | + dataBus.ConnectionString(azureStorageConnectionString); |
| 97 | + dataBus.Container(containerName); |
| 98 | + dataBus.BasePath(basePathWithinContainer); |
| 99 | + dataBus.MaxRetries(maxNumberOfRetryAttempts); |
| 100 | + dataBus.NumberOfIOThreads(numberOfIoThreads); |
| 101 | + dataBus.BackOffInterval(backOffIntervalBetweenRetriesInSecs); |
| 102 | + |
| 103 | + #endregion |
| 104 | + } |
| 105 | +} |
0 commit comments