Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

service

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 have to get Visual Studio to do it?

like image 852
Bigballs Avatar asked Feb 20 '09 13:02

Bigballs


1 Answers

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
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        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
like image 88
Marc Gravell Avatar answered Oct 03 '22 23:10

Marc Gravell