Exposing a Class as a COM+ WCF Service

Remember COM+? Well, it is still around, and, in the days of WCF, it still usefull in certain scenarios, mostly because of the services COM+ offers OOTB (transactions, synchronization, object pooling, etc). For those of you that don't know how, I'm gonna demonstrate how you can expose an ordinary .NET class into both a COM+ component and a regular WCF service. Make sure you have Windows SDK 6.x or greater installed.

First, create a class library project in Visual Studio and add a reference to System.EnterpriseServices and System.ServiceModel assemblies. Then create an interface and a class library, with names IComPlusComponent and ComPlusComponent, for example. Here are its contents:

namespace ComPlus
	[ServiceContract]					//this interface is a WCF interface
	public interface IComponent
		[OperationContract]				//this method is visible to WCF
		String SayHello(String name);

namespace ComPlus
	[ProgId("ComPlus.Component")]				//the progid to be used when instantiating this class via COM
	[ClassInterface(ClassInterfaceType.None)]		//no interface will be generated
	[Transaction(TransactionOption.Supported)]		//our components supports transactions
	[ComDefaultInterface(typeof(IComPlusComponent))]	//the default interface, not really necessary unless you have more than one
	[Synchronization(SynchronizationOption.Required)]	//synchronization is supported
	public class ComPlusComponent : ServicedComponent, IComPlusComponent
		[AutoComplete(true)]				//the method will automatically commit any existing ambient transaction unless an exception is thrown
		public String SayHello(String name)
			return (String.Format("Hello {0} from {1}", name, Environment.MachineName));

Feel free to add your own namespace, by the way, and to generate your own GUIDs, although I guess you can reuse mine.

Next, go to your AssemblyInfo.cs file, add the following attributes and make sure ComVisible is set to true:

[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationName("ComPlus")]
[assembly: ApplicationID("FE8C5234-909E-485E-AF7C-FEFC4729F9DD")]
[assembly: ApplicationAccessControl(AccessChecksLevel = AccessChecksLevelOption.Application, Authentication = AuthenticationOption.None, ImpersonationLevel = ImpersonationLevelOption.Anonymous, Value = false)]

[assembly: ComVisible(true)]

On the project's properties, Build tab, add the following to the Post-build event command line:

  • gacutil /u $(TargetName)
  • gacutil /i $(TargetPath)
  • regsvcs $(TargetPath)

Also on the project's properties, Signing tab, the Sign the assembly checkbox must be checked, and a valid strong name key file must be selected (you can create a new file or reuse an existing one).

Once the project is build and everything is OK, you need to go to the IIS administration and create a virtual path in your default web site. Call it, for example, ComPlus, and have it point to some location in your hard disk, for example, C:\InetPub\WWWRoot\ComPlus.

The final step is running WCF Service Configuration Editor, which ships with Windows SDK, and you can find on the Start Menu, under Microsoft Windows SDK xx\Tools\Service Configuration Editor. Open it and select Integrate on the File menu. Select the ComPlus application and the IComPlusComponent interface of the ComPlus.ComPlusComponent class. Select the SayHello method and the Web hosted hosting mode. Finally, select the IIS virtual directory you created previously (ComPlus).

If all goes well, you can now navigate to http://localhost/ComPlus/ComPlus.ComPlusComponent.svc and you will see the familiar WCF endpoint page. If you need to tweek some parameters, you will find them on the Web.config file under the physical path for the IIS virtual directory.

Bookmark and Share


No Comments