1

We are running a client-server application developed in Winforms C# (.net framework 4.0), that has the following components

  1. Client (Windows application)
  2. Server (Console Application)

We are studying the possibility of implementing the server (the console application) as a windows service.

Let me first outline the approach we have decided to take

  1. Create a windows service project (using windows service project template)
  2. Integrate/encapsulate the server functionality into the windows service

There is just one single question for which we need an answer. The attempt here is to make this question as objective as possible so it can be classified as a real question.

Do we need to convert the Server to a dll project?

Or can we give a reference to the server exe file in the windows service project?

Personally I do not think the second option is possible. But I would like to know if i am missing something.

Thanks a ton in advance

Romi

Romi24
  • 271
  • 2
  • 7
  • 15

4 Answers4

2

You can add the exe as a reference in your service project. Exe is just an assembly anyway. Maybe you have to set as public some classes, but the same would happen if you create a dll. I usually use another trick. I start from a service project, and I change it to behave as a Console Application:

Properties->Application->Output Type: Console Application

Then I write something like this in the main:

        if (Environment.UserInteractive)
        {
            log.Info("Starting as a console...");
            // call my service runner 
        }
        else{
              log.Info("Starting as a service...");
            log.Info(this.ServiceDisplayName);
            log.Info(this.ServiceDescription);
            ServiceBase[] servicesToRun = new ServiceBase[] 
            { 
                new MyServiceImpl();
            };
            try
            {
                ServiceBase.Run(servicesToRun);
            }
            catch (Exception e)
            {
                log.Fatal("A fatal error occurred while running.", e);
                throw;
            }
         }

With this strategy I have an exe that while run interactively it behave like a console, but could be installed in the service control manager. Maybe a similar refactoring would help your code.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • Okay I am new to windows service creation. But I interested in running the same assembly as either a service or a console application. The problem is that when I create a project of type windows service then I can't run the project from either the command prompt or the VS IDE. The error message I get is that the assembly should be executed by the SCM (words to the effect) – Romi24 Jan 24 '14 at 15:23
  • Yes you have to change the main as I shown. When you have Environment.UserInteractive DONT call ServiceBase.Run but instantiate your service class and make it running by calling the OnSTart code. – Felice Pollano Jan 24 '14 at 16:50
  • So, just the fact that I am using the Environment.UserInteractive property will give me a chance to run it either as a service or console application? – Romi24 Jan 24 '14 at 17:15
  • Yes you should avoid ServcieBase.Run in interactive mode, and remember the properties Properties->Application->Output Type: Console Application – Felice Pollano Jan 24 '14 at 17:21
1

The console application can be referenced just like a DLL and you can consume the public classes it contains.

mageos
  • 1,216
  • 7
  • 15
1

It will be good idea to convert the server code in dll project and then add the reference of that dll into windows service project and use it there.

However it is very much possible to use the reference of an executable project to another project in .net.

Walkthrough: Creating a Windows Service Application in the Component Designer

Ramashankar
  • 1,598
  • 10
  • 14
1

.NET based Windows services aren't really that special in and of themselves. You can use the same console-based application project for both. The main differences are:

What I would do is create the server as a service project, following the boilerplate in the docs:

class MyServer : ServiceBase 
{
    protected override void OnStart(string[] args)
    {
        // OnStart() MUST return, so spawn off a thread here
    }
    public void MyStart(string[] args)
    {
        OnStart(args);
    }           
}

You'll have to change the project type to "Console Application" in the project properties. The default for the service application template is "Windows Application", but it doesn't seem to matter which you use. Except for the part where you're supposed to debug a service by installing it, starting it, then attaching a remote debugger. Thank Cthulhu this is unnecessary when you:

Start it as appropriate based on Environment.UserInteractive:

public static void Main(string[] args)
{
    if (Environment.UserInteractive)
    {
        new MyServer().Start(args);
    }
    else
    {
        ServiceBase.Run(new ServiceBase[] { new MyServer() });
    }
}

This way you get a windows service capable of running in standalone mode, that can be debugged using F5.

Community
  • 1
  • 1
millimoose
  • 39,073
  • 9
  • 82
  • 134