How can I make service apps with Visual c# Express?

后端 未结 4 2057
执笔经年
执笔经年 2021-01-30 07:32

I have build an application to parse Xml file for integrating data in mssql database. I\'m using Visual c# express. There\'s a way to make service with express edition or I a ha

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 08:12

    Absolutely you can. You can even do it with csc. The only thing in VS is the template. But you can reference the System.ServiceProcess.dll yourself.

    Key points:

    • write a class that inherits from ServiceBase
    • in your Main(), use ServiceBase.Run(yourService)
    • in the ServiceBase.OnStart override, spawn whatever new thread etc you need to do the work (Main() needs to exit promptly or it counts as a failed start)

    Sample Code

    Very basic template code would be:

    Program.cs:

    using System;
    using System.ServiceProcess;
    
    namespace Cron
    {
        static class Program
        {
            /// 
            /// The main entry point for the application.
            /// 
            static void Main()
            {
                System.ServiceProcess.ServiceBase.Run(new CronService());
            }
        }
    }
    

    CronService.cs:

    using System;
    using System.ServiceProcess;
    
    namespace Cron
    {
        public class CronService : ServiceBase
        {
            public CronService()
            {
                this.ServiceName = "Cron";
                this.CanStop = true;
                this.CanPauseAndContinue = false;
                this.AutoLog = true;
            }
    
            protected override void OnStart(string[] args)
            {
               // TODO: add startup stuff
            }
    
            protected override void OnStop()
            {
               // TODO: add shutdown stuff
            }
        }
    }
    

    CronInstaller.cs:

    using System.ComponentModel;
    using System.Configuration.Install;
    using System.ServiceProcess;
    
    [RunInstaller(true)]
    public class CronInstaller : Installer
    {
      private ServiceProcessInstaller processInstaller;
      private ServiceInstaller serviceInstaller;
    
      public CronInstaller()
      {
        processInstaller = new ServiceProcessInstaller();
        serviceInstaller = new ServiceInstaller();
    
        processInstaller.Account = ServiceAccount.LocalSystem;
        serviceInstaller.StartType = ServiceStartMode.Manual;
        serviceInstaller.ServiceName = "Cron"; //must match CronService.ServiceName
    
        Installers.Add(serviceInstaller);
        Installers.Add(processInstaller);
      } 
    }  
    

    And a .NET service application is not installed the same way as normal service application (i.e. you can't use cron.exe /install or some other command line argument. Instead you must use the .NET SDK's InstallUtil:

    InstallUtil /LogToConsole=true cron.exe
    

    Resources

    • Creating a Windows Service in .NET by Mark Strawmyer
    • Writing a Useful Windows Service in .NET in Five Minutes by Dave Fetterman
    • Installing and Uninstalling Services
    • Walkthrough: Creating a Windows Service Application in the Component Designer

提交回复
热议问题