I want to have a global variable at application level, now the thing is that the variable must not be in side Application_Start since it will be sustainable to IISRESET for getting updated. Is there any way to maintain a global variable at application level which can be updated without IISRESET.
There are three options,
Web.Config - where you can store custom application specific settings
Going back to db and store the settings and load on application reset or initial load.
Store in ASP.NET Cache/Enterprise library cache.
Related
I have built an API application with ASP.NET Core 6. I used Environment.SetEnvironmentVariable(), but in production mode it will change every 20 minutes. So does it works like session to change every 20 minutes?
This only works in production mode.
It sounds like your application pool is getting restarted every 20 minutes which is a default behavior set in IIS. You should not rely on setting the environment variables for data or settings because it will not persist indefinitely. Instead, I would place this code in your Startup class and keep the value in a global class or if you have to use the environment variable for some reason, always add it on startup.
If you're trying to get Session-like behavior then the right way to do this is to read/write the value into a database table that is keyed to the specific user using the user ID or some cookie value.
Working with a series of websites that programmatically sets some appSettings values per website by simply looping through user-specified values that are stored in a database.
foreach (DataRow dr in result.Tables[0].Rows) {
ConfigurationManager.AppSettings.Set(dr["app_key_name"].ToString(), dr["app_key_value"].ToString());
}
This allows each site to use the same codebase, but have different variables defined and accessible.
The problem is that one (of many) sites seems to frequently lose the programmatically added appSettings. The few appSettings that are set within the Web.Config file remain, but the ones that were added programmatically seem to fail sometimes, or not reload when the site recycles.
Should these variables be check on Application_BeginRequest and re-added if they don't exist? Thoughts?
Don't you need to invoke Save()?:
config.Save(System.Configuration.ConfigurationSaveMode.Modified)
I have a problem. If I run my application by clicking on it, it loads the saved app.config settings.
However, I need to have the Application run at Startup. Got this working too, but when it loads it does not load the saved settings - just the default ones.
The first time my Application loads with the default settings I require the user to login to there account through a REST API, grab some data and store it. I then set
Properties.Settings.Default.is_installed = true
but when the app runs in Startup it loads the default (false).
Has anyone experienced this? Any help would be appreciated!
If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.is_installed = true;
Properties.Settings.Default.Save();
Here is MSDN Reference 1.
Settings that are application-scoped are read-only, and can only be
changed at design time or by altering the .config file in between
application sessions. Settings that are user-scoped, however, can be
written at run time just as you would change any property value. The
new value persists for the duration of the application session. You
can persist the changes to the settings between application sessions
by calling the Save method.
Here is MSDN Reference 2.
Saving User Settings at Run Time:
Application-scope settings are read only, and can only be changed at
design time or by altering the .exe.config file in
between application sessions. User-scope settings, however, can be
written at run time, just as you would change any property value. The
new value persists for the duration of the application session. You
can persist changes to user settings between application sessions by
calling the Settings.Save method. These settings are saved in the
User.config file.
I am using session to do two things:
Keep track of which css file to load for a particular company.
Keep track of the Company Id Guid that tracks what company we are in.
I am seeing that this is coming in as null sometimes but I'm not getting a consistent problem to track down. I have no doubt this is probably something in my code that I need to track down but this bring me up to my questions about session...
Am I relying on Session to much to pass information between screens?
Is it consistent?
What could be clearing it between page loads?
Is session easily cleared by the user?
Should I be using something else more reliable?
I am worried that I will move this to live usage and have all kinds of problems.
I'm simply doing this:
Session["Css"] = css;
and reading it the same way:
css = Session["Css"]
UPDATE
The session I am using:
HttpSessionStateBase Controller Session
There are a few types of Session State. InProc, StateServer, and SqlServer. I believe the default is InProc. You can read more about this on MSDN here and here.
Each of these will obey the timeout value for sessionState defined in your web.config file. For a single server setup (which is what I typically do) I usually have my sessionState setup as follows:
<sessionState
mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424"
timeout="2880" />
This is the same default timeout as Forms Auth, so my sessions will stick around as long as my users auth cookie/session. This will require you to set the start up on the ASP.NET State Server to Automatic. Below is my high-level pass at explaining the types and their potential drawbacks.
InProc
This will be reset everytime the application pool recycles the worker process for the web app. I believe this is what is happening to you, and this is why your session variables are null at what appear to be random times.
StateServer
This will persist state across apppool recycles, but requires that you enable the ASP.NET State Server Service, and change it's start up type to Automatic.
You can run into issues if you have a web farm or multiple web servers handling the website, unless you run a dedicated state server.
This requires that variables stored in session variables are [Serializable].
SQLServer
This will persist session variables to a SQL database, and is is generally the best approach for a web farm situation.
This requires that variables stored in session variables are [Serializable].
Could you not be writing your method to GetCss() something along these lines?
GetCss() {
var css = (CastToYourTypeHere)Session["css"];
if(css == null) {
//do whatever you'd normally do here to set the css,
//e.g. get the users guid and find their company css, and set css equal to it
css = myHypotheticalGetCssBasedOnTheUserFunction();
}
return css;
}
That way you're kind of covering all bases in that, you can happily use Session, if it it's lost for whatever reason you're just re-running your initial 'getCssForThisUser' type code.
IMO, session isn't a bad way to hold this kind of data for each user between pages in the type of example you've described.
I want to access my windows Azure Data Cache from my Role Entry StartUp routine. However I keep getting this error:
{"ErrorCode:SubStatus:Server collection cannot be empty."}
However when I do the same from within my Controller class it loads the Data Cache fine and I can go ahead and do things with it.
Is there anything special for the Role Entry class that I have to do to access the Data Cache prior to my application starting?
Or can't I access the Cache in the Role StartUp ?
Cheers
Starting with Azure SDK 1.3, there is a major change - the Full IIS mode. Read this blog post to get full undertanding of full IIS and what is it.
In short - your RoleEntryPoint descendant (where your OnStart method is being executed) lives in whole another AppDomain (and process actually - WaIISHost.exe), while your actual web application just lives in IIS (w3wp.exe). That's why there is no way to do something in OnStart() that would affect your web applicatin or that would be able to directly read your web.config.
If you do read Azure Data Cache in OnStart to do some preload of data for the web application, just do in your Global.asax's Application_Start() event handler.
If you need to read Azure Data Cache in OnStart for reason's specific to the RoleEntryPoint, you have to load the configuration from web.config. Web.config is placed in "./bin/web.config" relative to your AppRoot folder. (there are two copies of your application when you use WebRoles with full IIS - one lives in AppRoot and one lives in SitesRoot).
Hope this helps!
WebRole's OnStart probably does not use your web.config where you probably have specified server names and access keys for your AppFabric DataCache provider.
I would try manually instrumenting the server connection configuration.