Multiple Windows Services, One Installer, Removing Dependencies - c#

I have a single service installer project that installs multiple services. This is great but a single unhandled exception in any single service will stop all services that were installed by this installer.
The code for the installer looks something like this
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1() ,
new Service2() ,
...
};
ServiceBase.Run(ServicesToRun);
So once the service is installed I can see several separate services in the windows service management window and each can be started, stopped, paused, resumed independently.
However if one suffers an unhandled exception then they all stop.
Ideally only the service that had the problem would stop and the other services would continue on their merry way.
Can anyone suggest a way to do this without creating a truly separate installer project for each service?

Add independent error handling per service at the highest level, this way not all of the services will be effected.
You may also want to add a auto service restart if the error is not critical. Just be sure you add tons of logging in the case of service shut downs and restarts from errors.

Related

Using Windows Service Application inside Windows forms application C#

I've created a simple socket application in which I have to open a socket receiver to listen for incoming messages on desktop. Problem is I need to be able to run the application as background service and I need to be able to start and stop the service from System Tray icon, for that I've created a windows forms application and added and I've created a windows service inside a windows forms application. On debug mode when I call the service code directly It's working fine. The service is installing and the OnStart method is being called. But when I try to start the application after building the project and running the .exe file I get the following error.
"Cannot start service from the command line or a debugger a windows service must first be installed. Then start with the server explorer"
And when I try to start the service manually from the windows services panel I get the following error.
"Error 1053: The service did not respond to the start or control request in a timely fashion".
This is the code used to install the service which is working fine.
AssemblyInstaller installer = GetInstaller(assembly)
installer.Install(state);
installer.Commit(state);
And this is the code for starting the service which is giving the error.
"Cannot start service from the command line or a debugger a windows service must first be installed. Then start with the server explorer"
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new SocketService()
};
ServiceBase.Run(ServicesToRun);
Can anyone please help me with this. I need to create a single msi installation utility of the entire application. In which I need to install both the desktop application and the windows service. Where the desktop application can Start or Stop the service. Thanks.

How to start two services in separate instances

I am starting WCF services from a Windows service. But at the same time I need to start another Windows service from the same windows service in separate instance.
Below is the code which I am using for starting a single WCF service.
Program.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WCFService()
};
ServiceBase.Run(ServicesToRun);
}
I need help to start another service from this service in separate instance, please help me.
Thanks in advance.
I found answer in below link
Multiple Windows Services in One exe
We need to add one more Windows Service file in the same file and need to add installer for that. It will start two services in the same Single Windows service.

How to properly program a windows service?

