Hello Nick,
I'm not sure why your service sometimes doesn't stop running, because I
don't know the details inside your Server class... However, I can provide
some information regarding the ServiceBase.OnStop method and the
AppDomain.Unload method, hope these information will help you on the issue.
The first thing I need to mention is the AppDomain.Unload method.
If you're developing your service on .NET 2.0 or higher, you should notice
the Remarks section in the Unload method's document:
** Begin Quote **
In the .NET Framework version 2.0 there is a thread dedicated to unloading
application domains. This improves reliability, especially when the .NET
Framework is hosted. When a thread calls Unload, the target domain is
marked for unloading. The dedicated thread attempts to unload the domain,
and all threads in the domain are aborted. If a thread does not abort, for
example because it is executing unmanaged code, or because it is executing
a finally block, then after a period of time a
CannotUnloadAppDomainException is thrown in the thread that originally
called Unload. If the thread that could not be aborted eventually ends, the
target domain is not unloaded. Thus, in the .NET Framework version 2.0
domain is not guaranteed to unload, because it might not be possible to
terminate executing threads.
** End Quote **
That means even you call the AppDomain.Unload(_domain), the "_domain" could
be failed to be unloaded. And there could be a
CannotUnloadAppDomainException or other exceptions raised, and since your
OnStop method doesn't handle any exceptions, the stop operation will fail
in case of any exceptions.
Besides the Unload method, is it possible that the Server.Stop method
raises any exceptions?
And since you call the Service.Stop async in OnStopped, it is also possible
that a ThreadAbortException being raised when code in Server is still
executing and the AppDomain.Unload starts to kill the threads in _domain.
Have you looked at the Application Logs in the Event Viewer? Is there any
error regarding exceptions in the OnStop method?
So could you try just remove the AppDomain.Unload from the OnStop code? If
your application is a hosted service, the AppDomain that run the service
will be unloaded automatically, hence everything inside your AppDomain
including your own "_domain" will be released by the system. You don't have
to manually unload it unless I missed something here.
Please check the log for any exceptions during OnStop and try remove the
Unload, and let me know if that works for you.
Thanks,
Jie Wang
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days 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. 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/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.