Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:55:27+00:00 2026-06-11T10:55:27+00:00

I have been building ASP.NET MVC application and I’m worried about potential multi-threading issues

  • 0

I have been building ASP.NET MVC application and I’m worried about potential multi-threading issues when I launch it. One particular concern is the following code:

private static IDictionary<string, ISettings> _settingsDictionary = new Dictionary<string, ISettings>();

public T Settings<T>() where T : ISettings, new() {
    var key = typeof(T).FullName;

    if (!_settingsDictionary.ContainsKey(key))
        _settingsDictionary[key] = _settingsService.GetSettings<T>();

    return (T)_settingsDictionary[key];
}

Notice the dictionary is defined as static. This allows me to cache the dictionary so that it returns the same instance for every request for the length of the application.

This works fine when testing locally but i’m worried it may suffer when used by hundreds of users. This has led me to investigate the ConcurrencyDictionary. Please could you advise me on whether I need to use it and how i would go about doing so if that is the case.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:55:28+00:00Added an answer on June 11, 2026 at 10:55 am

    Yes there is a potential data race here:

    if (!_settingsDictionary.ContainsKey(key))
        _settingsDictionary[key] = _settingsService.GetSettings<T>();
    

    which could cause two threads to add the same key, since they can be interrupted at any point.

    You could use ConcurrentDictionary.GetOrAdd instead:

    private static ConcurrentDictionary<string, ISettings> _settingsDictionary = new ConcurrentDictionary<string, ISettings>();
    
    public T Settings<T>() where T : ISettings, new() {
        var key = typeof(T).FullName;
    
        return _settingsDictionary.GetOrAdd(key, _settingsService.GetSettings<T>());
    }
    

    Edit: Since you don’t want _settingsService.GetSettings<T>() to be executed every time, an alternative could be:

    private static IDictionary<string, ISettings> _settingsDictionary = new Dictionary<string, ISettings>();
    private static object locker = new object();
    
    public T Settings<T>() where T : ISettings, new() {
        var key = typeof(T).FullName;
        lock(locker) 
        {
            if (!_settingsDictionary.ContainsKey(key))
                _settingsDictionary[key] = _settingsService.GetSettings<T>();
    
            return (T)_settingsDictionary[key];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been building websites using vs 2008 (mostly asp.net mvc, jquery and webforms).
I have an ASP.NET MVC app that I have been building, and I am
I've been building an application with Fluent nHibernate/ASP.NET MVC - and I've dug around
I've been building my sample asp.net application using VWD2008 and the development virtual server
In the ASP.NET MVC site I am building, I have some methods where the
With ASP.NET MVC 1.0 I always have been able to generate strongly typed links
I'm building an asp.net MVC 2 app. I have a list view which lists
I am in the process of building a large asp.net Mvc project and have
I am building a search view in asp.net MVC 2 So I have: public
I have an issue that's been bugging me this morning. I'm building an ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.