Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a COM Automation Server in C#

I currently have a .NET class library written in C# that exposes its functionaility via COM to a C++ program (pre-.NET).

We now want to move the library out-of-process to free up address space in the main application (it is an image-processing application, and large images eat up address space). I remember from my VB6 days that one could create an "OLE automation server". The OS would automatically start and stop the server .exe as objects were created/destroyed. This looks like the perfect fit for us: as far as I can see nothing would change in the client except it would call CoCreateInstance with CLSCTX_LOCAL_SERVER instead of CLSCTX_INPROC_SERVER.

How would I create such an out-of-process server in C#? Either there is no information online about it, or my terminology is off/out of date!

like image 423
Grokys Avatar asked Nov 18 '09 14:11

Grokys


People also ask

What is an Automation server?

An Automation server is an application that exposes programmable objects (called Automation objects) to other applications (called Automation clients). Automation servers are sometimes called Automation components.

What is COM Automation?

A particular usage of Microsoft's COM-based component software architecture that lets applications expose their internal functions as COM objects. Called "automation" or "OLE automation," it enables tasks that are normally selected from menus to be automatically executed.

What is MFc Automation?

For the purposes of this discussion, we will define a micro fulfillment center (MFC) as an automated system for grocery ecommerce order picking that is co-located at a supermarket. The MFC handles item storage, order picking and order dispense for a large percentage of items in an ecommerce order.

How do you Automation variables in Business Central?

To use an automation server in Dynamics 365 Business CentralDefine a variable of type Automation and give it a name. Select the Subtype field, and in the Automation Object List window that opens, in the Automation Server field, get a list of available automation servers. Select a server and then choose the OK button.


3 Answers

You can actually do this in .NET (I've done it before as a proof-of-concept), but it's a bit of work to get everything working right (process lifetime, registration, etc).

Create a new Windows application. In the Main method, call RegistrationServices.RegisterTypeForComClients- this is a managed wrapper around CoRegisterClassObject that takes care of the class factory for you. Pass it the Type of the managed ComVisible class (the one you actually want to create- .NET supplies the class factory automatically) along with RegistrationClassContext.LocalServer and RegistrationConnectionType.SingleUse. Now you have a very basic exe that can be registered as a LocalServer32 for COM activation. You'll still have to work out the lifetime of the process (implement refcounts on the managed objects with constructors/finalizers- when you hit zero, call UnregisterTypeForComClients and exit)- you can't let Main exit until all your objects are dead.

The registration isn't too bad: create a ComRegisterFunction attributed method that adds a LocalServer32 key under HKLM\CLSID(yourclsidhere), whose default value is the path to your exe. Run regasm yourexe.exe /codebase /tlb, and you're good to go.

like image 94
nitzmahone Avatar answered Oct 05 '22 22:10

nitzmahone


You could always expose your .NET class as COM classes using InteropServices and then configure the library as a COM+ application. The .NET library would run out-of-process and be hosted by a DLLHOST.EXE instance.

like image 45
Tim Lowes Avatar answered Oct 05 '22 22:10

Tim Lowes


Here is an article in MSDN that covers all aspects of how to create COM localserver in c# (.net): link

like image 38
Nedko Avatar answered Oct 05 '22 23:10

Nedko