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:
Creating an instance of the COM component with CLSID from the IClassFactory failed due to the following error: 800401f7 (or 800a01b8).
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.
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.
EDIT: I created threads in sample windows application like this:
for (int i = 0; i < threadsCount; i++)
{
Task task = Task.Factory.StartNew(() => {
for (int j = 0; j < loopCount; j++)
{
SendRequest();
}
});
}
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.