Should I implement IDisposable on a singleton?

前端 未结 3 864
别那么骄傲
别那么骄傲 2021-01-13 22:19

I have a windows service, which contains a singleton which in turn uses some loggers, message queue listeners and so on. Those classes implements IDisposable. S

3条回答
  •  离开以前
    2021-01-13 23:15

    No; Singleton shouldn't implement IDisposable. What if someone disposes the instance prematurely when others are in need of that?

    Also note that implementing IDisposable will not help you when your service is crashed/stopped. You'll have to dispose it manually! but you can't find right time to do it.

提交回复
热议问题