IIS not retain Application State Variable - asp.net-mvc

I have created a MVC application and tested in my local , it was working as expected, but when I deployed in to an other server in application pool. It is not retaining the Application State Variable value.
So I changed all the Application State variable to Static custom objects and tried.Again I cannot get any values from the static variables used.
As I seen from some blog,I changed the mode from Integrated to Classic, even then I can get the value.
Can any one please help me in getting out from the issue

Are you deploying to a webfarm or are your servers clustered? if so - thats the problem. There is no guarentee you will hit the next server the same one as the one that served the request. The app pool has nothing to do with the behavior here (only in exceptional scenarios) but rather your environment.
Did you create a seperate app pool for this application? if not - its best practice to do so.

Related

Clearing a Singleton object between MVC sessions

I have an MVC web application that uses a Singleton object to get and store user identity information once. This object is stored as part of the model class.
Everything works fine when I'm running locally in Debug mode. It loads the Singleton at startup and when I exit and run again, it reloads. I assume this is because IIS express clears everything when the process exits.
When I publish to an actual web server, however, it loads the Singleton and seems to keep it around, even if I exit the website and return later. I'm not sure if this is because of caching or session information is being retained.
Can someone explain a good way to load data once but force it to be cleared on exit? I'm having difficulty understanding the scope and lifetime of data in an MVC application.
Thanks.

ASP.NET caching startup values

I'm converting an application from C# WebForms to MVC.
The application gets settings from a centralized location using Web Services. These are settings you would typically find in a Web.Config, but the desire of the company is to store these values in a centralized location for all apps.
Currently, any time you request an application setting, it checks HttpContext.Cache to see if you've already retrieved the settings. If you haven't, it makes the web service call, and stores the settings (100+ objects that are essentially key/values) in the HttpContext.Cache. So the call to get application settings only occurs once.
Should I be looking at another way to do this? I was thinking the settings should just be a REST service call where you pass the key and get a value (the current service is an *.ashx which is really not ideal for exception handling amoung other reasons). But obviously this would result in more web requests. What is considered a best practice here? Is the current method fine and I should just leave the code working the same in the MVC app?
It is better to load all resources in one call:
fewer HTTP requests are better for performance. Http requests can have a latency around 50 ms thus it will take much longer to get all values one by one 50 x 100 = 5000 ms => 5 secs
if the external web service goes down then your application still works because you already downloaded and cached all values
I will keep the current solution if it works and focus on new things instead of rewriting a working code.

MVC app getting stuck on an error after server restart

The scenario is as follows. I start an instance of MVC app to debug it. The app uses simple membership and I log in during this run. Then I go back to VS change something and start the instance again. It doesn't happen really often but sometimes at this moment membership starts acting odd. As the app starts, some action, that is behind [Authorize] attribute (to be exact the attribute is on the controller), is called. However the action fails because WebSecurity.CurrentUserId is equal -1 (the action in question just loads some user information based on WebSecurity.CurrentUserId).
If I clear cookies in browser, everything is fine, but I can't expect users to do the same when they encounter the problem.
My colleague explaind to me that it's (probably) happening because my local IIS decided to restart and some of session cookies became invalid, but if this can happen on local instance of IIS, wouldn't it be possible to also happen on the remote server?
Other important fact, the action that fails is called (more like redirected to) by a custom filter that we wrote. This filter is applied to all actions (but doesn't affect the one mentioned). Can this filter somehow make MVC ignore [Authorize] attribute?
I have a dirty workaround for this problem that should work (with this specific app), but I would prefer to prevent the problem from appearing int the first place.
I think this is related to this. Basically when the server gets reset authentication cookies die. They get recreated right away, except my app doesn't really have access to them till the page is reloaded (just like with logging in).
I partially solved the problem described above (a redirect is preformed somewhere on the way) so the application no longer gets stuck. However, if someone was logged in during the time the server restarted and he tries to preform a post after that, his post will not work and he will be redirected to a get action with the same name as the post action (our custom filter is to blame for that). Unfortunately I cannot fix the filter, because I would need user id for that and at the point at which the filter is called, it's still -1.
I guess my question is not too well written and kind of very localized (I should probably rewrite it or reask it), but the underlaying problem is more general than it seems, so let me salvage all the useful information into this answer.
Question 1: There is nothing preventing IIS from having a hiccup on a remote server and restarting the app, so yes this can (and happens) on the remote server (frequency will depend on the app itself and IIS configuration). The problem of disappearing session data seems to be related to the restarts of the app pool rather than the app itself.
Question 2: The custom filter has little to do with the situation. As pointed by Larry, in simple membership authorization is kind of unrelated to session data. If your session data is lost, the user does not stop being authorized, however user data is stored in the session. Without session you don't know who the user is. This information becomes available one action after session data was lost. So loosing session data can lead to a crash of the application or like in my case (where a custom filter depends on user data) to even weirder results.
So if you encounter unexpected disappearance of user data in your app (such as WebSecurity.CurrentUserId becoming -1), it might be worth investigating if your app pool is getting restarted (and why). Setting memory limits for an app pool seems to increase the likelihood of those restarts.

IIS instances sharing data

I don't know if this is common or something but I wanted to check. So I am building a site on an iis7 server and coming across a weird problem. Whenever I have 2 clients accessing the site it seems they are sharing info. Here is an example, when one client does a search for a particular item, the other client goes to the search page and see's the results of client's one search results. I am using a global class to store this information on my code behind.
So here is my question, my understanding of servers was that if two clients accessed the server they were running on different instances of the site, meaning that even if I have a global class in my code it would be as if two machines were running it. Am I wrong in this understanding?
Also are there settings in IIS that I need to change for this to work?
In asp.net, you can use Session variables which are unique serialized token type things stored in server memory. You can store html form info in these sessions so another page on your site can read it.
The syntax in your MVC controller action to create a Session would be:
Session["MyFormData"] = someObject;
http://msdn.microsoft.com/en-us/library/ms178581.aspx

global variable problem with rails+apache passenger

In my application I set a global variable in application controller,
In my development machine it works fine and variable is referred accross the application.
But in the production server (Apache passenger), when I change the global variable value through the application, it updates the table but, it doesn't reflect in the application. Every time I have to restart the server to get the global variable changed.
Please advise me where to set the global variable, so that I change the value of global variable from my application.
Thanks
The production Apache server will be using multiple processes and each process will have its own version of your application. Setting your global variable in one process won't affect the variable's value in any of the other processes.
If you need to share some information across instances, you'll have to store it in a database or similar common data store that is shared by all your server processes.

Resources