I have several dependency injection services which are dependent on stuff like HTTP context. Right now I’m configuring them as singletons the Windsor container in the Application_Start handler, which is obviously a problem for such services.
What is the best way to handle this? I’m considering making them transient and then releasing them after each HTTP request. But what is the best way/place to inject the HTTP context into them? Controller factory or somewhere else?
With Castle Windsor you can use the
PerWebRequestlifetime – that should fit pretty well with your requirements.That means you can just inject the HTTP stuff into your services, and the container will take care of the proper lifetime management. However, this requires you to also register all these services (and all consumers of those services and so on) as PerWebRequest (or Transient) because if you register them as Singletons, they will hold on to stale (and possibly disposed) contexts.