Asp.net MVC requests in different Application Domains

删除回忆录丶 提交于 2019-12-12 05:08:51

问题


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:

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!

来源:https://stackoverflow.com/questions/10233257/asp-net-mvc-requests-in-different-application-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!