Session containing items implementing IDisposable

后端 未结 3 1536
北海茫月
北海茫月 2020-12-17 00:18

In ASP.NET if items are left in the session state that Implement IDisposable but are never specifically removed and disposed by the application when the session expires will

相关标签:
3条回答
  • 2020-12-17 01:01

    If the IDisposable pattern is implemented properly, then yes (i.e. the class's destructor will take care of disposing the object). I don't believe the ASP.NET session manager makes any guarantees about explicitly calling Dispose() on classes implementing IDisposable.

    Note that despite Mark's aggressive objections, I am not suggesting "routinely" adding finalizers. I am simply suggesting that if you want the Dispose method on your object called when the session expires, this is a viable option.

    0 讨论(0)
  • 2020-12-17 01:01

    I'd disagree with Sean's answer; firstly, finalizers should not be routinely added to classes, even if they are IDisposable - finalizers should only really be used in classes that represent unmanaged resources. Conversely, a class with a finalizer often is also IDisposable.

    Re the question: is Dispose() called - no, it isn't. The object will be garbage collected at some point in the future (indeterminate), but that is about it. A finalizer wouldn't add much here, as any encapsulated objects will also already be eligible for collection (assuming that they aren't referenced elsewhere).

    0 讨论(0)
  • 2020-12-17 01:10

    I'd be concerned to have Disposable objects in Session. It will almost certainly create a scalability issue for you. Anything that is Disposable is probably connected to some limited resource, if you have many active sessions you are likely to use up that resource. Secondly I'd expect that many (most?) disposable objects will not work well in a web farm as the resource they are tied to is probably local to a single machine and they won't serialize and then deserialize on another machine in the same state.

    0 讨论(0)
提交回复
热议问题