【问题标题】:The service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs本地计算机上的服务启动然后停止。如果某些服务没有被其他服务或程序使用,它们会自动停止
【发布时间】:2014-05-06 14:51:58
【问题描述】:
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
    private ServiceProcessInstaller serviceProcessInstaller;
    private ServiceInstaller serviceInstaller;

    public ProjectInstaller()
    {
        serviceProcessInstaller = new ServiceProcessInstaller();
        serviceInstaller = new ServiceInstaller();
        // Here we can set properties on serviceProcessInstaller
        //or register event handlers
        serviceProcessInstaller.Account = ServiceAccount.LocalService;

        serviceInstaller.ServiceName = MyNewService.MyServiceName;
        this.Installers.AddRange(new Installer[] {
            serviceProcessInstaller, serviceInstaller });
    }

    private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {

    }
}



public partial class MyNewService : ServiceBase
{

    FileSystemWatcher myWatcher = new FileSystemWatcher("C:\\Users\\Ahmed\\Desktop\\demo");
    public const string MyServiceName = "MyNewService";
    private FileSystemWatcher watcher = null;


    public MyNewService()
    {
        InitializeComponent();
        myWatcher.NotifyFilter = NotifyFilters.LastAccess
           | NotifyFilters.LastWrite
           | NotifyFilters.FileName
           | NotifyFilters.DirectoryName;
    }
    //private static void OnChanged(object source, FileSystemEventArgs e)
    //{

    //    WatcherChangeTypes wct = e.ChangeType;
    //    Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
    //}
    protected void FileCreated(object sender, FileSystemEventArgs e)
    {
        if (e.ChangeType == WatcherChangeTypes.Created)
        {
            if (Directory.Exists(e.FullPath))
            { Console.WriteLine("Directory Exists"); }            // a directory
            else { Console.WriteLine("File"); }
            // a file
        }
    }


    protected override void OnStart(string[] args)
    {

        this.ServiceName = MyServiceName;


        FileSystemWatcher watcher = new FileSystemWatcher("C:\\Users\\Ahmed\\Desktop\\demo", "*.txt");

        //Watch for changes in LastAccess and LastWrite times, and
        //the renaming of files or directories.
        watcher.NotifyFilter = NotifyFilters.LastAccess
                             | NotifyFilters.LastWrite
                             | NotifyFilters.FileName
                             | NotifyFilters.DirectoryName;

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }

    protected override void OnStop()
    {
        watcher.EnableRaisingEvents = false;
        watcher.Dispose();

        LogEvent("Monitoring Stopped");
    }

    void OnChanged(object sender, FileSystemEventArgs e)
    {
        string mgs = string.Format("File {0} | {1}",e.FullPath, e.ChangeType);
        LogEvent(mgs);
    }

    void OnRenamed(object sender, RenamedEventArgs e)
    {
        string log = string.Format("{0} | Renamed from {1}",
                                   e.FullPath, e.OldName);
        LogEvent(log);
    }
    private void LogEvent(string message)
    {
        string eventSource = "File Monitor Service";
        DateTime dt = new DateTime();
        dt = System.DateTime.UtcNow;
        message = dt.ToLocalTime() + ": " + message;

        EventLog.WriteEntry(eventSource, message);
    }

    private void eventLog1_EntryWritten(object sender, EntryWrittenEventArgs e)
    {

    }
}


static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new MyNewService() 
        };
        ServiceBase.Run(new ServiceBase[] { new MyNewService() });
    }
}

【问题讨论】:

  • 问题解决了 this.ServiceName = MyServiceName;在 OnStart() 方法中导致问题。

标签: c# windows-services


【解决方案1】:

这里的问题是this.ServiceName = MyServiceName;应该在Constructor中,而不是OnStart方法中。构造函数将为每个实例设置它,但它会引发异常,因为在调用 OnStart 时服务已经被认为正在运行。

ServiceName 向服务控制管理器标识服务。此属性的值必须与相应安装程序类的 ServiceInstaller.ServiceName 属性中记录的服务名称相同。在代码中,服务的ServiceName通常设置在可执行文件的main()函数中。

--MSDN Reference

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 2013-12-27
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多