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

  • SEARCH
  • Home
  • 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 4053190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:24:57+00:00 2026-05-20T14:24:57+00:00

I have a class that implements singleton pattern as you can see below: public

  • 0

I have a class that implements singleton pattern as you can see below:

public class SearchSingletonObject
{
    private static SearchSingletonObject foundation = null;
    public SearchObject Object= null;
    private static StringCollection views = null;
    private static object control = new object();
    public IEnumerable<string> blacklist = null;
    public static void ClearFoundation()
    {
        foundation = null;
    }
    public static SearchSingletonObject Foundation
    {
        get
        {
            if (foundation == null)
            {
                lock (control)
                {
                    if (foundation == null)
                    {

                        foundation = new SearchSingletonObject();
                        var blacks = File.ReadAllText(HostingEnvironment.ApplicationPhysicalPath + "\\blacklist.txt");
                        foundation.blacklist = blacks.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Where(x => !string.IsNullOrEmpty(x) || x != " ");
                        views = new StringCollection();
                        var items = ConfigurationManager.AppSettings["SearchView"].Split(',');
                        foreach (var item in items)
                        {
                            views.Add(item);
                        }
                        foundation.Object = new SearchObject(ConfigurationManager.AppSettings["ContentDistributor"],
                                                                ConfigurationManager.AppSettings["Port"],
                                                                views);
                    }
                }
            }
            return foundation;
        }
    }
    public SearchSingletonObject()
    {

    }
}

We’re using this class in our wcf rest services. But in non-periodic intervals we get “Value can not be null” errors. Here is my log file:

08.03.2011 11:40:39 ERROR Message : Value cannot be null.
Parameter name: source

StackTrace : at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2           predicate)
at Search.Service.SearchService.Search(String keyword, Int32 offset, Int32 hit, String navstate, String username, Boolean issecure, Int32 suggest, String sortref, String fields, Int32 IsFirstSearch, String misspelled, String category) in D:\tfs\Hey\HeyRestApi\HeyService\HeyService.cs:line 68

Log file mentions the row below in my service:

var blacks = SearchSingletonObject.Foundation.blacklist.Where<string>(x => item.Equals(x)).FirstOrDefault();

It seems strangely the “blacklist” object getting null value. After the error, we have to reset IIS, to get application working. We can not reproduce the error on our local servers. It just happens in our customers production environment.

How can we solve that problem and what can be the reason of this strange error?

Thanks in advance,

  • 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-05-20T14:24:57+00:00Added an answer on May 20, 2026 at 2:24 pm

    Double-checked locking is broken.

    Short explanation:

    Even if foundation is non-null, without a lock you can’t be sure that foundation’s members are non-null from the point of view of your processor.

    You need to remove the if statement that’s outside the lock.

    Update

    A better solution is to mark foundation as volatile:

    private static volatile SearchSingletonObject foundation = null;
    

    The need for volatile modifier in double checked locking in .NET has more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static DataLibrary class that implements a singleton pattern. public static FacilityRepository
I have a generic class that implements IList public class ListBase<T>: IList<T>, IListBase{ IEnumerator<T>
I have a class that implements multiple interfaces. I would like to register these
Let's say I have a class that implements the IDisposable interface. Something like this:
I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have class method that returns a list of employees that I can iterate
I have script with defined class (for instance, Singleton.php). This class implements classic singleton
I have an object cache which implements the Singleton design pattern. My approach to

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.