Search code examples
.netdependency-injectioncastle-windsor

using PerWcfSession lifestyle component outside of the WCF context


We are using Catle Windsor as our DI conatiner.

We have server that handle wcf requests so we registered the DbContext injected into our repositories with PerWcfSession lifestyle in order to dispose that context after each request.

On the same server we have another thread running sepeartly to do some scheduling work and we want this scheduler use one of the repository we have but the problem is that we can't reuse this repository because his DbContext is with PerWcfSession lifestyle and the scheduler thread is not part of the WCF request\response flow.

I am thinking maybe in that case we need seperate container for that seperate thread but i am not sure of that.

any ideas?


Solution

  • Few choices come on my mind:

    • Separate your WCF service and scheduler to a separate processes, so that you will end up with separate applications with separate containers and different component lifestyles. Furthermore you will benefit from microservice architecture and have pros like different requirements for scalability and so on (of course there are cons as well).
    • As you said you may have only one process with multiple containers for each thread.
    • Have a one process with one container but multiple registrations for each component with different lifestyles. Than you have to use service overriding to specify where particular registrations belongs to (see https://github.com/castleproject/Windsor/blob/master/docs/registering-components-one-by-one.md Service override section).