Soumya Dwibedy
What is the difference between services.addscoped, services.addtransient, and services.addsingleton?
By Soumya Dwibedy in .NET Core on Jun 26 2019
  • Soumya Dwibedy
    Jul, 2019 1

    Transient services: The object of these services are created newly every time a controller or service class is called or executed. ex: lets consider a request is made to a web api controller method ‘xcontroller’ as a transient then the request will be assigned a unique id, now from inside that controller the request is redirected to another controller method for further execution, then the same request id will be replaced with a fresh request id.

    Singleton service: The object of this service are created once initially and does not change for any no of requests, regardless of whether an instance is provided in ConfigureServices. Ex: in the above example no matter how many redirects happen within the application or new requests are made to the application the id remains same for all.

    Scoped service: The objects of this service are same within the every requests and changes with new requests. Ex- In the above example the id for the request will remain same as the same request is redirected to another controller method. But at the same time if another request is made to the controller then it will be assigned a fresh id.

    • 6
  • Soumya Dwibedy
    Jun, 2019 26

    Transient services: The object of these services are created newly every time a controller or service class is called or executed. ex: lets consider a request is made to a web api controller method 'xcontroller' as a transient then the request will be assigned a unique id, now from inside that controller the request is redirected to another controller method for further execution, then the same request id will be replaced with a fresh request id. Singleton service: The object of this service are created once initially and does not change for any no of requests, regardless of whether an instance is provided in ConfigureServices. Ex: in the above example no matter how many redirects happen within the application or new requests are made to the application the id remains same for all. Scoped service: The objects of this service are same within the every requests and changes with new requests. Ex- In the above example the id for the request will remain same as the same request is redirected to another controller method. But at the same time if another request is made to the controller then it will be assigned a fresh id.

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS