Can anyone explain me how MVC handles requests? I have a MVC Web application, and I had some issues with that until I tried to check in which AppDomain runs each request. There is strange thing(for me) happening that sometimes it creates controller in one AppDomain, sometimes in another. I added AppDomain.CurrentDomain.GetHashCode() to the constructor of the controller and the value is different for different requests.(but not for each request, sometimes the value was already used) It means that somehow it creates and runs the controller in another AppDomain.

I use visual studio 2010 development web server for debugging.

There is a callstack here: Callstack to the Controller

The fact there is Appdomain Transition and my code running in different AppDomains it definitely creates different AppDomains, but how I can control this? I'm not very experienced with this things, so can anybody give me some advise how to change that? I need my code to run in one AppDomain.

Thank you.

Update 1: It can be little bit confusing, I meant that my code runs in the different AppDomains in MVC, but WebServer creates the AppDomain and wraps calls to MVC in it. So, why this is happenning? Can anyone explain?

Update 2: I have found and issue and why it's creating the AppDomain each request. We have managed C# dll "PEngineCSharp" that internally uses unmanaged method "Process" through DllImport of C++ library called "PEngine".

In my controller I have several methods, and in one of them "PEngineProcess", the method is called like this:

PEngineCSharp.PEngine.Process(ProcessInfo info);

After each call to the "PEngineProcess" method of the controller each request starts in seperate AppDomain. If I don't call this method, but call others, that are pure managed they all run in one AppDomain. After commenting the call "PEngineCSharp.PEngine.Process(ProcessInfo info);" calls to the controller's method "PEngineProcess" doesn't create new AppDomain and everything runs in one AppDomain. For testing I just added "PEngineCSharp.PEngine.Process(ProcessInfo info);" to another controller's method and after that requests began to start again in new AppDomains. So i've found the cause of such behavior, also I've tried everything on VisualStudio WebServer, IIS Express 7.5 and IIS 7.0 - such behavior is same for all of them.

Please, can anyone explain me what is happening there? Thank you!

share|improve this question
"I need my code to run in one AppDomain." Because...??? edit: Let me say it a different way - this is way outside your control, and you need to figure out why your code is AppDomain-dependent and fix that. This is all part of the HTTP pipeline and isn't at all specific to MVC. – GalacticCowboy Apr 19 '12 at 17:19
Well I can be wrong of course, but I have an issue with DbContext of EF 4.1 CodeFirst. It configures model once per one AppDomain, and as I have each call in different AppDomain the performance falls, each request to the db takes too much time because each time it "builds" a model. I didn't notice such behavior in my small test application, I've tried to run it hundred times and it always runs in one AppDomain. With this big project things are different and I don't know where to look, and can't find any information about that.I don't understand why it is so,I only can think because of security – anderhil Apr 19 '12 at 17:24
GalacticCowboy: So can you give me some tips where to look? or to read about that? I can't find an info about that, and what influences such behavior. – anderhil Apr 19 '12 at 17:47
1  
What does your initializer look like? Is it completely rebuilding the model every time, instead of just when it changes? – GalacticCowboy Apr 19 '12 at 17:56
Also, how is your code structured? Are you using Areas? Or are these subsequent calls to the same controller that end up in different AppDomains? – GalacticCowboy Apr 19 '12 at 17:58
show 2 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.