Windsor Container: How to force dispose of an object?

后端 未结 3 2038
别跟我提以往
别跟我提以往 2021-02-08 03:15

I have an object that implements IDisposable that is registered with the Windsor Container and I would like to dispose of it so it\'s Dispose method is called and next time Reso

3条回答
  •  执念已碎
    2021-02-08 03:50

    It depends on the lifestyle of the component you specified when you added it to the container.

    You would use Release() If the lifestyle is Pooled. This puts the component back in the pool for the next retrieval (the object is not destroyed, so disposing would be bad)

    if the lifestyle is transient, a new object is created when you get the component. In this case the disposal is up to you, and you do not need to call Release

    If the lifestyle is Thread, the same component is used for each thread, not destroyed.

    If the lifestyle is Singleton, only one component is created and not detroyed.

    Most likely, you are using transient components? (if you are concerned about disposing of them in a timely manner) in that case, just wrap it with a using and you're set (or call the dispose yourself somewhere)

    using(ISomeService service = container.Resolve())
    {
     // Do stuff here
     // service.Dispose is automatically called 
    }
    

    Edit - Yes, in order to "refresh" or dispose and recreate your singleton you would need to either destroy the container or write a custom lifecycle. Doing a custom lifecycle is not actually that difficult and keeps the logic to do so in one place.

提交回复
热议问题