0

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 Utity 2 wich does not have PerRequestLIfetimeManager, which would instantiate MainBCUnitOfWork per each request, which is sugested 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?

Community
  • 1
  • 1
RenanStr
  • 447
  • 5
  • 15

1 Answers1

1

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.

Backs
  • 22,763
  • 4
  • 44
  • 72
  • I have configured my application to use PerRequestLifeTimeManager but in the documentation they recomended to add this line in my code DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule)); which sugested that I use this inside UnityMvcActivator.cs class which I don't have in my project. All settings of Unity are in Global.asax. When I try to add it to Global.asax it did not work. Where should I place it? What does this line will do for my app? – RenanStr Mar 15 '16 at 20:58