Reuse legacy COM component in ASP .NET core Web API

Rajesh Subramanian 0 Reputation points
2024-04-26T16:04:18.72+00:00

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?

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

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2024-04-26T18:40:24.8666667+00:00

    if you com object is single threaded, then in asp.net 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 asp.net 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:

    https://github.com/idobatter/node-win32ole

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

    https://learn.microsoft.com/en-us/windows/win32/com/single-threaded-apartments

    1 person found this answer helpful.

  2. Michael Taylor 51,346 Reputation points
    2024-04-26T16:20:02+00:00

    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.