Stefan Soljemo

Im working with a .NET project that contains four processes running as services. Those processes using more than 10 database connections to SQL Server or Oracle. My customer wants the system to allow rebbot of servers during production. This mean that the process need to terminate in a controlled way. My service is coded as below and does not terminate in a controlled way. The System calls my OnShutdown method who stop a TCP server in a controled way and disconnect all database connections and close open cursors. The problem is that the Main will hangs at the System.ServiceProcess.ServiceBase.Run method. This cause the SCM to kill my process and the database connection in the process connection pool will remain opened in the Oracle server.

I get problem to start myprocesses after the reboot because all available database conenction in Oracle is busy.

Here is some code.

public C_MMCSvc()

{

ServiceName = "MMC_Server";

this.CanStop = true;

this.CanShutdown = true;

}

static void Main()

{

System.ServiceProcess.ServiceBase oServiceHanlder = new C_MMCSvc();

System.ServiceProcess.ServiceBase.Run( oServiceHanlder);

}

protected override void OnStart(string[] args)

{

m_oMMC = new C_MMC();

m_oMMCThread = new Thread(new ThreadStart( m_oMMC.Run));

m_oMMCThread.Start();

}

protected override void OnStop()

{

StopService();

}

{

StopService();

}

private void StopService()

{

m_oMMC.Stop();

m_oMMCThread.Join();

Thread.Sleep( 500);

}




Re: .NET Base Class Library .NET service does not stop normally during shutdown

nobugz

Your code doesn't show the implementation of C_MMC.Stop(). Surely, that's where the problem is, the thread won't stop and the Join() call just hangs. Thread.Abort() might work. Or not if the thread is blocked on some unmanaged code in the Oracle data provider. Hard to tell...





Re: .NET Base Class Library .NET service does not stop normally during shutdown

Stefan Soljemo

Thank for answer. Im in really trouble. The code below shows the implement of the m_oMMC.Stop. It make a call to the base class that itterate all connected cliens to its TCP server, then it set the ListenActive flag to zero. this flag hold the system main thread loop shown last in this post. The method Stop returns sucessfully. Note that the server stops in a controled way when stopping the service with the SCM panel.

public void Stop()

{

StopServer();

m_oMMCListenActive = false;

}

m_oMMCListenActive = true;

while( m_oMMCListenActive)

{

Thread.Sleep( 1000);

}






Re: .NET Base Class Library .NET service does not stop normally during shutdown

nobugz

Okay, if you're pretty sure that OnStop() normally works well, it must have something to do with the shutdown. Perhaps your service critically depends on other services that have already stopped by the time your OnStop() starts running. Thus gumming up your StopService() method. You'd normally manage that with service dependencies. Wild guess by the way, I'd recommend you add tracing so you really know what is going on.





Re: .NET Base Class Library .NET service does not stop normally during shutdown

Stefan Soljemo

Im traced the Service with remote debugging and im shure that all functions succeeded except that the void main does not return. Is there any way to force the blocked call to the System.ServiceProcess.Run at main to return The strange thing is that is does when stopping the service in normal way.






Re: .NET Base Class Library .NET service does not stop normally during shutdown

nobugz

The Run() method is blocking on the StartServiceCtrlDispatcher() API function. It is documented to not return "until all running services in the process have terminated". Are you sure the Join() call returns Why would you have Oracle connections left open after you properly stopped the service

I'm out of ideas otherwise...





Re: .NET Base Class Library .NET service does not stop normally during shutdown

Stefan Soljemo

I created a new service without any database connection. Simply one .cs file with a lot of tracing to a log file. The output show that the Main will remain running until the system kills the process. Now we only have to things left to consider.

  1. Is there any resourses allocated by .NET framework behind the sienes.
  2. Do we have problem with out test server.

Here is the output from the code below when starting and stopping the service from SCM:
22:34:09 Main Called
22:34:09 OnStart Called, and Returns
22:34:29 OnStop Called
22:34:29 StopService Called
22:34:30 StopService Returns
22:34:30 OnStop Returns
22:34:30 Dispose Called
22:34:30 Dispose Returns
22:34:30 Main Returns

Here is the output when rebooting the computer
22:35:25 Main Called
22:35:25 OnStart Called, and Returns
22:36:05 OnShutdown Called
22:36:05 StopService Called
22:36:06 StopService Returns
22:36:06 OnShutdown Returns

And the code:

public class C_TstSvc : ServiceBase
{
public C_TstSvc()
{
ServiceName = "Test_Server";
this.CanStop = true;
this.CanShutdown = true;
}

static void Main()
{
Log( "Main Called");
System.ServiceProcess.ServiceBase oServiceHanlder = new C_TstSvc();
System.ServiceProcess.ServiceBase.Run( oServiceHanlder);
Log( "Main Returns");
}

protected override void OnStart(string[] args)
{
Log( "OnStart Called, and Returns");
}

protected override void OnStop()
{
Log( "OnStop Called");
StopService();
Log( "OnStop Returns");
}

protected override void OnShutdown()
{
Log( "OnShutdown Called");
StopService();
Log( "OnShutdown Returns");
}

private void StopService()
{
Log( "StopService Called");
Thread.Sleep( 500);
Log( "StopService Returns");
}

static private void Log( string sMsg)
{
StreamWriter m_oFileWriter;
m_oFileWriter = File.AppendText( "c:\\Aniware\\Test\\TestService.log");
m_oFileWriter.NewLine = "\r\n";
m_oFileWriter.WriteLine( DateTime.Now.ToLongTimeString() + " " + sMsg);
m_oFileWriter.Close();
}

protected override void Dispose(bool disposing)
{
Log( "Dispose Called");
base.Dispose (disposing);
Log( "Dispose Returns");
}