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
IOperation
with a getter on aGuid
type, and four interfaces that represent a task for each life cycle available:

- We will now implement these interfaces in an
Operation
class:

- In the
ConfigurationServices
method of theStartup.cs
class, we manage the lifetime for each interface:

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

- Now, we create the MVC controller and add an
Index
action to show the value ofGuid
for 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...