Hi,
I have the following code running as a windows service, but it just
won't work... trying to run the service as 'adminuser', and i have
made sure it logs on as 'adminuser' in the Services console of
Computer Management. There are no errors coming up in the event viewer
either. Any ideas?
################
## Service1.cs
################
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Xml;
using System.Security;
using System.Timers;
namespace testservice
{
public partial class Service1 : ServiceBase
{
Timer timer = new Timer();
public Service1()
{
this.ServiceName = "CCIPVulnerabilityService";
this.EventLog.Log = "Application";
this.CanHandlePowerEvent = false;
this.CanHandleSessionChangeEvent = false;
this.CanPauseAndContinue = false;
this.CanShutdown = false;
this.CanStop = true;
}
public void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
timer.AutoReset = true;
timer.Interval = 120000;
timer.Elapsed += new ElapsedEventHandler(DoWork);
timer.Enabled = true;
timer.Start();
}
public void DoWork(object source, ElapsedEventArgs e)
{
eventLog1.WriteEntry("doing work");
}
protected override void OnStop()
{
eventLog1.WriteEntry("my service stopped");
}
}
}
####################
## testserviceinstaller.cs
####################
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace testservice
{
[RunInstaller(true)]
class testserviceinstaller : Installer
{
public testserviceinstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new
ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new
ServiceInstaller();
serviceProcessInstaller.Account = ServiceAccount.User;
serviceProcessInstaller.Username = "DOMAIN\\adminuser";
serviceProcessInstaller.Password = "ExampleP@55word";
serviceInstaller.DisplayName = "CCIPVulnerabilityService";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "CCIPVulnerabilityService";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}
################
## Service1.Designer.cs
################
namespace testservice
{
partial class Service1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
this.testserviceinstaller1 = new
testservice.testserviceinstaller();
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).BeginInit();
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).EndInit();
}
#endregion
private System.Diagnostics.EventLog eventLog1;
private testserviceinstaller testserviceinstaller1;
}
}