code_junkie

Windsor Container: How to force dispose of an object?

Nov 10th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. container.Release(obj);
  2.  
  3. obj.Dispose();
  4. container.Release(obj);
  5.  
  6. using(ISomeService service = container.Resolve<ISomeService>())
  7. {
  8. // Do stuff here
  9. // service.Dispose is automatically called
  10. }
  11.  
  12. var container = new WindsorContainer();
  13. container.AddComponentWithLifestyle<MyDisposable>(Castle.Core.LifestyleType.Singleton);
  14. var obj = container.Resolve<MyDisposable>(); // Create a new instance of MyDisposable
  15. obj.DoSomething();
  16. var obj2 = container.Resolve<MyDisposable>(); // Returns the same instance as obj
  17. obj2.DoSomething();
  18. container.Dispose(); // Will call the Disposable method of obj
  19. // Now the components need to be added back in
  20. container.AddComponentWithLifestyle<MyDisposable>(Castle.Core.LifestyleType.Singleton);
  21. var obj3 = container.Resolve<MyDisposable>(); // Create a new instance of MyDisposable
Add Comment
Please, Sign In to add comment