A customer asked me how to register his Enterprise Service App programmatically as NT-Service. There is no Attribute in the System.EnterpriseServices namespace do accomplish that. But you can do it yourself in an easy way. You have just to implement an own Installer derived class which is doing the registration stuff with help of a little component called RegistrationHelper. RegistrationHelper configures your Enterprise Service App the same way regsvcs.exe does. Additionally we need to use the COM Admin catalog components with its interface ICOMAdminCatalog2 which has the functionality we would like to use: CreateServiceForApplication. To use it in our installation class we have to create a primary interop assembly to keep our signed entity. To create a PIA just use tlbimp /keyfile:xyz.xyz /primary /out:COMAdmin.dll COMAdmin.dll. Following these simple guidelines we have just the following code:

 

[RunInstaller(true)]

public class InstallMyComponent : Installer

{

      public override void Install(System.Collections.IDictionary stateSaver)

      {

            string applicationName = "Enterprise Services App as NT Service";

            string tlbName         = "ESAppAsService.tlb";

            string assemblyName    = Assembly.GetExecutingAssembly().GetName().Name + ".dll";

           

            stateSaver.Add( "ApplicationName", applicationName );

            stateSaver.Add( "TlbName", tlbName );

            stateSaver.Add( "AssemblyName", assemblyName );

           

            RegistrationHelper helper = new RegistrationHelper();

            helper.InstallAssembly( assemblyName, ref applicationName, ref tlbName, InstallationFlags.FindOrCreateTargetApplication );

           

            COMAdmin.ICOMAdminCatalog2 catalog = new COMAdmin.COMAdminCatalogClass();

            catalog.CreateServiceForApplication( applicationName, "ESAppASNTService", "SERVICE_DEMAND_START",

  "SERVICE_ERROR_NORMAL", "", null, null, false );

      }

     

      public override void Rollback(System.Collections.IDictionary savedState)

      {

            RegistrationHelper helper = new RegistrationHelper();

            helper.UninstallAssembly( ( string ) savedState[ "AssemblyName" ],

  ( string ) savedState[ "applicationName" ] );

      }

     

      public override void Commit(System.Collections.IDictionary savedState)

      {

            base.Commit (savedState);

      }

}

 

This is how our installer class looks like. I know, there is no error handling, no uninstall routine and so forth. To register our app in COM+ use installutil.exe instead of regsvcs.exe. Viola!

 

I will update this post as the CodeClip will be available to demonstrate this.