title: ASP.NET Web Api with COM dll
body: <p>I have ASP.NET Web Api project where I need to use VB6 Com dll. I have one controller, where I create object of class from com dll and use that object in my action. Everything seems to work when I call that action from ca. 2000-2500 threads, but when I run it from more threads, then I get this error while creating com object instance:</p>
  
  <pre><code>Creating an instance of the COM component with CLSID from the IClassFactory failed due to the following error: 800401f7 (or 800a01b8).
  </code></pre>
  
  <p>I found that com components run in STA Thread mode, but ASP.NET Web Api in MTA Thread mode, but I don't know if this caused the problem, because I couldn't find anything how to change mode to STA in ASP.NET Web Api project.</p>
  
  <p>I use self hosted ASP.NET Web Api and I host it on Windows Service. When I stop the service and run it again, I can again send ca. 2500 threads.</p>
  
  <p>EDIT: 
  I created threads in sample windows application like this:</p>
  
  <pre><code>for (int i = 0; i &lt; threadsCount; i++)
          {
              Task task = Task.Factory.StartNew(() =&gt; {
                  for (int j = 0; j &lt; loopCount; j++)
                  {
                      SendRequest();
                  }
              });
          }
  </code></pre>
  
  <p>EDIT2: Probably com object is not released, because in the Task Manager I can see that Handles grows up and I get this error when it has 2000+ handles. I call Marshal.ReleaseComObject, so I am not sure what can be wrong.</p>