I have a service process that contains two services but only one of them ever
works properly. The background is I have two classes which are Individual()
and Mass() in the main service class and they both inherit ServiceBase. The
main Method is as follows:
static void Main()
{
System.ServiceP rocess.ServiceB ase[] ServicesToRun;
ServicesToRun = new System.ServiceP rocess.ServiceB ase[]
{
new MassService(),
new IndividualServi ce()
};
System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
}
When I install the service and start it only the Mass service perfoms any
action and nothing occurs in the Individual service. I have tried creating
installers for each service and the same result occurs. I know the Individual
service works because if I reverse the order in Main() then Mass does not
work. Here is the code and both Mass and Indivudal are the same:
public class MassService : ServiceBase
{
private const string RETRY_MAX_TIME_ EXCEEDED = "The file \"{0}\" could not
be processed because a timeout occurred while waiting for the file to finish
uploading.";
private const string SUCCESS_EVENT_M SG = "The File \"{0}\" was successfully
procesed and has been deleted from the file system at \"{1}\"";
private const string FAILED_TO_ADD_T O_QUEUE = "The file \"{0}\" was not
added to the queue.";
private const string INBOUND_FILE_CO MPLETE_MSG = "The file \"{0}\" from IMDS
CDB was suucesfuly created at \"{1}\".";
private const string CLEANUP_EXCEPTI ON_MSG = "An error occured while
performing cleanup. The exception was: \"{0}\"";
private const string MASS_FSO_WATCHE R_EXCEPTION_MSG = "Error creating the
FileSystemWatch er object. The exception was: \"{0}\"";
private const string APP_CONFIG_EXCE PTION_MSG = "Error reading the
application configuration file. The exception was: \"{0}\"";
private const int RETRY_MAX_TIME = 3600000; // 1 hour
private const int RETRY_DELAY = 10000; // 10 seconds
private System.Diagnost ics.EventLog _eventLog = null;
private System.IO.FileS ystemWatcher _fileSystemWatc her = null;
private void InitializEventL og()
{
string _source = "MyApp Library Logging";
string _log = "MyApp";
if (!EventLog.Sour ceExists(_sourc e))
{
EventLog.Create EventSource(_so urce, _log);
}
_eventLog = new EventLog(); //Create Event Log
_eventLog.Log = _log; //Assign Event Log Name
_eventLog.Sourc e = _source; //Assign Event Source Name
}
private void IntializeFileSy stemWatcher()
{
string _massFileStore = string.Empty;
string _fileType = string.Empty;
try
{
_massFileStore = ConfigurationMa nager.AppSettin gs["MassXmlFileSto re"];
_fileType = ConfigurationMa nager.AppSettin gs["FileType"];
}
catch (Exception ex)
{
_eventLog.Write Entry(APP_CONFI G_EXCEPTION_MSG +
ex.Message.ToSt ring());
}
try
{
_fileSystemWatc her = new FileSystemWatch er(_massFileSto re, _fileType);
}
catch (Exception ex)
{
_eventLog.Write Entry(MASS_FSO_ WATCHER_EXCEPTI ON_MSG +
ex.Message.ToSt ring());
}
_fileSystemWatc her.EnableRaisi ngEvents = true; // Begin watching.
_fileSystemWatc her.IncludeSubd irectories = true; // Monitor Sub Folders
_fileSystemWatc her.NotifyFilte r = NotifyFilters.D irectoryName |
NotifyFilters.F ileName; // Apply filters
// Add event handlers for new XML files and deletion of existing XML
files.
_fileSystemWatc her.Created += new
FileSystemEvent Handler(OnXMLFi leCreated);
_fileSystemWatc her.Deleted += new
FileSystemEvent Handler(OnXMLFi leDeleted);
}
protected override void OnStart(string[] args)
{
InitializEventL og(); //Initialize Event Log
IntializeFileSy stemWatcher(); //Initialize File System Watcher
}
protected override void OnStop()
{
_fileSystemWatc her.Dispose();
_eventLog.Dispo se();
}
private void OnXMLFileDelete d(object source, FileSystemEvent Args args)
{
string _filename = args.FullPath;
DateTime _deletedAt = DateTime.Now;
_eventLog.Write Entry(String.Fo rmat(SUCCESS_EV ENT_MSG, _filename,
_deletedAt));
}
private void OnXMLFileCreate d(object source, FileSystemEvent Args args)
{
string _filename = args.FullPath;
DateTime _receivedAt = DateTime.Now;
_eventLog.Write Entry(String.Fo rmat(INBOUND_FI LE_COMPLETE_MSG , _filename,
_receivedAt));
}
private void FileCreated(obj ect sender, FileSystemEvent Args args)
{
string filename = args.FullPath;
DateTime receivedAt = DateTime.Now;
bool timedOut = false;
bool processed = false;
while (!(timedOut || processed))
{
if (FileUploadComp leted(filename) )
{
PassOffFileToOr acle(filename);
processed = true;
}
else
{
TimeSpan timeElapsed = DateTime.Now - receivedAt;
if (timeElapsed.To talMilliseconds RETRY_MAX_TIME)
{
timedOut = true;
}
else
{
Thread.Sleep(RE TRY_DELAY);
}
}
}
if (timedOut)
{
_eventLog.Write Entry(String.Fo rmat(RETRY_MAX_ TIME_EXCEEDED,
filename));
}
}
private bool FileUploadCompl eted(string filename)
{
try
{
using (FileStream inputStream = File.Open(filen ame, FileMode.Open,
FileAccess.Read ,
FileShare.None) )
{
return true;
}
}
catch (IOException)
{
return false;
}
}
private void PassOffFileToOr acle(string fileName)
{
// Add the file to the queue
FileQueue _queue = new FileQueue();
_queue.AddFileT oQueue(fileName );
if (!_queue.AddFil eToQueue(fileNa me))
try
{
_queue.GetAndRe moveTopMostFile Name();
// Oracle DAL Layer here
}
catch (Exception)
{
_eventLog.Write Entry(String.Fo rmat(FAILED_TO_ ADD_TO_QUEUE,
fileName));
}
}
private void CleanUpFile(str ing fileName)
{
string _massFileStore = string.Empty;
try
{
_massFileStore = ConfigurationMa nager.AppSettin gs["MassXmlFileSto re"];
System.IO.File. Delete(_massFil eStore + @"\" + fileName);
}
catch (Exception ex)
{
_eventLog.Write Entry(CLEANUP_E XCEPTION_MSG + ex.Message.ToSt ring());
}
}
}
I have created one installer and the services panel shows both and I can
start both but nothing is occuruing when it comes to the Individual service
but the Mass service works perfectly.
Any ideas why both services are not working?