c# - Castle components dispose order -


We have several castle windsor components declared in the config file, some of the deep inside components may require the services of other components. is.

The problem occurs when the application is shutting down and the container is being settled. During the settlement () / stop () of the commenceable / disposable component (A) when it requires the services of any other component (B) the component Knotfoyd exception was raised then that time B was removed before the container has gone.

I have noticed that the order of component announcements in the App Config file is important. . And a reodering and solution to B problem

There is a better way to influence the order in which the components are disposed of

Edit:? After requesting in a comment I provide a sample code here that throw ComponentNotFoundException:;; ICriticalService & gt;

  class program {static void main () {IoC.Resolve & lt () DoStuff (); IoC.Resolve & LT; IEmailService & gt; () SendEmail ("Blow") .; IoC.Clear (); }} Internal Class CriticalService:. ICriticalService, IStartable {Public Zero Start () {} Public Zero Stop () {//, ComponentNotFoundException should be thrown as the EmailService is already dealt with and removed from the container IoC.Resolve & lt; IEmailService & gt; () SendEmail ("Stop"); } Public Zero DoStuff () {}} Internal Class EmailService: IEmailService {Public Zero SendEmail (String Message) {Console.WriteLine (Message); } Public Zero () {Console.WriteLine ("Email Service Service."); GC.SuppressFinalize (this); }} Internal Interface ICriticalService {Zero DoStuff (); } Internal Interface IEmailService: IDisposable {Zero SendEmail (string message); } Public Static Class IoC {Private Static Read Only IWindsorContainer _container = New Windsor Container (New XML Interpreter ()); Static IoC () {_container.AddFacility & lt; Initial qualification & gt; (); // The following 2 lines solve the swapping problem _ container.admnant & lt; ICriticalService, CriticalService & gt; (); _container.AddComponent & lt; IEmailService, EmailService & gt; (); } Public stable zero clear () {_container.Dispose (); } Public static t resolve & lt; T & gt; () {Return (t) _ Container [typef (t)]; }}  

Note:.

, a static IOC class, how to code in the container, You are actually using the container as a service locator, thus losing most of the benefits of dependency injection.

The problem is that without a proper injection, Windsor is not aware about CriticalService -. IEmailService dependency, then it can not ensure the proper ordering of the settlement.

In this case you are refactor to make the dependence clear, windsor components in the correct order:

  Internal range Critical Services: iCriticalService, Istateble {email at private RedondoIEMLSE; Public Critical Services (IEELSViews Email) {this.email = email; } ...}  

Like after refactoring.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -