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 SearchMgrServic e...
Service SearchMgrServic e has been successfully installed.
Creating EventLog source SearchMgrServic e 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.ex e -install"
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Configur ation;
using System.Configur ation.Install;
using System.Runtime. InteropServices ;
using System.Windows. Forms;
using Microsoft.Win32 ;
public class TestService : System.ServiceP rocess.ServiceB ase
{
private System.Componen tModel.Containe r components = null;
public TestService()
{
InitializeCompo nent();
}
static void Main( string[] startArgs )
{
string serviceName = "TestServic e";
try
{
string startOptions = null;
if ( startArgs.Lengt h > 0 )
{
startOptions = startArgs[0].Trim();
}
string assemLocation =
System.Reflecti on.Assembly.Get ExecutingAssemb ly().Location.T oLower();
if ( startOptions != null && startOptions.To Lower() ==
"-install" )
{
System.Configur ation.Install.T ransactedInstal ler
transInstaller = new TransactedInsta ller();
TestServiceInst aller svcInstaller = new
TestServiceInst aller();
transInstaller. Installers.Add( svcInstaller );
string[] cmdLine = { "/assemblypath=" + assemLocation };
System.Configur ation.Install.I nstallContext instContext = new
InstallContext( assemLocation.R eplace(".exe"," .txt") , cmdLine ) ;
transInstaller. Context = instContext ;
transInstaller. Install( new Hashtable() );
System.Windows. Forms.MessageBo x.Show( assemLocation );
Microsoft.Win32 .RegistryKey regKey = Registry.LocalM achine;
try
{
RegistryKey serviceKey = regKey.OpenSubK ey(
"System\\Curren tControlSet\\Se rvices\\TestSer vice" , true
);
serviceKey.SetV alue( "Descriptio n" ,
"Remoting communication manager/interface for local Cisvc
catalogs." );
serviceKey.SetV alue( "Type" , "272" );
serviceKey.Clos e();
}
catch( Exception svcXxx )
{
System.Windows. Forms.MessageBo x.Show( svcXxx.ToString () );
}
finally
{
regKey.Close();
}
}
if ( startOptions != null && startOptions.To Lower() == "-remove"
)
{
System.Configur ation.Install.T ransactedInstal ler
transInstaller = new TransactedInsta ller();
TestServiceInst aller svcInstaller = new
TestServiceInst aller();
transInstaller. Installers.Add( svcInstaller );
String assemPath = String.Format( "/assemblypath=" +
System.Reflecti on.Assembly.Get ExecutingAssemb ly().Location )
;
String[] cmdLine = { assemPath };
InstallContext instContext = new InstallContext( "", cmdLine
);
transInstaller. Context = instContext ;
transInstaller. Uninstall( null );
}
}
catch( Exception xxx )
{
System.Windows. Forms.MessageBo x.Show( xxx.ToString() );
}
System.ServiceP rocess.ServiceB ase[] ServicesToRun =
new System.ServiceP rocess.ServiceB ase[] { new TestService() };
System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
}
private void InitializeCompo nent()
{
components = new System.Componen tModel.Containe r();
this.ServiceNam e = "TestServic e";
this.CanHandleP owerEvent = false;
this.CanPauseAn dContinue = false;
this.CanShutdow n = true;
this.CanStop = true;
this.AutoLog = true;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{
TestHost testHost = new TestHost();
testHost.Listen ();
}
protected override void OnStop()
{
}
}
//[RunInstaller(tr ue)]
public class TestServiceInst aller :
System.Configur ation.Install.I nstaller
{
private ServiceInstalle r serviceInstalle r;
private ServiceProcessI nstaller processInstalle r;
public TestServiceInst aller()
{
processInstalle r = new ServiceProcessI nstaller();
processInstalle r.Account = ServiceAccount. LocalSystem;
serviceInstalle r = new ServiceInstalle r();
serviceInstalle r.ServiceName = "TestServic e";
serviceInstalle r.DisplayName = "Test Service";
serviceInstalle r.StartType = ServiceStartMod e.Automatic;
this.Installers .Add(processIns taller);
this.Installers .Add(serviceIns taller);
}
}
tia
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!