locked
Windows service application in Visual C# Express RRS feed

  • Question

  • I'm trying to build a simple Windows Service application in Visual C# Express. I simply can't get started.

    The documentation mentions a NON-existent template, which makes it kind of hard.

    However, I can see there's a 'ServiceBase' item in the toolbox and I have a feeling that that's the way to go, but there's no documentation telling how to use that.

    If I start a Windows Form application, I can drop this 'ServiceBase' component on the page, but this must be the wrong approach.

    Can anyone point me in the right direction - a step by step approach to get started would be really nice (missing in the help files).

    What project template to use? Where to drop 'ServiceBase'?

    (just a comment: Why do I have to write methods? MS should have made 'ServiceBase's methods trigger events, much easier to code )

    Monday, March 27, 2006 4:14 AM

Answers

  •  

    The service application templates are not included in the Visual Studio Express products, but there is a workaround to allow you to use them. What I have done in the past is to load up the trial version of the Visual Studio System and create a base application from each template and then export them as templates. Then once inside the Express IDE I would then import the templates, from this point on I now have a windows Service template that I can use.

    If you do not have the trial you may be able to find someone to export the template for you.

    Tuesday, March 28, 2006 8:31 PM
  •  

    You might be able to create an application base in 2003 using the templates that it has... and then upgrade the application using 2005 (Just need to open and follow the wizard). Then I would export that as a template file so that you can use it at a later date.

    Thursday, May 18, 2006 11:22 AM

