2

I am creating an ASP.NET Web API gateway which needs to communicate to legacy COM component business logic written in C++. So I am creating another Web API which internally gets data from COM component through C# CLI layer.

Hence C++ COM component is not thread safe, I am considering to span multiple instances of WEB API per user request. The outer Web API Gateway can be instantiate the inner Web API per user and request to it.

Do any API gateways like Ocelot have any feature to achieve spanning multiple instance of same Web API? Or how to achieve spanning multiple instances and make request?

4
  • 1
    Um .... I would probably write a background service to manage a pool of external processes, growing the pool as needed. Then a scoped service to request an instance from the pool on first use, releasing the instance on dispose... Communicating via stdio? Named pipes? ... then just build the web api in front of that. You could then deploy as a pod in Kubernetes for more load balancing / failure handling? Commented Jan 30 at 1:05
  • 1
    Are you sure the C++ COM component is not thread safe? Of course it's technically possible but COM infrastructure (threading models learn.microsoft.com/en-us/windows/win32/cossdk/…) is supposed to help the COM component developer with that type of question (ie to simplify if it's marked "apartment" in the registry, COM can serialize calls from thread to thread, for example if a COM coclass members cannot be accessed from multiple threads). Commented Jan 30 at 6:45
  • @SimonMourier My COM component is a legacy one which has lot of shared data structures. It doesn't have any thread sync techniques. Commented Jan 30 at 7:13
  • 2
    Ok, but it doesn't need to, COM can handle that, that why the threading model feature was added (it's more than 30 years old). You should investigate what's its threading model is, if it declares one, but you can also force it declaratively to be "Apartment" learn.microsoft.com/en-us/windows/win32/com/inprocserver32. You can even put "Single" (although Microsoft doesn't document it any more) for the ThreadingModel value which will force all calls to any instance of this object for the process to be serialized to the initial (STA) thread that created the first COM object of this type Commented Jan 30 at 8:39

1 Answer 1

1
+50

Like already mentioned by the comment thread, you can load your COM model in a single-thread appartment state. If it's a legacy COM component, you have two major options to achieve it: either via registry or via your code.

A. If the COM component is correctly deployed and registered, you can change the appartment state via the registry with ThreadingModel 'Apartment' being a single-thread apartment.

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID
   {CLSID}
      InprocServer32
         (Default) = path
         ThreadingModel = Apartment

B. The other way is programmatically instantiate the COM component in a single-thread apartment, which would look somehow like this in your code:

using System.Runtime.InteropServices;

// create a new thread in single-thread apartment state
var staThread = new Thread(LoadCOMComponent);
staThread.SetApartmentState(ApartmentState.STA); 
staThread.Start();
staThread.Join(); 

static void LoadCOMComponent()
{
    try
    {
        var comType = Type.GetTypeFromProgID("yourobject");
        // activate COM object
        var comObject = Activator.CreateInstance(comType);
        // use COM object
        [your code]
        // release COM object
        Marshal.ReleaseComObject(comObject);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.