Romi24
Romi24

Reputation: 271

Windows Service - Encapsulating Existing Functionality

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

Upvotes: 1

Views: 299

Answers (4)

Ramashankar
Ramashankar

Reputation: 1658

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

Upvotes: 1

millimoose
millimoose

Reputation: 39970

.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.

Upvotes: 1

Felice Pollano
Felice Pollano

Reputation: 33262

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.

Upvotes: 2

mageos
mageos

Reputation: 1291

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

Upvotes: 1

Related Questions