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

10

This is a question I've always had (including in earlier versions of ASP.NET), but I'll ask here in the context of ASP.NET 5. The question is: when building an application with ASP.NET 5 we coded a Startup class that is responsible for setting up the pipeline with a Configure method. Anything about launching the web application is done there.

This is exactly where I am: the concept of initializing the web application. A web application does nothing on its own, it waits for requisitions to do. The doubt is: made a request the application is initialized, receives the request, sends the response and ends? Or is it initialized if it is not, processes the request, responds and waits for new requests?

How does this initialization concept work for an ASP.NET 5 web application? If it is actually initialized if it is not and wait for new requests, how does this actually work?

I made a test to see if I understood better what is happening as follows: I created a singleton class Exemplo which has a DataInstanciado property. After that I created a controller ExemploController as follows:

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

When I send a request to "/ api / instance" I received an object containing a datetime that is the date the object was created. In subsequent requests the returned object was exactly the same, with the same date. After 30 minutes, when requesting again the returned object was another, with the date of 30 minutes later.

In this way, the server seems to be keeping the application running. It looks like the objects are in memory. But I do not understand how this works. How does the life cycle of this type of application work?

EDIT : I know there is an application life cycle running in IIS, but IIS is not specifically what I'm asking. ASP.NET 5 applications use the OWIN specification and therefore can 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.

    
asked by anonymous 26.02.2015 / 03:41

1 answer

7

OWIN

  

OWIN defines a default between .NET servers and WEB applications. The purpose of OWIN is to decouple the server and application, and encourage the development of simple modules for a Web application in .NET.

link

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

Life Cycle

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

Important points:

  • It remains in memory
  • It has a startup
  • Can be terminated at any time (IIS, Weblistener, codegen, etc.).
  • IIS Lifecycle

    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:

  • IIS listens for the specified port on the server (80, 8080, 443, etc.)
  • Load your project (DLL's) and its settings (web.config)
  • Run an internal code to initialize your application
  • For each request made to the server, IIS assigns the response based on the implementation of its code (which is always loaded into 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 learn more about the life cycle of an IIS application here (in English).

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

    In these cases, to work on the same concept of previous lifecycles the concept is basically the same as IIS:

  • Listen for a specific port.
  • Initialize your application by discovering the class Startup
  • Assign responses based on the% registered%.
  • It may be easier to understand by this diagram:

    Thisdiagramisabstract,doesnotofficiallyrepresentthelifecycledevelopedbyMicrosoft.p>

    Okay,myapplicationstaysinmemory,butwhataboutMVC?

    Mvc,inASPNET5applications,isaservice(middleware)thatanswersrequestsandassignsresponsesbasedontheprocessingofcontrollers(selectedfromtheURL).ThemiddlewaresMVC,justlikeallothers,willalwaysbeinmemory,registeredinalist.

    Butthis(persistent)servicecreatesamiddleware(transient)foreveryHttprequest.

    So,codeslikethiswillalwaysshownewvalues:

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

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

    There's a simple reason for this to happen:

    link

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

        
    02.03.2015 / 21:59