How is the life cycle of an ASP.NET 5 application?

Asked

Viewed 834 times

10

This is a question that I have always had (including in previous versions of ASP.NET), but I will ask here in the context of ASP.NET 5. The question is this: when building an application with ASP.NET 5 we encode a class Startup who is responsible for setting up the pipeline with a method Configure. Anything related to web application startup is done there.

That is exactly where my doubt lies: the concept of initialization of the web application. A web application does nothing for itself, it expects requests to do. The question is: made a request the application is initialized, receives the request, sends the reply and finishes? Or it initializes if not, processes the request, responds and waits for new requests?

How does this startup concept work for an ASP.NET 5 web application? If it really starts if you’re not and you’re waiting for new requests, how does it really work?

I did a test to see if I understood better what is happening as follows: I created a Singleton class Exemplo who owns a property DataInstanciado. After that I created a controller ExemploController as follows:

public class ExemploController : Controller
{
    [HttpGet("/api/instancia")]
    public Exemplo GetInstancia()
    {
        return Exemplo.ObtemInstancia();
    }
}

When sending a request for "/api/instance" I received an object containing a datetime which is the date of the creation of the object. In later requests the returned object was exactly the same, with the same date. After 30 minutes, when requesting the returned object again, it was another, with the date 30 minutes later.

This way, the server seems to be keeping the application running. It seems that the objects remain in memory. But I don’t understand how this works. How the life cycle of this type of application works?

EDITION: I know there’s a life cycle of an application running on IIS, but it’s not specifically IIS I’m asking about. ASP.NET 5 applications use the OWIN specification and can therefore be hosted not only on IIS, but in other ways as well (such as self host). What I wanted to know is how the life cycle works, regardless of where the application is hosted.

  • I believe this is the server session time and not Asp.net Lifecycle.

  • I really thought it had something to do with session, so to test put in the controller before Return the line this.Context.Session.Clear() and then I started getting the error "Invalidoperationexception: Session has not been configured for this application or request." so that apparently the use of sessions is not even set by default.

  • see if this helps https://msdn.microsoft.com/en-us/library/bb470252(v=vs.140). aspx

  • @kabstergo, it helps a little, but that’s in relation to the OSI specifically. ASP.NET 5 uses the OWIN specification, hence the lifecycle should not be connected directly to the server. You know if you have something similar for ASP.NET 5?

1 answer

7


OWIN

OWIN sets a default between . NET servers and WEB applications. OWIN’s goal is to decouple the server and the application, and encourage the development of simple modules for a web application on . NET.

http://owin.org/

Owin does not force the specification of a life cycle of a web application, but rather the development and use of Microservices that can be injected into the application.

Life Cycle

The lifecycle of ASP NET 5 web applications is totally dependent on the application that loads it (basically an ASP NET 5 project is a library that implements a specification).

Important points:

  1. She remains in memory
  2. It has a boot
  3. Can be finalized at any time (IIS, Weblistener, codegen, etc).

Life cycle of the OSI

In the case of IIS (and also IIS Express) you need to point the directory where your web application is and then start it. This process causes:

  1. IIS listens to the specified port on the server (80, 8080, 443, etc..)
  2. Upload your project (DLL’s) and your settings (web.config)
  3. Run an internal code to initialize your application

To each request made to the server the IIS assigns the response based on the implementation of its code (which is always loaded in memory). This is why the date (coming from a Singleton class) is always the same:

public class ExemploController : Controller
{
    [HttpGet("/api/instancia")]
    public Exemplo GetInstancia()
    {
        return Exemplo.ObtemInstancia();
    }
}

You can study more deeply about the life cycle of an IIS application here (is in English).

ASP NET 5 life cycle (IIS, Web Listener, etc)

In such cases, to work on the same concept of previous life cycle the concept is basically the same as the IIS:

  1. Listen to a specific door.
  2. Initialize your application by discovering the class Startup
  3. Assign responses based on middlewares registered.

Perhaps it will be easier to understand by this diagram:

Ciclo de vida ASP NET 5 This diagram is abstract, it does not officially represent the life cycle developed by Microsoft.

Ok, my application remains in memory, but what about the MVC?

Mvc, in ASP NET 5 applications, is a service (middleware) that meets requests and assigns responses based on controller processing (selected from the URL). The middleware MVC, like all others will always be in memory, recorded in a list.

But this (persistent) service creates a Controller (transient) for all Http requests.

so codes like this will always show new values:

public class ApiController
{
    [HttpGet("api/date/")]
    public DateTime GetDate()
    {
        return DateTime.Now;
    }
}

After 30 minutes, when requesting the returned object again, it was another, with the date 30 minutes later.

There’s a very simple reason for this to happen:

https://technet.microsoft.com/pt-br/library/cc753179%28v=Ws.10%29.aspx

IIS has a "memory recycle", if your application is long inactive it simply removes the application from memory (it will start again when a new request appears).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.