Reuse legacy COM component in ASP .NET core Web API

Rajesh Subramanian 0 Reputation points

I have a legacy COM dll which exposes business logic via COM objects. Its implemented using VC++. I am in process of modernising my application for web. Initially I wanted reuse this COM component without any change. A single instance of COM object only can be created in one process. It doesn't support multiple instances in a process. How to consume this COM component from my ASP.NET web API?

Microsoft Technologies based on the .NET software framework.
3,459 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
4,241 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce ( 57,891 Reputation points

    if you com object is single threaded, then in core you would run it a dedicated thread (see background service). then you would use messages to send a request / response to the com object.

    to implement messages, you could use blocking collections. you could also just have static request/response variables, and use locking and a mutex.

    another option is create a node app that hosts the com object. the you new app would use httpclient to call the api. you could write a simple c++ wrapper, but node is single threaded, so this is nothing special. or try:

    if you might also look at updating your com object to a single threaded apartment model and create on a STA thread.

    1 person found this answer helpful.

  2. Michael Taylor 49,251 Reputation points

    Same way you do it in your legacy app.

    1. Add a COM reference in the project by right-clicking the Dependencies node and selecting the COM object.
    2. Create an instance of the COM client in your app and use accordingly.

    Of course using COM immediately makes your app Windows only but that is probably OK for now.