title: Same application with different assemblies - Singleton not working as expected
body: <p>I have a complex Winforms application that I have to refactor. I have a big problem with a Singleton-Class that is used in this application. It doesn't do what I think it should do (only in Release-Build).</p>
  
  <p>I have a Program.cs (Proj1.proj) that is the main entry point. </p>
  
  <pre><code>    static class Program
      {
          private static readonly ILog log = LogManager.GetLogger(typeof(Program));
  
          [STAThread]
          static void Main(string[] args)
          {
              try
              {
                XmlConfigurator.Configure();
                log.Debug("Logging enabled");
                File.WriteAllText("test1.txt", "Test");
                DBConnection.Instance.GetConnectionString();
                log.Debug("GetConnectionString called");
                DBConnection.Instance.GetDatabasename();
                log.Debug("GetDatabasename called");
                Application.Run(new Mainform());
              }
              catch (Exception ex)
              {
               //...
              }
          }
      }
  </code></pre>
  
  <p>Here is the DBConnection.cs (Proj2.proj)</p>
  
  <pre><code>public class DBConnection
  {
      private static readonly ILog log = LogManager.GetLogger(typeof(DBConnection));
  
      private static DBConnection instance;
  
      private string connectionString;
      private string databaseName;
  
      private DBConnection()
      {
          log.Debug("DBConnection ctor");
          this.connectionString = "example";
          this.databaseName = "example2";
      }
  
      public static DBConnection Instance
      {
          get
          {
              if (instance == null)
              {
                  log.Debug("Instance was null");
                  File.WriteAllText("test2.txt", "Instance was null");
                  instance = new DBConnection();
              }
              log.Debug("Return instance");
              return instance;
          }
      }
  
      public string GetConnectionString()
      {
          return this.connectionString;
      }
  
      public string GetDatabasename()
      {
          return this.databaseName;
      }
  }
  </code></pre>
  
  <p>I observed the following in my Winform Application in the Release Build:</p>
  
  <ul>
  <li>The Log-Entry "Instance was null" is <strong>never</strong> written in the
  log-file</li>
  <li>"Return instance" is written and that suggests that "Instance was null" should be in the log-file</li>
  <li>The Log-Entry "DBConnection ctor" is written more than once</li>
  <li>File.WriteAllText doesn't work (<em>doesn't create file or write to it / no exception</em>) in DBConnection.cs but is working in Programm.cs</li>
  </ul>
  
  <p>In Debug Build</p>
  
  <ul>
  <li>All expected Log-Entries are written and only one Instance is created</li>
  </ul>
  
  <p>What can cause this behaviour? Why does it work in Debug but not in Release? Why does he call the constructor twice? Why are some log-entries not written despite the fact that logging is working. Why does File.WriteAllText not work? </p>
  
  <p>I'm out of ideas and I can't find the cause of the problem. Clearly there has to be a setting in the project somewhere that can cause this... because if you rebuild the example shown above in a new project it will work...</p>