Monday, August 23, 2021

Late Binding Client Code Generator for COM Components

Let's suppose you have a C# class that acts as a server and you need clients to consume it across network.
There are two options:
  1. DCOM via COM+
  2. WCF
Presently .NET framework provides RCW (Remote callable wrapper) mechanism for .NET code to interface with COM components. However it does not support late binding and has following limitations notably:
  1. Early bound requires tlb or dll to be part of the project for unmanaged dlls
  2. Managed com dlls cannot be consumed using tlb by managed client
  3. Don't support  remote activation to work across network.
Late Binding Client Code Generator tries to address as below:
  • Generate C# based late binding client code 
  • Generate VBScript based late binding client code 
  • Generate remote registration files
  • Supports .Net framework 3.5
  • Generate code that can be deployed as a WCF Server using COM+ Integration
  • Generate code to start and stop COM+ application
Example
WSC COM component
As described in the previous post, use sillycalc.wsc. Load  the wsc file as shown below and generate the wrapper code.



COM+ services offers features such as thread pooling, object pooling, just in time activation, security etc. These services can be availed by hosting COM components in a COM+ application.
This also allows the COM component to be consumed across the network using DCOM.

Add the component in a COM+ application by executing the command

ComAppHelper.exe  -ins -app:testcalc -reg  D:\Github\TechBlog\COM+\WSC\server\sillycalc.wsc

This will create a COM+ application called calctest and hosts SillyCalc.WSC  as shown below

C# client
The outputfile.cs contains a class COM_LB_SillyCalc_WSC_1 which can be instantiated to create a proxy for the server. This class also contains wrapper code for calling the methods and properties defined in the server.
Below is an example:
       
class Program
{
        public static void Main()
        {
            var lbclient = new COM_LB_SillyCalc_WSC_1("127.0.0.1");
            Console.WriteLine("40+6={0}", lbclient.add(40, 6));
        }
}	

 
 

The output

VBScript client
outputfile.vbs contains a class COM_LB_SillyCalc_WSC_1 which can be instantiated to create a proxy for the server. This class also contains wrapper code for calling the methods and properties defined in the server.
Add following at the bottom of outputfile.vbs, Edit add function to remove set.
       
	   
set x = new COM_LB_SillyCalc_WSC_1
x.createcomobject("127.0.0.1")
msgbox (x.add(10,2))

 
 
Execute as shown below:
cscript  outputfile.vbs

The output


Managed COM component
As described in the previous post, use CalcServer.Calucalator. Load  the dll file as shown below and generate the wrapper code.


Add the component in a COM+ application by executing the command:

ComAppHelper.exe -ins -app:calctest  -reg -runforever  D:\Github\TechBlog\COM+\Managed\server\bin\CalcServer.dll

This will create a COM+ application called calctest and hosts CalcServer.dll as shown below

C# client
The outputfile.cs contains a class COM_LB_CalcServer_Calucalator which can be instantiated to create a proxy for the server. This class also contains wrapper code for calling the methods and properties defined in the server.
Below is an example:
       
	   
class Program
{
        public static void Main()
        {
            var lbclient = new                     COM_LB_CalcServer_Calucalator("127.0.0.1");
            Console.WriteLine("40+6={0}", lbclient.add(40, 6));
        }
}

 
 

The output

VBScript client
outputfile.vbs contains a class COM_LB_CalcServer_Calucalator which can be instantiated to create a proxy for the server. This class also contains wrapper code for calling the methods and properties defined in the server.
Add following at the bottom of outputfile.vbs, Edit add function to remove set.
       
	   
set remoteclient = new COM_LB_CalcServer_Calucalator
remoteclient.CreateComObject "127.0.0.1"
msgbox remoteclient.add(10,2)

 
 
Execute as shown below:
cscript  outputfile.vbs

The output

Debugging a Component in COM+ Application
To debug, simply attach to the dllhost process in visual studio. 


Make sure correct pdb files are copied.

Set breakpoint and debugger breaks.


Source and Binaries can be found here.


No comments:

Post a Comment