Windows Service发送关于file的电子邮件使用SystemFileWatcher创建 [英] Windows Service to send email on fileCreated using SystemFileWatcher

查看:195
本文介绍了Windows Service发送关于file的电子邮件使用SystemFileWatcher创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从此服务获得任何结果,我的try/catches都没有给我消息框,也由于安装程序的限制,我也无法对其进行调试.

它与installUtil一起安装,并且可以让我启动和停止该服务.

我是一名初级开发人员,非常新手,并且已经从代码项目"中收集了一些服务内容,希望你们能在下面的代码中指出我的简单/无知错误.

预先感谢您的帮助!

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!

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);
            }

        }
    }
}

推荐答案

首先,MessageBox.Show()在Windows服务中不起作用.出于调试目的,我建议您使用一些日志记录框架(请参阅log4net).如果要将调试器附加到Windows服务,则可以使用Debug->附加到进程并找到运行服务的进程,或者可以通过编程方式附加调试器(以下示例):
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):
if(!Debugger.IsAttached)
{
   Debugger.Launch();
}


也许您可以更改解决方案的结构,以使实际逻辑(检测文件夹中的更改和电子邮件通知)驻留在一个项目中,并在另一个项目中创建所需类型的实例.这样,您可以从控制台应用程序启动扫描仪,这将使调试更加容易.如果确定可以执行扫描和电子邮件发送实施,则可以将其托管在Windows服务中.

乌鲁斯人


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


这篇关于Windows Service发送关于file的电子邮件使用SystemFileWatcher创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