w.brian
w.brian

Reputation: 17417

Application_Start equivalent in ASP.NET 5

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

Answers (3)

Ahmet Arslan
Ahmet Arslan

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

Matt DeKrey
Matt DeKrey

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

Priyank Sheth
Priyank Sheth

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 :

Asp.Net 5 Startup

Asp.Net 5 Startup 2

For your last question, I did not found any other method documentation or declaration in Startup.cs class anywhere online.

Upvotes: 2

Related Questions