title: Dangers of C# MTA thread creating STA COM object
body: <p>I'm currently looking at a problem we are having in a .Net Framework 2.0 windows service (C#) that has X number of MTA threads running that access COM components.  Each thread initializes it's own instance of the com component object.  The com component object does not have any UI elements.  It's simply business logic that communicates with a sql server database and a C# dll with a com interface that in turn does socket communication and access to the same sql server database.  </p>
  
  <p>Through my research I've found that you should not be instantiating STA COM components on an MTA thread but I can't find any specific text to say what the dangers of this are or maybe it's the fact I don't understand COM threading apartments that well.  </p>
  
  <p>Would there be concurrency problems with the model being described above?  Even if each MTA thread is creating it's own STA COM object? </p>
  
  <h2>Edit</h2>
  
  <p>The problem we are actually running into is an object reference not set to instance of an object error in the setter of the connection string in following code block.  This occurs within the C# COM object that is called by the c++ COM object:</p>
  
  <pre><code>IDbConnection connection;
  
  //Code omitted for brevity where connection is initialized
  
  connection.ConnectionString = myConnectionString;
  </code></pre>
  
  <blockquote>
    <p>Exception Type: System.NullReferenceException  Message: Object
    reference not set to an instance of an object.   Data:
    System.Collections.ListDictionaryInternal   TargetSite: Void
    ConnectionString_Set(System.String)       at
    System.Data.OracleClient.OracleConnection.ConnectionString_Set(String
    value)        at
    System.Data.OracleClient.OracleConnection.set_ConnectionString(String
    value)</p>
  </blockquote>