title: Locking a singleton public method in an ASP.Net app, or not
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Here is some 'pseudo code' for the singleton. I hope I have made my question understandable.</p>
  
  <pre><code>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);  
      }
  }
  </code></pre>