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 377929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:44:38+00:00 2026-05-12T14:44:38+00:00

I am attempting to create a singleton service that is used to process incoming

  • 0

I am attempting to create a singleton service that is used to process incoming requests to an HttpHandler. At the moment the service is being instantiated on every request. I make a call to the static class that holds an instance of the service, implemented as a singleton as below:

 public static class ServerApplication {

static Service instance = null;
static readonly object padlock = new object();

/// <summary>
/// Service singleton.
/// </summary>
public static Service Service {
  get {
    lock (padlock) {
      if (instance == null) {
        instance = new Service();
      }
      return instance;
    }
  }
}

And access it using a call as below in the HttpHandler:

ServerApplication.Service.Process(request);

I have set a breakpoint on the instance = new Service(); line and with multiple requests the
breakpoint is triggered per request.

My aim is a service that exists across requests as it loads and caches lots of data from files and databases that is reused with most requests.

Can anyone see what is going wrong?

  • 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-12T14:44:38+00:00Added an answer on May 12, 2026 at 2:44 pm

    A couple of things:

    • If it’s a multiprocessor box, technically, you should mark the shared service instance with the “volatile” keyword or use a call to MemoryBarrier (see http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx). You didn’t specify architecture, so hard to say if this is really the issue, but better safe than sorry.
    • You should implement a double-check lock (eg, check for null both before and after acquiring the lock on “padlock”). This way you’re doing a much cheaper comparison instead of acquiring a lock on all the subsequent reads.

    That should cover you on the concurrency fronts. It’s also possible (though less likely) that your AppDomain is being unloaded between requests (ie, you wrote a file inside the web directory causing ASP.NET to think your app is stale), which would cause the statics to be reset.

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

Sidebar

Related Questions

I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I am attempting to create my 1st Simple DLL. I have a class(that's a
I'm attempting to create a WCF service where several thousand (~10,000) clients can connect
I am attempting to create an application that requires a TFS server URL. The
I am attempting to create a script that wraps a Groovy class that will
I'm attempting to create a Please wait UIAlertView that will appear while my code
I am attempting to create an application that will call a remote modem and
I'm currently attempting to create a tabbed interface in a web application, and based
I am attempting to create a small application to collect data received from an
I am attempting to create new membership users in an Ektron CMS400.NET-based website by

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.