From cb13fd7ddf3150253b9752e9764d00e50f556398 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 13 Nov 2023 16:48:07 -0800 Subject: [PATCH] Register DataProtection_Azure asynchronously --- .../Startup.cs | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.DataProtection.Azure/Startup.cs b/src/OrchardCore.Modules/OrchardCore.DataProtection.Azure/Startup.cs index d876d0fee2d..957820a2d29 100644 --- a/src/OrchardCore.Modules/OrchardCore.DataProtection.Azure/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.DataProtection.Azure/Startup.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; using Fluid; @@ -15,7 +16,6 @@ namespace OrchardCore.DataProtection.Azure { public class Startup : StartupBase { - private readonly IShellConfiguration _configuration; private readonly ShellOptions _shellOptions; private readonly ShellSettings _shellSettings; private readonly ILogger _logger; @@ -24,12 +24,10 @@ public class Startup : StartupBase private readonly FluidParser _fluidParser = new(); public Startup( - IShellConfiguration configuration, IOptions shellOptions, ShellSettings shellSettings, ILogger logger) { - _configuration = configuration; _shellOptions = shellOptions.Value; _shellSettings = shellSettings; _logger = logger; @@ -37,21 +35,29 @@ public Startup( public override void ConfigureServices(IServiceCollection services) { - var connectionString = _configuration.GetValue("OrchardCore_DataProtection_Azure:ConnectionString"); - - if (!string.IsNullOrWhiteSpace(connectionString)) - { - services.AddDataProtection().PersistKeysToAzureBlobStorage(connectionString, GetBlobContainerName(connectionString), GetBlobName()); - } - else + services.Initialize(async sp => { - _logger.LogCritical("No connection string was supplied for OrchardCore.DataProtection.Azure. Ensure that an application setting containing a valid Azure Storage connection string is available at `Modules:OrchardCore.DataProtection.Azure:ConnectionString`."); - } + var configuration = sp.GetRequiredService(); + + var connectionString = configuration.GetValue("OrchardCore_DataProtection_Azure:ConnectionString"); + + if (!string.IsNullOrWhiteSpace(connectionString)) + { + var containerName = await GetBlobContainerNameAsync(configuration, connectionString); + + services.AddDataProtection() + .PersistKeysToAzureBlobStorage(connectionString, containerName, await GetBlobNameAsync(configuration)); + } + else + { + _logger.LogCritical("No connection string was supplied for OrchardCore.DataProtection.Azure. Ensure that an application setting containing a valid Azure Storage connection string is available at `Modules:OrchardCore.DataProtection.Azure:ConnectionString`."); + } + }); } - private string GetBlobContainerName(string connectionString) + private async Task GetBlobContainerNameAsync(IShellConfiguration configuration, string connectionString) { - var containerName = _configuration.GetValue("OrchardCore_DataProtection_Azure:ContainerName", "dataprotection"); + var containerName = configuration.GetValue("OrchardCore_DataProtection_Azure:ContainerName", "dataprotection"); // Use Fluid directly as the service provider has not been built. try @@ -73,14 +79,14 @@ private string GetBlobContainerName(string connectionString) throw; } - var createContainer = _configuration.GetValue("OrchardCore_DataProtection_Azure:CreateContainer", true); + var createContainer = configuration.GetValue("OrchardCore_DataProtection_Azure:CreateContainer", true); if (createContainer) { try { _logger.LogDebug("Testing data protection container {ContainerName} existence", containerName); var _blobContainer = new BlobContainerClient(connectionString, containerName); - var response = _blobContainer.CreateIfNotExistsAsync(PublicAccessType.None).GetAwaiter().GetResult(); + var response = await _blobContainer.CreateIfNotExistsAsync(PublicAccessType.None); _logger.LogDebug("Data protection container {ContainerName} created.", containerName); } catch (Exception) @@ -94,9 +100,9 @@ private string GetBlobContainerName(string connectionString) return containerName; } - private string GetBlobName() + private async Task GetBlobNameAsync(IShellConfiguration configuration) { - var blobName = _configuration.GetValue("OrchardCore_DataProtection_Azure:BlobName"); + var blobName = configuration.GetValue("OrchardCore_DataProtection_Azure:BlobName"); if (string.IsNullOrEmpty(blobName)) { @@ -114,7 +120,7 @@ private string GetBlobName() var template = _fluidParser.Parse(blobName); - blobName = template.Render(templateContext, NullEncoder.Default); + blobName = await template.RenderAsync(templateContext, NullEncoder.Default); blobName = blobName.Replace("\r", string.Empty).Replace("\n", string.Empty); } catch (Exception e)