abp icon indicating copy to clipboard operation
abp copied to clipboard

Define custom "per application" setting which is read after app start from DB using appservice. How? SettingDefinitionProvider does not fit here?

Open alexandis opened this issue 5 years ago • 4 comments

ABP 4.3.0 / Angular.

I need to define custom setting for application. In my case it's an application version which is stored in DB and changed manually after next deployment (accessible via appservice method) and needs to be read once ONCE application start and be accessible from Angular client.

I can see SettingDefinitionProvider inherited class is adviced, where in Define method new settings are added. But it's a transient class and besides, Define method is sync. How to implement my requirement properly? It needs to be something like:

public class Program
{
    public static async Task Main(string[] args)
    {
        IWebHost webHost = CreateWebHostBuilder(args).Build();

        // Create a new scope
        using (var scope = webHost.Services.CreateScope())
        {
            // Get the MyVersionAppService instance
            var myVersionAppService = scope.ServiceProvider.GetRequiredService<MyVersionAppService>();

            //Get app version asynchronously
            var actualVersion = await myVersionAppService.GetAppVersionAsync('MY_APP_NAME');

            //TODO: add 'actualVersion' into 'SettingDefinition' somehow...
        }

        // Run the WebHost, and start accepting requests
        // There's an async overload, so we may as well use it
        await webHost.RunAsync();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

alexandis avatar Sep 29 '21 12:09 alexandis

Seems like I can use it like that:

            try
            {
                Log.Information("Starting AbxEps.Fines.HttpApi.Host.");
                var host = CreateHostBuilder(args).Build();

                using var scope = host.Services.CreateScope();

                var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
                var versionRepository = scope.ServiceProvider.GetRequiredService<IVersionRepository>();
                var version = await versionRepository.GetAsync('MY_APP_NAME');
                configuration["app_version"] = version;

                await host.RunAsync();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly!");
            }
            finally
            {
                Log.CloseAndFlush();
            }

alexandis avatar Sep 29 '21 21:09 alexandis

Hi,

Can you please check the documentation https://docs.abp.io/en/abp/latest/Settings

You don't need to make anything in the Program.cs

cotur avatar Sep 30 '21 11:09 cotur

@cotur Hi - I've already read this part. And that's why I expressed my doubt in the title: "SettingDefinitionProvider does not fit here?" I doubt because:

  1. it is not clear when SettingDefinitionProvider comes into game. I need to make sure everything happens ONCE on application start;
  2. Define method is SYNC, as I mentioned. And I need to call the appservice or repository method which is ASYNC. I don't want to wait till DB request finishes.

alexandis avatar Sep 30 '21 13:09 alexandis

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 29 '21 16:11 stale[bot]