RE: windows services in .net, auto-restarting?



Hi Jeffrey...

A couple of us spent a long time googling around on the subject yesterday
and found some of your other posts on other groups (there isn't a clear
favorite group for this kind of question from the titles).

We've got a service using .Net 2.0. At first, the worker thread caught
exceptions and quietly exited which was not the optimal solution since that
left the process alive doing no work. Our next attempt was calling the SCM
via the native method to stop the service from the worker thread. But then
we found that the auto restart didn't kick in. Tried the same thing with
Stop(), creating the ServiceController object and stopping through that, etc.
As you say, the net result was that the SCM considered it a clean stop no
matter what we set the exit code to.

I thought one of the other guys tried throwing an uncaught exception from
the worker thread without success. Ultimately, we settled on
Environment.Exit() from the worker thread; that seemed to do the trick. As
long as we aren't hosting multiple services in the process, it seemed to fit
the bill.

Thanks
Mark


""Jeffrey Tan[MSFT]"" wrote:

Hi Mark,

Based on my understanding, you wanted to allow the service to leverage the
auto-restarting configuration while exceptions are generated in the
background worker thread. If I have misunderstood you, please feel free to
tell me, thanks.

Do you use .Net1.1 or .Net2.0 Windows Service? Do you catch the crash
exception in your thread proc? Based on my test in a VS2005 service with
auto-restarting configuration, if there is any unhandled exception
generated in the worker thread(without catching), the service process will
terminate and after 1 minite, the SCM will try to restart it again. My test
code is very simple, listed below:

private void ThreadProc()
{
throw new Exception("abc");
}

protected override void OnStart(string[] args)
{
System.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(ThreadProc));
t.Start();
}

Based on my experience, the SCM will take action on the auto-restarting
feature if the service fails. A service fails if its process dies without
the service setting its state to SERVICE_STOPPED. This is the key point.

If the exception is not caught in the ThreadProc, it will certainly crash
the service process without setting status to SERVICE_STOPPED. So the
auto-restarting will take effect.

If you have caught the exception, the Stop() method will not take effect in
our scenario. This is because Stop() method internally calls DeferredStop()
method which uses SetServiceStatus with setting SERVICE_STOPPED(Note
SERVICE_STOPPED is of value 1 in const):

public void Stop()
{
this.DeferredStop();
}

private unsafe void DeferredStop()
{
fixed (NativeMethods.SERVICE_STATUS* service_statusRef1 =
&this.status)
{
int num1 = this.status.currentState;
this.status.checkPoint = 0;
this.status.waitHint = 0;
this.status.currentState = 3;
NativeMethods.SetServiceStatus(this.statusHandle,
service_statusRef1);
try
{
this.OnStop();
this.WriteEventLogEntry(Res.GetString("StopSuccessful"));
this.status.currentState = 1;//This is SERVICE_STOPPED
NativeMethods.SetServiceStatus(this.statusHandle,
service_statusRef1);
....
}
catch (Exception exception2)
{
....
}
}
}
Also, the service main thread ServiceMainCallback will also call
SetServiceStatus with SERVICE_STOPPED. So if the main thread dies normally
or Stop() method is called, the service is recognized as exiting without
failure.

So, to achieve your requirement, we can not allow the main thread to exit
normally, one workaround is catching the exception in ThreadProc and does
the cleanup and save work in the catch handler. Finally, you may invoke
"throw;" keyword again to rethrow the exception. This will force the
service to die as failure, which allow the SCM to auto-restart the service
based on configuration.

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • RE: "not handled in user code" dialog
    ... How do you implement the global error handler for Asp.net application? ... to handle all the unhandled exception in main Asp.net thread. ... exception you have seen is generated in a worker thread. ... Microsoft Online Community Support ...
    (microsoft.public.vsnet.debugging)
  • RE: Backup & Monitoring not working!
    ... Error message in Windows Small Business Server 2003: ... Microsoft CSS Online Newsgroup Support ... This newsgroup only focuses on SBS technical issues. ... An unhandled exception occurred during the execution of the ...
    (microsoft.public.windows.server.sbs)
  • Re: DropDownList has SelectedValue which in invalid
    ... From your description you want to avoid getting the exception: ... 'ddlAssignedTo' has a SelectedValue which is invalid because it does not ... Microsoft Online Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: "not handled in user code" dialog
    ... to handle all the unhandled exception in main Asp.net thread. ... exception you have seen is generated in a worker thread. ... Microsoft Online Community Support ...
    (microsoft.public.vsnet.debugging)
  • If youre still interested...
    ... When calling the code below we get an Exception "Exception from HRESULT: ... call any Excel COM methods so it must be in this code.) ... values in the UDF definition, the error "800A03EC" will be thrown. ... Microsoft Online Community Support ...
    (microsoft.public.office.developer.com.add_ins)

Loading