Asp.Net 5 Application is losing state when enters an Event Handler C-Sharp

Question: A C# Application is losing Application State e.g. IFeatureCollection, it says " System.ObjectDisposedException: IFeatureCollection has been Disposed. Object name : 'Collection'.

Answer: If you experience this error it is because your code execution entered into an Event Handler hence losing the Application State. All Objects, Interfaces and Collections will be disposed because the Event Handler enters into a listening mode. The Event Handler will listen to an incoming data stream.

In order to solve this issue, if you want to get access to the Application Features, e.g. IFeatureCollection or the Database Context then consider calling a function that returns IActionResults. The call should originate from the Front-End, this way all Dependecies will be injected into a Dependecy Container and ready or available for Access again. If you want to store data from the previous request or from the logic in the Event Handler then cache the data you need (done in the Event Hanlder) then access the data from the Cache.

[NB] Keep in mind to clear the Cache before a new request or Register a CacheRepository as a Scopped in Service Container.


Edit this Article
Event Handler C-Sharp IFeatureCollection has been Disposed .NET 5 published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy