Using the life cycles available in ASP.NET Core
In this recipe, we will see an example with all the life cycles available and look at what it involves.
Getting ready
We will see what the differences between all these life cycles are.
How to do it...
- First, let's create an interface named
IOperationwith a getter on aGuidtype, and four interfaces that represent a task for each life cycle available:

- We will now implement these interfaces in an
Operationclass:

- In the
ConfigurationServicesmethod of theStartup.csclass, we manage the lifetime for each interface:

- Now, let's create an
OperationServiceclass that we will use in an MVC controller and execute in several HTTP requests:

- Now, we create the MVC controller and add an
Indexaction to show the value ofGuidfor each implementation:

- Finally, we create the view that shows the result:

How it works...
We can see the different values of Guids.
Request 1 :

Request 2:

The conclusion is the following:
- Transient: Objects are always different; a new instance...