All replies

  • Have you read the MSDN ServiceBase class entry?

    It has a C# example. Create a new empty project and paste the code into a new code file. You probably also need to add a reference to System.ServiceProcess .NET component.

     

    Monday, March 27, 2006 12:34 PM
  • I have seen it, but I was looking for a way to use the 'ServiceBase' component in the toolbox. Manual coding is not the future, especially not in Express products. Since MS has put a component in the toolbox (actually three related to services), there must be a way to use these (setting properties in the IDE is so much easier).

    Can anyone show how to use these components?

    Monday, March 27, 2006 7:24 PM
  • I only see one item in the toolbox related to services. ServiceController class which is for starting and stopping installed services.

    I can not find any class in the System.ServiceProcess namespace supporting what you are suggesting.

    To do what you want seem to need a 3rd party component if there is one. I searched some but could not find one.

     

    Monday, March 27, 2006 8:28 PM
  • Look in the 'General' tab.

    I have 'ServiceBase', 'ServiceInstaller' and 'ServiceProcessInstaller'.

    That's the components I want to learn how to use.

    Monday, March 27, 2006 8:46 PM
  • Update: Now I know where I have got them from: Select 'Options' menu, 'Choose Toolbox items'. Here I have selected the three items, so they appear in the toolbox!
    Tuesday, March 28, 2006 1:42 AM
  • As far as I can tell, that is not how you use those classes. Consider this from about the ServiceBase class entry.

    Provides a base class for a service that will exist as part of a service application. ServiceBase must be derived from when creating a new service class.

     

    Tuesday, March 28, 2006 7:28 AM
  •  

    The service application templates are not included in the Visual Studio Express products, but there is a workaround to allow you to use them. What I have done in the past is to load up the trial version of the Visual Studio System and create a base application from each template and then export them as templates. Then once inside the Express IDE I would then import the templates, from this point on I now have a windows Service template that I can use.

    If you do not have the trial you may be able to find someone to export the template for you.

    Tuesday, March 28, 2006 8:31 PM
  • Hello,

    I face the problem recently. How about if I use the template from Visual C# .NET Standard 2003, would the template be differed from Visual Studio 2005? Thanks for any advice.

    Charoite
    Thursday, May 18, 2006 2:12 AM
  •  

    You might be able to create an application base in 2003 using the templates that it has... and then upgrade the application using 2005 (Just need to open and follow the wizard). Then I would export that as a template file so that you can use it at a later date.

    Thursday, May 18, 2006 11:22 AM
  • Hello Glenn Wilson

    Thanks for your reply. I have solved it by downloading samples from the CodeProject and use C# Express to export it as a template. Then, I am now have a Windows Service project template for me. Thanks again.

    Charoite

    Ref.: http://www.codeproject.com/dotnet/simplewindowsservice.asp

    Saturday, May 20, 2006 12:40 AM
  • Someone else has already mentioned that the C# 2008 Express Edition doesn't include a template for a Windows service, but manually creating one is extremely straight-forward.  You need to create a Windows forms project, remove Form1 and make Main() empty; include a reference to System.Configuration.Install and System.ServiceProcess.

     

    You will need to create a class that derives from System.ServiceProcess.ServiceBase and override the base methods as needed:

     

    public class MyService : System.ServiceProcess.ServiceBase

     

    Your Main() will call System.ServiceProcess.ServiceBase.Run:

     

    System.ServiceProcess.ServiceBase.Run(new MyService());

     

    Technically you don't need an installer, but it makes life easier (otherwise you would have to manually create the necessary registry entries).

     

    Once you have the classes defined you should be able to use the designer to set parameters, and you can also export your project as a template.

     

    /***************

    * Program.cs *

    ***************/

    using System;

    using System.ServiceProcess;

    namespace MyService

    {

    static class Program

    {

    [STAThread]

    static void Main()

    {

    ServiceBase.Run(new MyService());

    }

    }

    }

     

    /*****************

    * MyService.cs *

    *****************/

    using System;

    using System.ServiceProcess;

    using System.Diagnostics;

    namespace MyService

    {

    public class MyService : ServiceBase

    {

    public MyService()

    {

    InitializeComponent();

    }

     

    protected override void OnStart(string[] args)

    {

    this.EventLog.WriteEntry("MyService Service Has Started");

    }

     

    protected override void OnStop()

    {

    this.EventLog.WriteEntry("MyService Service Has Stopped");

    }

     

    private void InitializeComponent()

    {

    this.ServiceName = "MyService";

    this.CanStop = true;

    this.AutoLog = false;

    this.EventLog.Log = "Application";

    this.EventLog.Source = "MyService";

    }

    }

    }

     

    /**************************

    * MyServiceInstaller.cs *

    **************************/

    using System;

    using System.ServiceProcess;

    using System.ComponentModel;

    using System.Configuration.Install;

    namespace MyService

    {

    [RunInstaller(true)]

    public class MyServiceInstaller : Installer

    {

    private ServiceProcessInstaller ServiceProcessInstaller1;

    private ServiceInstaller ServiceInstaller1;

     

    public MyServiceInstaller()

    {

    InitializeComponent();

    }

     

    private void InitializeComponent()

    {

    this.ServiceProcessInstaller1 = new ServiceProcessInstaller();

    this.ServiceProcessInstaller1.Account = ServiceAccount.LocalSystem;

    this.ServiceProcessInstaller1.Username = null;

    this.ServiceProcessInstaller1.Password = null;

    this.ServiceInstaller1 = new ServiceInstaller();

    this.ServiceInstaller1.Description = "MyService Service Template";

    this.ServiceInstaller1.DisplayName = "My Service";

    this.ServiceInstaller1.ServiceName = "MyService";

    this.ServiceInstaller1.StartType = ServiceStartMode.Manual;

    this.Installers.AddRange(new Installer[] { this.ServiceProcessInstaller1, this.ServiceInstaller1 });

    }

    }

    }

    • Proposed as answer by Kyle Wiering Tuesday, July 6, 2010 6:26 PM
    Tuesday, November 27, 2007 3:51 PM
  • Thanks for the above tip!

    Monday, February 4, 2008 1:19 PM
  • Thank you. I use Visual Express 2008 C# and there is no template for services. I have been looking all over and tried for a couples of days now with more or less the same formular as described i tgerberts post, but with no luck. It always came back with the message that the service has started and stopped and some services stop automaticly when they are not doing a job.

    After just directly doing what it says in tgerberts post and copying the files IT WORKS.....

    "Windows service process template C# using Visual Express"
    Sunday, February 10, 2008 5:54 PM
  • This worked great, but installation is still a problem. The installutil.exe in C:\WINDOWS\Microsoft.NET\Framework\vXYZ can be used to install it, but I wanted to create a .msi so others could install my service. Visual Studio Express doesn't support Setup projects. Is there a way to get Visual Studio Express to create a .msi?
    Sunday, May 2, 2010 9:04 PM
  • I know this is old, but I made a template for Visual Studio 2010 Express you can get it at my blog: http://blog.larmib.com/2011/windows-service-template-for-visual-studio-2010-express/ it is for Visual Basic. Someone can convert it to C#.
    Tuesday, October 11, 2011 6:12 PM