问题
Does anybody know of a C# (mono) Linux web server hosting configuration that enables consistent static variables across threads (ie not the CGI model of an instance per thread)?
Details
I have some c# (mono) code that we need to host on linux as part of a simple web app. We don't need any traditional asp or even asp.net mvc features, really we just need the ability to respond to specific urls with dynamic content based on shared static variables.
Static Variables: The one special requirement we do have is that we need static variables to retain their value across all incoming requests (the same way they do on windows with an ASP.net mvc application). Common ways of hosting a mono c# web app like Apache mod_mono appear to use an 'c# application instance per thread' model where there would be multiple independent instances of our c# application each with their own static variables. That is the problem we need to avoid. I need a way of hosting a c# application that provides one common app with a shared set of static variables that all incoming requests will see. It is ok if when the app recycles that the static variables get reset (just like in normal IIS/MVC), so long as recycles are infrequent.
The background is that this web app needs to hold a large amount of data in RAM in a static variable and rapidly answer questions about that data. Having a copy of the app open for each thread would limit RAM resources too much and reloading the data into memory frequently would make the app too slow (occasional recycles like IIS/Asp.net MVC has are fine). The system works amazingly well on Windows/IIS7 (50,000+ dynamic requests per second) and we do not want change this architecture, so the answer I'm looking for relates to hosting not changing the application design.
回答1:
mod_mono does not spawn a new mono process for each new web request. Neither does it if you use the fastcgi server along with apache or nginx.
EDIT: Static variables are shared within the same Application Domain. This holds true for Microsoft .NET IIS and for mono (no matter if you are using mod_mono, xsp or mono-fastcgi-server). Even so apache spawns multiple worker threads and a single request is handled by a single worker thread, there is only a single mono process running at any given time (except if you have configured multiple asp.net applications, then there is one per application).
Mono tries to achieve 100% compatibility for ASP.NET as well, so there is no different behaviour by design!
The different behaviour of your program on linux is most likely not related to how mod_mono handles requests, but rather a bug in mono itself, or your code (i.e. by making a platform assumption that does not hold true on linux). You should really debug using MonoDevelop/xsp and try to pinpoint the problem more clearly, or paste some sample code here.
来源:https://stackoverflow.com/questions/13480328/c-sharp-mono-linux-web-server-hosting-with-consistent-static-variables-across