Groups | Blog | Home
all groups > dotnet sdk > june 2005 >

dotnet sdk : Some services stop working when they don`t have work to do.....


Chucker
6/2/2005 9:52:16 AM
I got a Server Class in an executable and I would like to change this in a
windows service. The code from the main function of the executable is the
following:

LoadReportingServer server = new LoadReportingServer ();
server->Start ();

Console::WriteLine (S"Press ENTER to stop the server...");
Console::ReadLine ();

server->Stop ();
server = 0;

I tried to turn this into a service following two tutorials, and it compiles
fine. Whenever I start the Service in the Service Manager, I get the Message
that my service started and stopped again.

Attempt 1:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.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 System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
LoadReportingService() };

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.

server = new LoadReportingServer();
server.start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

Attempt 2:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private ThreadStart threadDelegate;
private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.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 System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
LoadReportingService() };

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.CanPauseAndContinue = true;
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.

server = new LoadReportingServer();
threadDelegate = new ThreadStart(server.Start);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

What am I doing wrong? Can anybody help me? Thanks in advance

Chucker

Willy Denoyette [MVP]
6/3/2005 12:00:00 AM

[quoted text, click to view]

Where is the server.Start method?

Willy.

Chucker
6/3/2005 2:02:02 AM
[quoted text, click to view]
server = new LoadReportingServer();

the method is in the LoadReportingServer class can`t i delegate this directly?

Willy Denoyette [MVP]
6/3/2005 5:28:42 PM

[quoted text, click to view]

There is no need to create another instance of this class you are allready
running on an instance, simply call start();
But most importantly, what are you doing in start().
Note that you have to return from OnStart within 30 seconds, or the SCM will
consider the service has failed to start.

Willy.


AddThis Social Bookmark Button