0

I want to know some details about, how long the service object retains the value like is there any Time constraint for the service objects especially for Scoped and Transient.

Scoped: It retains the value for particular request (think of a browser tab).

Transient: It retains the value for the particular Component.

What if i stays long (not doing any action) in the particular Component(Page) where i have used Scoped and Transient service object.

Will the value be retained forever ? or will it dispose automatically after some time ?

Naveen
  • 127
  • 1
  • 10
  • 1
    If you mean “keep my browser on the page” then that doesn’t matter at all. It’s all about server side. Request comes and is handled and after that any objects that are tied to the request are dismissed. Note that they may be garbage collected some time after, not immediately. Is there a specific reason why you need to know the lifetime more specifically? – Sami Kuhmonen Feb 17 '20 at 07:36
  • 1
    What do you mean by stays long? once you've finished loading your page in browser, the request is finished. it realy does not matter how long you stay in that page. – Elendil Zheng-MSFT Feb 17 '20 at 09:13
  • 1
    If they implement `IDisposable`they get disposed when the parent container gets disposed (for requests: At the end of the request). Disposing is used to free **unmanaged** resources. The object still remains until the garbage collection collects it (assuming there are no references to it; If there are references, it won't be GCd but remain indefinitely until there are no more references but be in a disposed state). See [this answer](https://stackoverflow.com/a/40845639/455493) – Tseng Feb 17 '20 at 09:55
  • @Tseng I just wanna know is there any time constraints for the service objects and i referred the site you mentioned in your comment, it clearly says the value will be disposed. – Naveen Feb 17 '20 at 10:14
  • @ElendilZheng-MSFT Ex: I'm displaying a random number (Scoped object) in a page, so if i navigate to another page and come back the value stays right. Will the values gets destroyed after some time or not ?. Certainly it will be destroyed when i initiate a new connection. – Naveen Feb 17 '20 at 10:19

1 Answers1

1

What if i stays long (not doing any action) in the particular Component(Page) where i have used Scoped and Transient service object.

A request lasts from the point you hit enter in the url (or a link or button) until the page is loaded. After that, the request ends.

HTTP is stateless, it doesn't have any permanent connection or state. If a page takes 50 ms to load, after that the request ends and services get disposed. When you click a link, a new request starts with new dependencies instantiated.

Every request will always have a new instance, so don't "store" data in services that you need for next request. If you need stuff for next request only, use TempData and if it needs to persist more than until next time its accessed, use Sessions

Will the value be retained forever ? or will it dispose automatically after some time?

After end of the request, its not save to access the services anymore. Disposing releases all unmanaged resources (connection, file handles, unmanaged memory).

The object itself remains some (unspecified) time in memory until the garbage collection kicks in and frees the object and the memory. That assumes you are not holding any reference to the service after the request ends, since only objects that have no references can be garbage collected.

That's how managed runtimes work. Garbage collection is not deterministic, so it can happen at any time when the conditions to trigger it are happened.

Tseng
  • 61,549
  • 15
  • 193
  • 205