Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application_Start equivalent in ASP.NET 5

Tags:

asp.net-core

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?

like image 234
w.brian Avatar asked Jun 01 '15 03:06

w.brian


People also ask

What is Startup cs file in asp net?

When doing so, the app will include a Startup. cs file that is responsible for setting up request middleware in a way that's very similar to how ASP.NET Core behaves. If you need to run code when your ASP.NET MVC app starts up, it will typically use one of these approaches.

What is the difference between startup cs and global ASAX?

Global. asax file contains mostly application level pre-defined events where Startup. cs file is more about registering services and injection of modules in HTTP pipeline.

What is Startup class in ASP Net Web API?

The Startup class 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.

What is .NET Core life cycle?

The ASP.NET Core MVC Request Life Cycle is a sequence of events, stages or components that interact with each other to process an HTTP request and generate a response that goes back to the client. In this article, we will discuss each and every stage of ASP.NET Core MVC Request Life Cycle in detail.


3 Answers

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.

like image 187
Matt DeKrey Avatar answered Oct 12 '22 03:10

Matt DeKrey


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.

like image 30
Priyank Sheth Avatar answered Oct 12 '22 03:10

Priyank Sheth


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

like image 41
Ahmet Arslan Avatar answered Oct 12 '22 03:10

Ahmet Arslan