2
votes

Working on an MVC application with the below architecture. Bootstrapped with Castle Windsor.

Controller -> Service -> Repository (uses DbContext).

Now certain flows in the application require that I run some part of the flow in a thread.

For example:

Controller -> service ->Repo1 -> control returns to service -> new Thread() started-> Repo2

The issue I face is the dbcontext is disposed as it is declared as LifestylePerWebRequest().I have tried using LifestyleTransient() that didnt seem to work. What am I missing?

There are similar dependencies which i have to sometimes use in a separate thread and sometimes in a single request. How do i configure Windsor to handle these dependencies?

1
What happens when you use LifestyleTransient()?Andrei Mihalciuc
@AndreiMihalciuc the dbcontext still shows as null.nikhil pinto

1 Answers

0
votes

There is a nuget package that i use to extend the lifestyles for Castle Windsor and it is called: Castle.Windsor.Lifestyles.

It has hybrid lifestyles which are handy for web requests and threads.

container.Register
(
    Classes.FromAssemblyContaining<IServiceFactory>()
            .BasedOn<IServiceFactory>()
            .WithServiceAllInterfaces()
            .Configure(c => c.LifeStyle.HybridPerWebRequestPerThread())
);

The important functionality is HybridPerWebRequestPerThread() which creates a new instance for the initial web request and then for every new thread it will create a new instance.