Reputation: 123
I'm in the process of migrating from Azure Storage Emulator to Azurite, and with this option comes the ability to define custom storage accounts (and associated keys).
However, in doing so I've run into an incompatibility (possibly a bug or limitation of .NET Core 3.1.x) with the code in our app and the prescribed connection string format.
Our storage client code looks like so:
private CloudBlobClient ServiceClient
{
get
{
if (_serviceClientBacking == null)
{
var options = _optionsResolver.Get();
var connectionString = GetStorageConnectionString(options.AzureStorageName, options.AzureStorageKey);
var account = CloudStorageAccount.Parse(connectionString);
_serviceClientBacking = account.CreateCloudBlobClient();
_serviceClientBacking.DefaultRequestOptions = new BlobRequestOptions
{
RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), 4),
LocationMode = LocationMode.PrimaryThenSecondary,
MaximumExecutionTime = TimeSpan.FromSeconds(20)
};
}
return _serviceClientBacking;
}
}
passing in a connection string like this (reference):
return $"DefaultEndpointsProtocol=http;AccountName={azureStorageName};AccountKey={azureStorageKey};BlobEndpoint=http://{localUrl}:10000/{azureStorageName};QueueEndpoint=http://{localUrl}:10001/{azureStorageName};TableEndpoint=http://{localUrl}:10002/{azureStorageName};";
Parses out a valid storage context, however, the client does not have secondary connections defined. The client code will choke on that connection string as the LocationMode
implementation throws an error, if you use the PrimaryThenSecondary
mode. The connection works as expected when changed to using the PrimaryOnly
mode.
The ideal solution to this issue would be a connection string format that allows for secondary connections to be defined (which should be supported by Azurite by appending -secondary
to the connection uri), however, I haven't been able to find any references to such a format. For now, I'm planning to workaround this by changing the LocationMode
only when in a local development environment to work with emulator.
Upvotes: 0
Views: 502
Reputation: 2336
The following code should be based on SDK which is already deprecated: NuGet Gallery | Microsoft.Azure.Storage.Blob Please upgrade it to new SDK NuGet Gallery | Azure.Storage.Blobs 12.14.0
You can refer the .net v12 SDK code sample for how to set the secondary location Uri in the new SDK: set the secondary location Uri to BlobClientOptions, then use the BlobClientOptions object to create BlobServiceClient. https://learn.microsoft.com/en-us/azure/storage/blobs/storage-create-geo-redundant-storage?tabs=dotnet#understand-the-sample-code
For Azurite secondary location Uri format, see https://github.com/Azure/Azurite#ra-grs.
BTW, for Azurite issue/questions, normally you can raise an issue in Issues · Azure/Azurite (github.com). So other users can also see it.
Upvotes: 1