Home > Back-end >  what is scope and transit on request?
what is scope and transit on request?

Time:08-24

I am using .net core api.. I have below code... which is return a GUID in response... my question is when I tried from POSTMAN.. THIS url https://localhost:44362/weatherforecast

here I am clicking SEND in post man, and in each time.. I have tired with scope for testing , and transit for testing.. my question here in scope this should return same unique ID on SEND, unit I close browser? in in Transit it should return different GUID from first SEND , second SEND like wise...

not sure How this treated the same request in SCOPE of service in .net core api

my code is

            IOperationTransient transientOperation,
            IOperationScoped scopedOperation,
            IOperationSingleton singletonOperation,
            IOperationSingletonInstance singletonInstanceOperation)
        {
            _logger = logger;
            _operationService = operationService;
            _transientOperation = transientOperation;
            _scopedOperation = scopedOperation;
            _singletonOperation = singletonOperation;
            _singletonInstanceOperation = singletonInstanceOperation;
        }

  [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = _scopedOperation.OperationId.ToString()// Summaries[rng.Next(Summaries.Length)]
            }).ToArray();
        }
 public interface IOperation
    {
        Guid OperationId { get; }
    }

CodePudding user response:

Here a brief explanation about services lifetime in .NET:

Lifetime Singleton: When injecting a Singleton service its reference will always be the same during the lifetime of the application.

Lifetime Scoped: A scoped service is created once per scope. In AspNet, the default scope is the web request. So if you inject a Scoped Service IMyScopedService into two or more classes (e.g. UsersController(IUsersService, IMyScopedService), UsersService(IMyScopedService)) the reference of IMyScopedService will be the same within the same web request.

Lifetime Transient: A transient service is instantied every time the IoC container injects it into a constructor

CodePudding user response:

  • Singleton is a single instance for the lifetime of the application domain.
  • Scoped is a single instance for the duration of the scoped request, which means per HTTP request in ASP.NET.
  • Transient is a single instance per code request.

So when you define IOperation as Scope, container will create only one instance of it for your HTTP request, so it is absolutely normal that it creates one GUID as OperationId and for each of your requests returns that value and also definitely another value for other HTTP requests.

But if you define IOperation as transient, container creates for each code request an instance of IOperation. So each time you requests an OperationId, the value will be different.

Notice: We must be careful that the program does not have "Captive Dependency". Actually It occurs when a service intended to live for a short(er) amount of time gets held by a service that lives for a more extended amount of time.

Always consider the following rules when implementing dependency injection:

  1. Singleton service should not depend on either a Transient service or a scoped service.
  2. Scoped service should not depend on a Transient service.
  • Related