I've been having some fun reading about windows services on MSDN, but i'm having some trouble grasping some of the information.
I have created a windows service following the steps at this msdn page, and i noticed that they ommitted that one of the automatically generated sections (namely, the main section in program.cs) starts a collection of services. I'm having trouble understanding how will a service actually run, or more correctly, how do i correctly program a service to do what i want. The automatically generated service.cs only has a constructor, an an OnStart and OnStop event methods. Should i program the bulk of my code on the OnStart method, or is there a similar Main section to services?
Another question i have is regarding the deployment of services. At the page i mentioned, they referenced another page that has me use installutil on the VS developer console. How can i make an automatic installer for the service, in case the target platform doesn't have the developer console?
A service is no more than a (most of the time) console program. So, yes, the OnStart method is the entry point for your service, just like the Main method. If your service has to be executed periodically, you have to manage the loop part. The OnStop method is called whenever the user manually stops the service or when the system does.
The reason there is a ServiceBase array launched in the Main Program method, is that you can have multiple service classes and launch them in parallel.
You have to know that the content of the Main method can only be executed by the system in the context of an installed service. You cannot debug this way. You have to do it using a classic "console app". I personally use a trick to debug a service in VisualStudio more easily:
Let say we have an ImportService class that do our processing. We will use DEBUG compiler constant and create a DebugStart() method that will start processing (OnStart method is protected)
#if DEBUG
public void DebugStart()
{
// Call you processing method
}
#endif
Now let's edit our Main() Program method the same way to start our processing instead of the ServiceBase.Run method when debugging:
#if DEBUG
ImportService service = new ImportService();
service.DebugStart();
#else
ServiceBase[] ServicesToRun =
{
new ImportService()
};
ServiceBase.Run(ServicesToRun);
#endif
This way, when running your project in debug mode it will launch the service as a standard console program.
The installutil command is a windows command used to install a service program. To install your service, you have multiple solutions, depending on your installer:
Using InstallShield. Since Visual Studio 2013 (or maybe 2012?), InstallShield Lite Edition is standardly included in place of former Windows Installer. You have to activate and install it though (Create a new project of Other types>Configuration and deployment>Activate InstallShield Limited Edition). You can install windows services with it in the form of an installer
Making a simple .bat file that will run the installutil command on the client machine.
Using WiX which is very complete but also more complex but allows you to do anything during installation.
How can i make an automatic installer for the service, in case the target platform doesn't have the developer console?
You have to add a System.Configuration.Install.Installer to install your service on a remote machine. This should even work if the target machine doesn't have the developer console available.
I'm having trouble understanding how will a service actually run, or
more correctly, how do i correctly program a service to do what i
want.
That's actually a lot simpler than you might initially think. Your service will run the same as it would if it was a normal process. If you have a console application that opens a port and listens for traffic, then that would still work pretty much in the same way when you turn it into a service.
What does change, however, is how your service is started. After you have installed your service properly, you can start it like this:
[MTAThread]
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new YourServerClass()
};
ServiceBase.Run(ServicesToRun);
}
YourServerClass should inherit ServiceBase, which will allow you to use the OnStart, OnStop methods. In those methods you specify how your service is started, and what should be done (in terms of cleanup etc.) when your service is stopped.
If all went well, and you have the installetr and the actual service in the project, you should be able to install it as a service through the use of PowerShell, after which you can just start, stop and restart it from the Services window as you would with any other service.

Monitoring a custom windows service

I've built a windows service application using Visual Studio .NET and C#. This service will be deployed on a few hundred machines and I'm looking for a solid way to monitor the application. By monitor, I mean, I just want to check to make sure that it's running, and check the status of a few settings on each client.
Is there a common method of doing this?
The easiest thing to do is have each application "call home". Create a single central application, preferably a web app, and have the remote applications make a small call to the central app on whatever interval you feel is necessary. They can include the extra information you want to monitor.
Keep a list of where the application is deployed and if you don't get a call from any on the list within the expected timeframe, then you know it's offline.
If you can't change the actual application that you're monitoring, then write a small companion application that can run as a scheduled tasks and perform the same local checks and call back to the central application.
We do this with thousands of client machines worldwide and it works well.
You could write a little monitor utility program that checks the service state via the SCM and provides a simple HTTP interface so you can poll the status. This would basically be just a big loop with some reporting if the service state changes.
while (true)
{
string serviceName = "NameOfYourService";
ServiceController Svc = new ServiceController(serviceName);
if (Svc.Status != ServiceControllerStatus.Running)
{
//Do reporting/set status here
}
Thread.Sleep(5000);
}
You could take advantage of a monitoring tool like Zabbix, for example.

WCF: Managing loads of hosting console apps!

I now have a scenario where I have 6 console apps on my server which each host a WCF service.
It's getting messy and I am now thinking about having a windows app (probably WPF) which hosts each service in a separate thread. This would mean there's a central place to manage the services as well.
I'm just wondering if anyone has implemented such an app or any advice before going forward?
I work on a system that has ~8 windows services, all hosting 1 WCF service each. To easily coordinate the execution of the services, we created a service coordinator application that when started, will start the other 8 services. This makes starting, stopping, and restarting the services really easily... and because they're windows services and not console applications, there's no desktop space lost.
The projects themselves are actually compiled as console applications, so that we can work on them easily during development (and also run them with a /debug argument to test them after they've been deployed). Maybe something like that will work for you.
...which hosts each service in a separate thread...
why? By default the WCF runtime is doing the threading for you. MSDN has a god starting point on this or have a look at Programming WCF Services by Juval.
To host multiple services in one thread you could check out this multi-service-host (using AppDomains for separation). I've done a very similar host before reading this and it is now hosting >100 services (started with 30 something) for thousands of users in a single operating system service.

Categories