COM Calling WCF

Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any questions.

WCF can not be directly called by VB6; we need to create a .NET bridge for it.  Below is how to create that bridge.

Create a .NET Class Library project

1. What all functionality are intended to be used in COM, create an interface for it.  The interface should be public and with the [ComVisible(true)] attribute.  If WCF is to be called, then this interface will be same as the contract interface in WCF (except attributes OperationContract and ServiceContract will not be added in .NETBridge interface.)
2. Create .NET class implementing this interface. (Here write all functionality i.e. logic, call service, hit db …)
3. Class should be public and should have following attributes
   a. [ClassInterface(ClassInterfaceType.None)]
   b. [Guid("<A GUID>")]
   c. [ComVisible(true)]
4. In Visual Studio, right click the project node in Solution Explorer.  Go to Properties -> Signing
5. Check Sign the assembly. Create a new key (using the dropdown control).
6. Build your application.
7. Open Visual Studio command prompt.
8. Use regasm /tlb:<urDesiredTLB>.tlb  <urDLL>.dll   i.e.
      regasm /tlb: wcfCaller.tlb wcfCaller.dll
to generate the TLB file.

(Optional but recommended) in Visual Studio 6 tools, open OLE View and check your TLB.  It should contain your functions, and other stuff, dont worry about the format it looks.
9. The regasm utility will create a type library and register it in your Windows registry to make the classes in wcfCaller.dll available to COM clients.
10. Register to GAC gacutil /i wcfCaller.dll

Create a VB6 project

1. Create a vb project
2. To use .NET component, go to Project -> References. Search for your .NET component. Select it.
3. Create a new object of .NET class and call functions. use createObject for creating instance of .net component.

Issues Faced and Solutions Found

VB6 giving Automation error : - VB6 is not able to create instance of .NET class, your class must have empty constructor.
Unable to connect with web service: - .NET component calls web service by help of entries in its app.config. When service reference is added, it updates app.config. But when you register .NET component in gac it does not takes its config with it.

To make it able to connect with webservice, copy you .net component app.config and place it with VB6 exe with name .exe.config. Reason is dll executes inside calling process hence can access calling exe's config.

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.