Topshelf : Windows service error

  • Thread starter Thread starter Priya Bange
  • Start date Start date
P

Priya Bange

Guest
Hi Experts,

I have the following Topshelf code which works rarely and most of the time it errors out. Could you please support as am new to C#.

Could you please support with fix, what exactly is going wrong with it. Even if I try to put a single file it creates the same error.

--ConverterService.CS
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileConverterService
{
class ConverterService
{
private FileSystemWatcher _watcher;
private static readonly string InProgressDirectoryName = "processing";

public bool Start()
{
_watcher = new FileSystemWatcher(@"C:\psdata\psdata\in", "*.txt*");

_watcher.Created += FileCreated;

_watcher.IncludeSubdirectories = false;

_watcher.EnableRaisingEvents = true;
_watcher.InternalBufferSize = 32768; // 32 KB


return true;
}

private void FileCreated(object sender, FileSystemEventArgs e)
{


string rootDirectoryPath = new DirectoryInfo(e.FullPath).Parent.Parent.FullName;
string inputFileName = Path.GetFileName(e.FullPath);
string inProgressFilePath =
Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);

File.Move(e.FullPath, inProgressFilePath);

}

public bool Stop()
{
_watcher.Dispose();

return true;
}
}
}

-- Program.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace FileConverterService
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(serviceConfig =>
{
serviceConfig.Service<ConverterService>(serviceInstance =>
{
serviceInstance.ConstructUsing(
() => new ConverterService());

serviceInstance.WhenStarted(
execute => execute.Start());

serviceInstance.WhenStopped(
execute => execute.Stop());
});

serviceConfig.SetServiceName("FileMove");
serviceConfig.SetDisplayName("File Mover");
serviceConfig.SetDescription("File Mover Service");

serviceConfig.StartAutomatically();
});
}
}
}
---------------------------------------
Error
Topshelf.Hosts.ConsoleRunHost Critical: 0 : The service threw an unhandled exception, System.IO.IOException: The process cannot access the file because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost)
at System.IO.File.Move(String sourceFileName, String destFileName)
at FileConverterService.ConverterService.FileCreated(Object sender, FileSystemEventArgs e) in C:\psdata\TestingLocally\FileConverterService\ConverterService.cs:line 39
at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
Stopping the service
Control+C detected, attempting to stop service.

Continue reading...
 
Back
Top