blah blah blah is here! blah blah » Close

up0down
link

I've been tinkering with this windows service for the last few days, and I haven't gotten anywhere.

I got the service created, with the installer, and successfully installed it on the server. It starts and does nothing.

I added a streamwriter to create a little log file for myself to see where the hangup was. It creates the file, but doesn't have anything written to it, which means it never entered the OnStart event.

Some service examples I've seen show a main() method that initializes stuff. Something like:


static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
//Change the following line to match.

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



But when I put that in, it tells me that I have "more than one entry point defined"

Anyone have any hints to what I'm doing wrong? Here's my code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Threading;

namespace TestService
{
public partial class Service1 : ServiceBase
{
StreamWriter myFile = new StreamWriter(@"C:\Temp\Log.txt");
private SqlConnection cn;
public string connectionString = "Server=RMSPROD02; Database=WebRMSLinked;Trusted_Connection=True; Min Pool Size =8; Max Pool Size=15; Connection Reset = True; Connection Timeout = 200; Connection Lifetime=0;";

private Timer serviceTimer;

protected override void OnStart(string[] args)
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

base.OnStart(args);
myFile.WriteLine("In OnStart");
TimerCallback timerDelegate = new TimerCallback(DoWork);

// create timer and attach our method delegate to it
serviceTimer = new Timer(timerDelegate, null, 1000, 5000);

}

protected override void OnStop()
{
base.OnStop();
myFile.WriteLine("In OnStop");
}

private void DoWork(object state)
{
myFile.WriteLine("In OnTimer");
cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd = new SqlCommand("DingAgent", cn);
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@AgentName", Environment.MachineName);
cmd.ExecuteNonQuery();

}
}
}


And my installer

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


namespace TestService
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
}

last answered 2 years ago

1 answers

up0down
link

If you look at the Main() method in your program.cs file, then you should find that VS has already written the following code (or something like it) into it:

static void Main()
{
ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new ServiceBase[] { new Service1() };

ServiceBase.Run(ServicesToRun);
}

So, you therefore need to remove the following lines from your OnStart() method and then rebuild the service:
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

Feedback