Filter by title

Bootstrap Modules

ABP can be bootstrapped in various applications. like ASP NET Core or Console, WPF, etc.

Modules can be started sync or async, Corresponds to various event methods in the module. We recommend using async, Especially if you want to make async calls in the module's methods.

The PreConfigureServices, ConfigureServices, PostConfigureServices, OnPreApplicationInitialization, OnApplicationInitialization, OnPostApplicationInitialization methods of module have the Async version.

Async methods automatically call sync methods by default.

If you use async methods in your module, please keep the same sync methods for compatibility.

public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
    await AsyncMethod();
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
    AsyncHelper.RunSync(() => OnApplicationInitializationAsync(context));
}

ASP NET Core

Bootstrap ABP by using WebApplication

var builder = WebApplication.CreateBuilder(args);
builder.Host.AddAppSettingsSecretsJson()
    .UseAutofac();

await builder.Services.AddApplicationAsync<MyProjectNameWebModule>();
var app = builder.Build();

await app.InitializeApplicationAsync();
await app.RunAsync();

Console

Bootstrap ABP in Console App.

var abpApplication = await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options =>
{
    options.UseAutofac();
});

await _abpApplication.InitializeAsync();

var helloWorldService = _abpApplication.ServiceProvider.GetRequiredService<HelloWorldService>();

await helloWorldService.SayHelloAsync();

Bootstrap ABP by using HostedService

public class MyHostedService : IHostedService
{
    private IAbpApplicationWithInternalServiceProvider _abpApplication;

    private readonly IConfiguration _configuration;
    private readonly IHostEnvironment _hostEnvironment;

    public MyHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment)
    {
        _configuration = configuration;
        _hostEnvironment = hostEnvironment;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        _abpApplication = await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options =>
        {
            options.Services.ReplaceConfiguration(_configuration);
            options.Services.AddSingleton(_hostEnvironment);

            options.UseAutofac();
            options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog());
        });

        await _abpApplication.InitializeAsync();

        var helloWorldService = _abpApplication.ServiceProvider.GetRequiredService<HelloWorldService>();

        await helloWorldService.SayHelloAsync();
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        await _abpApplication.ShutdownAsync();
    }
}

WPF

Bootstrap ABP on OnStartup method.

public partial class App : Application
{
    private IAbpApplicationWithInternalServiceProvider _abpApplication;

    protected async override void OnStartup(StartupEventArgs e)
    {
        _abpApplication =  await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options =>
        {
            options.UseAutofac();
            options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
        });

        await _abpApplication.InitializeAsync();

        _abpApplication.Services.GetRequiredService<MainWindow>()?.Show();
    }

    protected async override void OnExit(ExitEventArgs e)
    {
        await _abpApplication.ShutdownAsync();
    }
}

Contributors


Last updated: July 31, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

ABP Community Talks
Low-Code, High Precision
13 Aug, 17:00
Online
Watch the Event
ABP Live Webinar
Webinar Calendar Webinar Calendar
Discover
ABP Platform
Register Now
Oct 01
Thursday,
17:00 UTC
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.