Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup Async Initializers on Tenant Container created. #13169

Closed
wants to merge 11 commits into from

Conversation

jtkech
Copy link
Member

@jtkech jtkech commented Jan 29, 2023

Useful to init any Tenant Singleton Service in an async way when a tenant container is built.

Without having to define and register another service as this is integrated by the system.

But by defining in any module startup an InitializeServicesAsync(IServiceProvider serviceProvider).

Or by using the new OCBuilder helper builder.InitializeServices(async serviceProvider => ....

In this PR we use the new OCBuilder extension helper to init the YesSql IStore.

builder.ConfigureServices(services =>
{
    services.AddScoped<IDbConnectionValidator, DbConnectionValidator>();
    ...
    // Configuring data access
    services.AddSingleton(sp =>
    {
        ...
        var store = StoreFactory.Create(storeConfiguration);
        ...
    });
    ...
})
.InitializeServices(async serviceProvider =>
{
    var store = serviceProvider.GetService<IStore>();
    if (store == null)
    {
        return;
    }

    await store.InitializeAsync();

    ...
});

Note: The usage of the OCBuilder helper allows to do this kind of thing from the web application.

@jtkech jtkech changed the title Introduce IStartup.InitializeServicesAsync() IStartup.InitializeServicesAsync() Jan 29, 2023
@@ -151,6 +149,22 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)
services.AddScoped<IDocumentStore, DocumentStore>();
services.AddSingleton<IFileDocumentStore, FileDocumentStore>();
services.AddTransient<IDbConnectionAccessor, DbConnectionAccessor>();
})
.InitializeServices(async serviceProvider =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name doesn't make it easy to understand what it does and when it's called.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebastienros

I'm open to any name ;) It is executed when a shell container is built, so after all ConfigureServices() and of course before all Configure()used to build the tenant pipeline. It allows to init some services in an async way, here I use it to initialize the IStore, see my 1st comment #13169 (comment)

.InitializeServices(async serviceProvider =>
{
    var store = serviceProvider.GetService<IStore>();
    if (store == null)
    {
        return;
    }

    await store.InitializeAsync();

    ...
});

@jtkech jtkech changed the title IStartup.InitializeServicesAsync() Async Initilizations in Startups, executed when a Tenant Container is built. Feb 15, 2023
@jtkech jtkech changed the title Async Initilizations in Startups, executed when a Tenant Container is built. Async Initilizations in Startups, run when a Tenant Container is built. Feb 15, 2023
@jtkech jtkech changed the title Async Initilizations in Startups, run when a Tenant Container is built. Async Initilizations in Startups, run on Tenant Container built. Feb 15, 2023
@jtkech jtkech changed the title Async Initilizations in Startups, run on Tenant Container built. Async Initializers in Startups, run on Tenant Container built. Feb 15, 2023
@jtkech jtkech changed the title Async Initializers in Startups, run on Tenant Container built. Startup Async Initializers executed when a Tenant Container is created. Feb 16, 2023
@jtkech jtkech changed the title Startup Async Initializers executed when a Tenant Container is created. Startup Async Initializers executed on Tenant Container created. Feb 17, 2023
@jtkech jtkech changed the title Startup Async Initializers executed on Tenant Container created. Startup Async Initializers on Tenant Container created. Feb 17, 2023
@jtkech jtkech closed this Feb 17, 2023
@jtkech jtkech deleted the jtkech/pre-configure branch March 17, 2023 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants