Service Transacted Installation Problem

  • Thread starter Thread starter poi
  • Start date Start date
P

poi

Running the code below, I get the error

Cannot start service from the command line opr debugger. A Windows
Service must first be installed (using installutil.exe) and then started
with the ServerExplorer.....


But the text log from my install shows this:

Running a transacted installation.

Beginning the Install phase of the installation.
Installing service SearchMgrService...
Service SearchMgrService has been successfully installed.
Creating EventLog source SearchMgrService in log Application...

The Install phase completed successfully, and the Commit phase is
beginning.

The Commit phase completed successfully.

The transacted install has completed.



But still the service will not start! I get errors in the event log as
well.



Below is the code, run with "testservice.exe -install"

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;



public class TestService : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.Container components = null;

public TestService()
{
InitializeComponent();
}

static void Main( string[] startArgs )
{
string serviceName = "TestService";
try
{
string startOptions = null;
if ( startArgs.Length > 0 )
{
startOptions = startArgs[0].Trim();
}

string assemLocation =
System.Reflection.Assembly.GetExecutingAssembly().Location.ToLower();

if ( startOptions != null && startOptions.ToLower() ==
"-install" )
{
System.Configuration.Install.TransactedInstaller
transInstaller = new TransactedInstaller();
TestServiceInstaller svcInstaller = new
TestServiceInstaller();
transInstaller.Installers.Add( svcInstaller );
string[] cmdLine = { "/assemblypath=" + assemLocation };
System.Configuration.Install.InstallContext instContext = new
InstallContext( assemLocation.Replace(".exe",".txt") , cmdLine ) ;
transInstaller.Context = instContext ;
transInstaller.Install( new Hashtable() );

System.Windows.Forms.MessageBox.Show( assemLocation );

Microsoft.Win32.RegistryKey regKey = Registry.LocalMachine;
try
{
RegistryKey serviceKey = regKey.OpenSubKey(
"System\\CurrentControlSet\\Services\\TestService" , true
);
serviceKey.SetValue( "Description" ,
"Remoting communication manager/interface for local Cisvc
catalogs." );
serviceKey.SetValue( "Type" , "272" );
serviceKey.Close();
}
catch( Exception svcXxx )
{
System.Windows.Forms.MessageBox.Show( svcXxx.ToString() );
}
finally
{
regKey.Close();
}
}
if ( startOptions != null && startOptions.ToLower() == "-remove"
)
{
System.Configuration.Install.TransactedInstaller
transInstaller = new TransactedInstaller();
TestServiceInstaller svcInstaller = new
TestServiceInstaller();
transInstaller.Installers.Add( svcInstaller );
String assemPath = String.Format( "/assemblypath=" +
System.Reflection.Assembly.GetExecutingAssembly().Location )
;
String[] cmdLine = { assemPath };
InstallContext instContext = new InstallContext( "", cmdLine
);
transInstaller.Context = instContext ;
transInstaller.Uninstall( null );
}
}
catch( Exception xxx )
{
System.Windows.Forms.MessageBox.Show( xxx.ToString() );
}

System.ServiceProcess.ServiceBase[] ServicesToRun =
new System.ServiceProcess.ServiceBase[] { new TestService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}



private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "TestService";
this.CanHandlePowerEvent = false;
this.CanPauseAndContinue = false;
this.CanShutdown = true;
this.CanStop = true;
this.AutoLog = true;
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

protected override void OnStart(string[] args)
{
TestHost testHost = new TestHost();
testHost.Listen();
}

protected override void OnStop()
{

}
}






//[RunInstaller(true)]
public class TestServiceInstaller :
System.Configuration.Install.Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;

public TestServiceInstaller()
{
processInstaller = new ServiceProcessInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;

serviceInstaller = new ServiceInstaller();
serviceInstaller.ServiceName = "TestService";
serviceInstaller.DisplayName = "Test Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;

this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}

tia
 
Back
Top