Topshelf/Topshelf — Issue #80: Service not stopped when deleting folder
Author: mpareja
Created: 2011-12-09 03:11:51+00:00
State: open

I've attached the logs below, but here is what I think is happening.

1. A service sub-directory is deleted.
2. NLog is crashing because it is trying to watch for config changes in a directory that no longer exists. (I presume this error is coming from my use of NLog inside my service. NLog should probably handle this more cleanly.)
3. TopShelf notices the Exception but doesn't seem to do anything about it.
4. TopShelf notices the folder has been removed but it does not try and stop the service. (Perhaps since it is in an error state?)
5. Later, a service sub-directory is added with the same name and TopShelf tries to create a shelf.
6. Since the previous service instance hasn't been stopped and TopShelf is trying to start the service again, it runs into a pipe collision.

```text
INFO 2011-11-14 05:38:13 - (demo.our_site.com-SystemJobs) Created
INFO 2011-11-14 05:38:13 - (beta.our_site.com-SystemJobs) Created
INFO 2011-11-14 05:38:14 - (demo.our_site.com-SystemJobs) Running
INFO 2011-11-14 05:38:14 - (beta.our_site.com-SystemJobs) Running
FATAL 2011-11-15 04:28:41 - An unhandled exception occurred within the service: beta.our_site.com-SystemJobs
System.ArgumentException: The directory name d:\domains\topshelf\services\beta.our_site.com-systemjobs is invalid.
at System.IO.FileSystemWatcher.set_Path(String value)
at NLog.Internal.MultiFileWatcher.Watch(String fileName)
at NLog.Internal.MultiFileWatcher.Watch(ICollection fileNames)
at NLog.LogFactory.ReloadConfigOnTimer(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._TimerCallback.PerformTimerCallback(Object state)
INFO 2011-11-15 04:28:41 - (beta.our_site.com-SystemJobs) Fault
ERROR 2011-11-15 04:28:41 - Fault on beta.our_site.com-SystemJobs: Topshelf.Exceptions.TopshelfException
An unhandled exception occurred within the service: beta.our_site.com-SystemJobs
The directory name d:\domains\topshelf\services\beta.our_site.com-systemjobs is invalid. at System.IO.FileSystemWatcher.set_Path(String value)
at NLog.Internal.MultiFileWatcher.Watch(String fileName)
at NLog.Internal.MultiFileWatcher.Watch(ICollection fileNames)
at NLog.LogFactory.ReloadConfigOnTimer(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._TimerCallback.PerformTimerCallback(Object state)

HelpLink:

INFO 2011-11-15 04:28:55 - (beta.our_site.com-SystemJobs) FolderRemoved
INFO 2011-11-15 04:28:55 - [Topshelf] Folder Removed: beta.our_site.com-SystemJobs
INFO 2011-11-15 04:32:34 - (beta.our_site.com-SystemJobs) FolderChanged
INFO 2011-11-15 04:32:34 - [Topshelf] Folder Changed: beta.our_site.com-SystemJobs
INFO 2011-11-15 04:32:34 - [Topshelf] Create Shelf Service: beta.our_site.com-SystemJobs
ERROR 2011-11-15 04:32:39 - cannot create shelf
System.InvalidOperationException: The ChannelDispatcher at 'net.pipe://localhost/topshelf_2356/controller/beta.our_site.com-SystemJobs/2356/beta.our_site.com-SystemJobs' with contract(s) '"WcfChannelOf_WcfMessageEnvelope"' is unable to open its IChannelListener. ---> System.InvalidOperationException: A registration already exists for URI 'net.pipe://localhost/topshelf_2356/controller/beta.our_site.com-SystemJobs/2356/beta.our_site.com-SystemJobs'.
at System.ServiceModel.Channels.UriPrefixTable`1.RegisterUri(Uri uri, HostNameComparisonMode hostNameComparisonMode, TItem item)
at System.ServiceModel.Channels.ConnectionOrientedTransportManager`1.Register(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.NamedPipeChannelListener`2.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
--- End of inner exception stack trace ---
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at Stact.WcfChannelHost..ctor(Fiber fiber, UntypedChannel output, Uri serviceUri, String pipeName) in d:\Home\Stact\src\Stact\Channels\WcfChannelHost.cs:line 45
at Topshelf.Model.HostChannel..ctor(UntypedChannel output, Uri address, String pipeName) in d:\BuildAgent-01\work\799c08e77fef999d\src\Topshelf\Model\HostChannel.cs:line 63
at Topshelf.Model.HostChannelFactory.CreateShelfControllerHost(UntypedChannel controllerChannel, String serviceName) in d:\BuildAgent-01\work\799c08e77fef999d\src\Topshelf\Model\HostChannelFactory.cs:line 39
at Topshelf.Model.ShelfReference.CreateShelfInstance(Type bootstrapperType) in d:\BuildAgent-01\work\799c08e77fef999d\src\Topshelf\Model\ShelfReference.cs:line 97
at Topshelf.Model.ShelfServiceController.Create() in d:\BuildAgent-01\work\799c08e77fef999d\src\Topshelf\Model\ShelfServiceController.cs:line 92
INFO 2011-11-15 04:32:40 - (beta.our_site.com-SystemJobs) Fault
ERROR 2011-11-15 04:32:40 - Fault on beta.our_site.com-SystemJobs: Topshelf.Exceptions.BuildServiceException
Couldn't build 'beta.our_site.com-SystemJobs'.
```text