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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:13:12+00:00 2026-06-07T05:13:12+00:00

I have an ASP.Net application that references a DLL which contains a singleton. The

  • 0

I have an ASP.Net application that references a DLL which contains a singleton. The singleton contains only two publically accessible methods and no public properties. The Web App creates an instance of this singleton during Application_Start. This is then available throughout the application lifespan.

The singleton maintains a connection to a 3rd party application, and listens for a number of different events that fire in the third party application. These events are logged and processed within the singleton, as and when they occur.

As users access the web app, their requests make use of a public method on the singleton that, in turn, uses private member data to call a method on the third party application. These calls are within a try…catch block.

Should I lock the public method to maintain thread safety, and if so would a simple private member of type System.Object, that is used only for the purpose of locking, be sufficient?

Here is some ‘pseudo code’ for the singleton. I hope I have made my question understandable.

using My3rdPartyDLL;

public sealed class MySingleton
{
    private static MySingleton instance = new MySingleton();
    private Object lockObj = new Object();

    private My3rdPartyAPI myAPI = null;


    public static MySingleton Instance
    {
        get{ return instance; }
    }   

    static MySingleton()
    {
    }

    private MySingleton() 
    {
        Initialise(); // Creates third party API and hooks up events.
    }

    // Here is the public method that I want to ensure is
    // thread safe.
    public void SomePublicMethod(String myString)
    {
        lock (lockObj)
        {
            try
            {
                My3rdPartyDLL.MyMethod(myString);
            }
            catch (Exception ex)
            {
                // deal with exception
            }
        }
    }

    private void My3rdParty_Event(EventObj obj)
    {
        MyEventLogger.WriteToLog(obj);  
    }
}
  • 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-07T05:13:14+00:00Added an answer on June 7, 2026 at 5:13 am

    A good point, but knowledge of whether the 3rd party is thread safe is
    not available to me. So, I created the singleton to maintain a single
    point of cohesion between my Web App and the API. I guess then if I
    cannot be sure 3rd Party API is thread safe, I should be defensive and
    deal with it in my own code. Perhaps I have just answered my own
    question… if this is the case, is using the lockObj a good method to
    deal with this?

    Given the fact that you cannot be sure if the external method is thread safe, it is better to synchronize calls to it.

    You can lock on a static lockObj, but watch out for performance. If you are serving a large number of requests/sec then you’ll run into problems (requests waiting for other requests to release the monitor), if the method is called at every requests or a close rate to once/request (or more).

    Since you’re locking a single method, you should be fine with lock. So no real use here for ManualResetEvent, Monitor and alternatives.

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

Sidebar

Related Questions

We have an ASP.NET web application, which is a C# DLL , that references
I have an ASP.NET Web Application which has a reference to 'C:\references\Utils.Varia.dll'. There is
I have an ASP.NET application that uses a custom .NET library (a .DLL file).
I have a DLL that provides mail sending functionality to an asp.net application, this
I have an ASP.NET application that is hosted in timezone A and is being
I have an ASP.NET application that calls other web services through SSL (outside the
I have an ASP.Net Application that sends text messages to mobile phones. It does
I have a database on SQL Server 2008. I have an ASP.NET application that
I have an Asp.Net MVC application that works in the vs.net development web server.
I have a ASP.NET MVC application that runs in both IIS 6 and 7.

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.