简体   繁体   中英

Can i store all my global application settings in Application Variable inside Global.asax file

I am developing a web application which is having performance issues. I am trying to put my best for that. I have done many changes and that's improving my performance too. I have one question :

  • Can i save all my AppSettings values in Global.asax file inside Application_Start event? Below is my code of Global.asax file:

     void Application_Start(object sender, EventArgs e) { // Code that runs on application startup // Setting all the common application level variables. Application["ConnectionString"] = ConfigurationManager.ConnectionStrings["UtilityServiceMISConstr"].ConnectionString; Application["SendErrorMail"] = ConfigurationManager.AppSettings["SendErrorMail"]; Application["SendErrorMailTo"] = ConfigurationManager.AppSettings["SendErrorMailTo"]; Application["LOGINURL"] = ConfigurationManager.AppSettings["LOGINURL"]; Application["ExpirePasswordDays"] = ConfigurationManager.AppSettings["ExpirePasswordDays"]; Application["FromMailID"] = ConfigurationManager.AppSettings["FromMailID"]; Application["BCCMailID"] = ConfigurationManager.AppSettings["BCCMailID"]; Application["CCMailID"] = ConfigurationManager.AppSettings["CCMailID"]; Application["IsSPLogging"] = ConfigurationManager.AppSettings["IsSPLogging"]; Application["DefaultReminderCount"] = ConfigurationManager.AppSettings["DefaultReminderCount"]; Application["PageSize"] = ConfigurationManager.AppSettings["PageSize"]; Application["SendSettlementMail"] = ConfigurationManager.AppSettings["SendSettlementMail"]; Application["SendSettlementMailTo"] = ConfigurationManager.AppSettings["SendSettlementMailTo"]; Application["Settlement"] = ConfigurationManager.AppSettings["Settlement"]; Application["AllowedLoginAttempts"] = ConfigurationManager.AppSettings["AllowedLoginAttempts"]; } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } 

Is this a good practice? If not then whats the best way to store my connection strings?

Recently i went in an interview. There i was asked a question that whats the best way to store connection string rather than reading web.config file again and again..?

Interviewers don't know everything, whatever seniority they claim to possess. Your application configuration gets read once, on application startup , so the ConfigurationManager.AppSettings[] indexer doesn't go to the configuration file every time you call it.

So no, it is no improvement whatsoever to use the code you show.

Configuration - best practices

No, your bottleneck is not there, and keeping configuration elements such as connection strings inside the Web.Config allows you to change the configuration of your application without having to recompile it or mess with anything else.

There's also other benefits of storing your connection string inside Web.Config , as you can encrypt the entire connection string instead of storing it in plain text... it adds another layer of security to your application.


Performance

Performance issues can be caused by a number of factors... here's a few of the most common culprits:

  • Front end

    • You may have too many scripts that are interfering with each other, or simply hogging the browsers resources. If this is the case, use the browsers debugging tools to find the root cause.
    • Not taking advantage of bundling and minification of your external resources means an extra round trip for the browser to load your website....

      • If you have 7 or 8 JavaScript files, that aren't minified or bundled together, your browsers is going to have to request those one by one, you'd be surprised just how much it could affect your application load times.
    • Not taking advantage of caching for Scripts, CSS and Images is another one, make sure static content is being cached.

  • Backend

    • If you're making calls to long-running tasks in a non asynchronous manner, it's really important that you rectify this. Every request made to your site is handled by an IIS thread, async allows IIS to hand off the request to another thread, so that it can go and serve other clients that are attempting to connect to your website.
    • Complex and slow LINQ queries.
    • Caching content by param.

List goes on, instead of worrying about what's happening on start up, try profiling your application and see which calls are taking excessively long to complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM