Search code examples
moduleblazorazure-blob-storageconnection-stringappsettings

How to access appsettings.json from module Configure Services


I have a blazor server side solution that contains an appsettings.json

I am configuring the blob storage in the ConfigureServices override in the Application Module of the Applications project. It currently has a hard coded connection string and is working perfectly.

Now I want to move the connection string to the appsettings.json file that is in the Blazor project.

I've tried to inject the IConfiguration into the constructor of the ApplicationModule, but the app throws an error when I try to do so.

I've searched through the ServiceConfigurationContext passed into to the ConfigureServices override. There is a Service property containing a collection of around 1,024 ServiceDescriptors and found one that contains the word IConfiguration in the ServiceType.FullName but haven't been able to figure out how to use it to get at the service itself in order to get at the appsettings.json values.

Can anyone shed any light on how to access the appsettings.json values from the application module?

Here is my code I am working with

namespace MyApp
{
    [DependsOn(
        typeof(MyAppDomainModule),
        typeof(AbpAccountApplicationModule),
        typeof(MyAppApplicationContractsModule),
        typeof(AbpIdentityApplicationModule),
        typeof(AbpPermissionManagementApplicationModule),
        typeof(AbpTenantManagementApplicationModule),
        typeof(AbpFeatureManagementApplicationModule),
        typeof(AbpSettingManagementApplicationModule),
        typeof(AbpBlobStoringModule),
        typeof(AbpBlobStoringAzureModule)
        )]
    public class MyAppApplicationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBlobStoringOptions>(options =>
            {
              options.Containers.ConfigureDefault(container =>
              {
                container.UseAzure(azure =>
                {
                  azure.ConnectionString = "DefaultEndpointsProtocol=https;AccountName=MyApplocalsa;AccountKey=<truncated-account-key>;EndpointSuffix=core.windows.net";
                  azure.ContainerName = "Pictures";
                  azure.CreateContainerIfNotExists = true;
                });
              });
            });
        }
    }
}

Solution

  • I finally figured out the answer to the question by looking at other modules in the solution.

    Here is the updated code

    namespace MyApp
    {
        [DependsOn(
            typeof(MyAppDomainModule),
            typeof(AbpAccountApplicationModule),
            typeof(MyAppApplicationContractsModule),
            typeof(AbpIdentityApplicationModule),
            typeof(AbpPermissionManagementApplicationModule),
            typeof(AbpTenantManagementApplicationModule),
            typeof(AbpFeatureManagementApplicationModule),
            typeof(AbpSettingManagementApplicationModule),
            typeof(AbpBlobStoringModule),
            typeof(AbpBlobStoringAzureModule)
            )]
        public class MyAppApplicationModule : AbpModule
        {
            public override void ConfigureServices(ServiceConfigurationContext context)
            {
                var configuration = context.Services.GetConfiguration();
    
                Configure<AbpAutoMapperOptions>(options =>
                {
                    options.AddMaps<MyAppApplicationModule>();
                });
    
                Configure<AbpBlobStoringOptions>(options =>
                {
                  options.Containers.ConfigureDefault(container =>
                  {
                    container.UseAzure(azure =>
                    {
                      azure.ConnectionString = configuration.GetSection("BlobStorage:ConnectionString").Value;
                      azure.ContainerName    = configuration.GetSection("BlobStorage:ContainerName").Value;
                      azure.CreateContainerIfNotExists = true;
                    });
                  });
                });
            }
        }
    }
    

    I needed to add the using

    using Microsoft.Extensions.DependencyInjection;
    

    I was able to get a reference to the configuration

    var configuration = context.Services.GetConfiguration();
    

    I updated the hard coded connection string with retrieving it from the configuration.

    azure.ConnectionString = configuration.GetSection("BlobStorage:ConnectionString").Value;
    azure.ContainerName    = configuration.GetSection("BlobStorage:ContainerName").Value;
    
    

    I updated the appsettings.json file in my Blazor app

    "BlobStorage": {
        "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=myapplocalsa;AccountKey=<truncated>;EndpointSuffix=core.windows.net",
        "ContainerName" :  "Pictures"
    }
    

    That was it!

    Thank you Joe for investing your time in providing answers to my question!