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.
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.
In ASP.NET Core, the Startup class provides the entry point for an application, and is required for all applications.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With