0

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?

Aydin
  • 15,016
  • 4
  • 32
  • 42
Rahul
  • 156
  • 2
  • 21
  • 3
    What has led you to believe that the reading of configuration information is the current performance bottleneck? – Damien_The_Unbeliever Jul 21 '15 at 08:54
  • 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..? So i thought to store my connection string in Application_Start event. – Rahul Jul 21 '15 at 08:59
  • But you're not storing your connection string in the `Application_Start`, you are **reading** it from there and the connection string is still in your `app.config` – Luke Jul 21 '15 at 09:10
  • So where is the best way to store connection strings? – Rahul Jul 21 '15 at 09:16
  • Most beginners prefer to guess what is slow instead of profiling the application. That can only lead to wrong solutions. – Lex Li Jul 21 '15 at 12:33

2 Answers2

1

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Okay. Forgetting about the performance. But is it valid to put my app settings inside Global.asax file as mentioned on above code? – Rahul Jul 21 '15 at 09:08
  • Sure, you can do that. – CodeCaster Jul 21 '15 at 09:10
  • Okies Thanks CodeCaster..:) – Rahul Jul 21 '15 at 09:11
  • It's only valid if you're happy with how the application_start method gets called with relation to how you're going to make use of the values. Maybe you need to read up on the application_start method and see if it fits in with what you're trying to do. – Luke Jul 21 '15 at 09:12
  • Putting too many application variables inside my web application, does this can affect my performance..? – Rahul Jul 21 '15 at 09:16
1

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.

Aydin
  • 15,016
  • 4
  • 32
  • 42
  • Okies Thanks Aydin Adn..:) – Rahul Jul 21 '15 at 09:52
  • Your welcome, if you don't understand any part of the answer, let me know so that I can clarify things for you. – Aydin Jul 21 '15 at 10:16
  • Thanks Aydin Adn. I appriciate your answers. Below is my question asked in my recent interview: Suppose in starting my website is having 200 users and suddenly it went to 20,000 users. How will i improve my performance and give response as quick as possible? – Rahul Jul 21 '15 at 10:27
  • When an interviewer asks questions like that, fire back and break down what type of answer they're expecting, there's an infrastructure side to that question and a dev side to that question. – Aydin Jul 21 '15 at 10:33
  • There's way too many variables, if I was asked that question I would ask what content we were serving, where we were serving them from, where our visitors originated, what the peak hours were... If you don't know the problem, then you can not solve it. I'd use their answers to those questions to set up a cloud environment on Azure and then configure scaling, but that's the beginning of a solution, not a complete solution... – Aydin Jul 21 '15 at 10:38
  • Hi Aydin Adn, he asked me from all the four parts i.e From Front end, From Backend, From UI and From Infrastructure point of view.. – Rahul Jul 21 '15 at 10:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83856/discussion-between-rahul-and-aydin-adn). – Rahul Jul 21 '15 at 10:57
  • Would you mind waiting 10 minutes? just in middle of prepping lunch – Aydin Jul 21 '15 at 10:58