I have a question about Unity Container. My MVC application starts on Application_Start at Global.asax which is a container of Unity Container that works like below
_container = new UnityContainer();
_container.RegisterType(typeof(MainBCUnitOfWork), new PerResolveLIfeTimeManager());
From what I know, IIS will instantiate the type MainBCUnitOfWork only one time on its life cycle and will utilize the same instance in all requests, which is why I am using LifeTimeManager of type PerResolveLifeTimeManager.
My application has always worked well in this mode, however I am trying to utilize shared database access / cross database access where the required access will come from a (session, querystring) and change the database with the method below:
public void ChangeDatabase(string database)
{
Database.Connection.ConnectionString = "server=localhost;User Id=root;password=mypassword;Persist Security Info=True;database=" + database;
}
On my local testing everything works ok, but I have questions when in production if IIS processing many requests at the same time.
I did some research and found references where IIS only process one request each time, and if needed to process more than one request I should activate Web Garden, but this would bring other problems. See this link IIS and HTTP pipelining, processing requests in parallel
My question is, does IIS server only process one request each time, independent of the source?
The change of database during execution time can interfere with prior requests which are still ongoing?
I use Unity 2 which does not have PerRequestLIfetimeManager, which would instantiate MainBCUnitOfWork per each request, which is suggested here MVC, EF - DataContext singleton instance Per-Web-Request in Unity
In case I update for a newer version and utilize one instance per request, what would be the impact on my performance?
What is recommended for this situation?
From what I know, IIS will instantiate the type MainBCUnitOfWork only one time on its life cycle and will utilize the same instance in all requests, which is why I am using LifeTimeManager of type PerResolveLifeTimeManager.
This is wrong. Look at this articles (one and two). PerResolveLifeTimeManager
is not a singleton life time manager. You'll get new instance of MainBCUnitOfWork
for every Resolve.
What is recommended for this situation?
Using PerRequestLifeTimeManager
is the best choice for web applications. You will get a new independant instance of your UnitOfWork for every request.