How to start two services in separate instances - c#

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.

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

How to self-host WCF in Windows Forms without another Windows process

I have a Windows application and want to self-host a WCF in it. This MSDN article walks you through how to self-host WCF in a console. Jason Henderson's article demonstrates how to call the service. But the problem is, I don't want to host my service in another Windows process. I want to host it in my client application. Here is my approach:
Ctrl + F5 to run the service
Add service reference to my client
application
Then I can start the service in my client like this
static void Main()
{
ServiceHost host = new ServiceHost(typeof(MyService));
host.Open();
Application.Run(new Form1());
host.Close();
}
It works. But what is the best way for doing this?
that is exactly what Microsoft recomends:
Hosting in Windows Services
http://msdn.microsoft.com/en-us/library/bb332338.aspx

.Net Solution: Build Steps for windows services

I have created 2 WCF service libraries in a solution. I have also created two windows services which will host the two wcf services.
these wcf services communicate with eachother. one gets requests and is reusable service for other systems, the other service sends requests to this service and takes requests from the UI.
I have created installers for the window services too.
I want to understand the build script tasks which I need to include which I will call during each time I want to deploy this solution to the server?
Should I delete all of the windows services and install again with new DLLs?
What is the best way
There is no need to delete the windows services. You can stop the service, replace the service executable and all the assemblies used by your service and restart the service. This is what I've always done, and I've never run into problems.

Multiple Windows Services, One Installer, Removing Dependencies

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.

Categories