Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put code to run after startup is completed

Tags:

asp.net-core

I have an aspnetcore app.

During startup, it does the usual startup actions.

After these are complete, I need to do some verification to ensure it was set up correctly. In particular, I need to call a stored procedure in the database using the default connection string. In other words, I need to create a class that uses dependency injection so that needs to be complete before it is called.

Just not sure where to put such code in StartUp.

like image 217
Greg Gum Avatar asked Jun 08 '18 15:06

Greg Gum


People also ask

Where is app start in .NET core?

ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.

What is the entry point for ASP.NET Core application?

In ASP.NET Core, the Startup class provides the entry point for an application, and is required for all applications.

What is IStartupFilter?

IStartupFilter is the basis of a mechanism for libraries to add middleware to the app. According to the Docs "IStartupFilter is useful to ensure that a middleware runs before or after middleware added by libraries at the start or end of the app's request processing pipeline".


1 Answers

Probably the best place is in Configure method after UseMvc() call. That is also the place where you usually apply migrations. You can add as many classes that DI knows as parameter.
For example:

public void Configure(IApplicationBuilder app) 

or

public void Configure(IApplicationBuilder app, AppUserManager userManager, IServiceProvider serviceProvider) 

or

public void Configure(IApplicationBuilder app, MyDbContext context) 

If you want to check this in background (only if you don't care about result - application should run also if verification fails), check my answer here.
Also this answer may help you.

like image 167
Makla Avatar answered Oct 22 '22 06:10

Makla