Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am unable to get any results from this service, none of my try/catches are giving me message boxes, nor am I able to debug it due to the installer restrictions.

It installs with installUtil, and will let me start and stop the service.

I'm a junior developer, very much a newb, and have gathered several pieces for this service from "The Code Project" and am hoping you guys can point out my simple/ignorant mistake in the code below.

Thank you in advance for any help!

C#
using System;
using System.Net.Mail;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Windows.Forms;

namespace TestCSWinWatcherService
{
    partial class FTPNotify
    {
       
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

         private void InitializeComponent()
        {
            this.FSWatcherTest = new System.IO.FileSystemWatcher();
            ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).BeginInit();
            // 
            // FSWatcherTest
            // 
            this.FSWatcherTest.EnableRaisingEvents = true;
            this.FSWatcherTest.IncludeSubdirectories = true;
            this.FSWatcherTest.NotifyFilter = System.IO.NotifyFilters.CreationTime | NotifyFilters.LastWrite;
            this.FSWatcherTest.Created += new System.IO.FileSystemEventHandler(this.FSWatcherTest_Created);

            // 
            // FTPNotify
            // 
            try
            {
                this.ServiceName = "FTPNotify";
                ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).EndInit();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error in initializing component " + ex.Message);
            }
        }



        private System.IO.FileSystemWatcher FSWatcherTest;

        /// <summary>
        /// Event occurs when the a File or Directory is created
        /// </summary>
        private void FSWatcherTest_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            MailMessage message = new System.Net.Mail.MailMessage("FileSystemWatcher@second-round.com", ConfigurationManager.AppSettings["EmailDestination"], "File Created " + e.FullPath.ToString(), "The Following File is Created " + Path.GetFileName(e.FullPath.ToString()) + " in " + Path.GetDirectoryName(e.FullPath.ToString()) + " at " + DateTime.Now.ToLongTimeString() + " by " + System.Environment.UserName.ToString());
            SmtpClient mSmtpClient = new SmtpClient("smail.domain.com", 8443);
            mSmtpClient.Credentials = new System.Net.NetworkCredential("user", "password");
            try
            {
                mSmtpClient.Send(message);
                throw new ApplicationException();
            }
            catch(Exception ex) 
            {
                MessageBox.Show("Error Sending email message "+ ex.Message);
            }
        }
        
    }

    public partial class FTPNotify : ServiceBase
    {
        public FTPNotify()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            try
            {
                FSWatcherTest.Path = ConfigurationManager.AppSettings["WatchPath"];
            }
            catch(Exception ex)
            { 
                MessageBox.Show("Error in OnStart of Service " + ex.Message);
            }

        }
    }
}
Posted
Updated 27-Feb-12 18:30pm

1 solution

First of all MessageBox.Show() won't work in windows service. For debugging purposes I suggest you use some logging framework(take a look at log4net). If you want to attach a debugger to a windows service you can use Debug -> Attach to process and find a process under which your service runs or you can programmaticaly attach the debugger(example below):
C#
if(!Debugger.IsAttached)
{
   Debugger.Launch();
}

Maybe you could change the arhitecture of you solution so the actual logic(detection of changes in a folder and e-mail notification) resides in one project, and you create a instances of needed types in other project. This way you start the scanner from console application which will make debugging easier. When you are sure that your scanning and e-mail sending implementation works than you can host it in a windows service.

Uros
 
Share this answer
 
Comments
DFriday13 28-Feb-12 10:14am    
That makes sense. Didn't know that the messagebox wouldn't work in a service. Thanks for the tips! I'm building a windows GUI that lets me launch the task from a button click, and reiterate using a timer now so that i can test all the functionality, then I can try wrapping it in a service(thus negating the need for the timer)
Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900