Reputation: 17417
I've come across two methods on the Startup class, ConfigureServices
and Configure
. Is there an equivalent to the Application_Start
method from previous versions of asp.net for doing arbitrary work when the application is starting?
Edit
As a follow up, what are the possible methods that ASP.NET 5 is expecting on the Startup class?
Upvotes: 9
Views: 5616
Reputation: 6150
You could use IHostedService interface on .NET Core 2.0 .
The IHostedService background task execution is coordinated with the lifetime of the application.When you register your class, you could do whatever you want on starting-stopping phases of application like using Application_Start and Application_End.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHostedService, MyBackgroundStartupTask>();
}
There is another option available since .NET Core 2.1. An abstract base class we could derive from, named BackgroundService
Upvotes: 1
Reputation: 11942
As Simple Man said, there is no direct equivalent method in ASP.NET 5; similar functionality should be started by your services when appropriate in keeping with the Single Responsibility Principle. (The closest is the Configure
method, which is where you should probably "start" any services that need to be "started".) However, there is one other method often overlooked in the Startup class: the constructor. Some logic, such as loading config files, may be appropriate there.
You can see how the methods are located on the Startup class in the Hosting repository. Only the two methods you mentioned and the Startup constructor are used.
Upvotes: 2
Reputation: 2362
If I am not wrong from what I understood, there is no such equal method. Rather, there are two different methods, ConfigureService
and Configure.
ConfigureService
is a method to configure services for your project. The purpose of this method is to set dependency injection for your project. This is the method that will be fired first after constructor has been called.
Configure
is a method to configure request pipeline. This method will execute after ConfigureService
.
You can refer below two links :
For your last question, I did not found any other method documentation or declaration in Startup.cs class anywhere online.
Upvotes: 2