D
D.R.Jayahar Devadhason
Guest
Hi,
I have a WCF web service and a client project. The client application has no UI. It has connection with the service. It contains only four classes. One class is main class all the other classes have been called in the main class. I want to run the client as a service. So I created one window service project, added all the four classes in "App_Code" folder, also added service reference, also added project installer class. I specified the StartType property of SelfHostedService as "Automatic" and also I gave the name of the service in Service name property. In WindowsService.cs file I have these codings.
public partial class WindowsService : ServiceBase
{
public ServiceHost ClientInfoService = null;
public WindowsService()
{
InitializeComponent();
ServiceName = "ClientService";
}
protected override void OnStart(string[] args)
{
//// TODO: Add code here to start your service.
try
{
Process process = new Process();
process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "MyClient.exe";
process.Start();
}
catch //Do Nothing
{
}
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
if (ClientInfoService != null)
{
ClientInfoService.Close();
ClientInfoService = null;
}
}
}
When I Install the client installer I am Showing simple UI for configure the port number, Server machine name. In that UI I have one button like "Start". I am starting the serivce through this button click event. Below are the codes present in under this event.
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "MyClient";
string status = controller.Status.ToString();
if (btnStart.Text == "Start")
{
if (status != "Stopped")
{
controller.Stop();
}
System.Threading.Thread.Sleep(5000);
controller.Start();
System.Threading.Thread.Sleep(500);
MessageBox.Show(this, "Started", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnStart.Text = "Stop";
}
else
{
if (status != "Stopped")
Thread.Sleep(500);
MessageBoxEx.Show(this, "Stopped", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
controller.Start();
System.Threading.
MessageBoxEx.Show(this, "stopped", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Thread.Sleep(500);
controller.Stop();
System.Threading.Thread.Sleep(500);
Some times Service is starting successfully and some times it is throwing an exception like "Service cannot be started. The service process could
not connect to the service controller
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp." The codings are executed even the MyClient.exe is running also.Can you anyone help me how can i run the client as a windows service successfully?
Thanks in Advance,
Jayahar
Continue reading...
I have a WCF web service and a client project. The client application has no UI. It has connection with the service. It contains only four classes. One class is main class all the other classes have been called in the main class. I want to run the client as a service. So I created one window service project, added all the four classes in "App_Code" folder, also added service reference, also added project installer class. I specified the StartType property of SelfHostedService as "Automatic" and also I gave the name of the service in Service name property. In WindowsService.cs file I have these codings.
public partial class WindowsService : ServiceBase
{
public ServiceHost ClientInfoService = null;
public WindowsService()
{
InitializeComponent();
ServiceName = "ClientService";
}
protected override void OnStart(string[] args)
{
//// TODO: Add code here to start your service.
try
{
Process process = new Process();
process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "MyClient.exe";
process.Start();
}
catch //Do Nothing
{
}
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
if (ClientInfoService != null)
{
ClientInfoService.Close();
ClientInfoService = null;
}
}
}
When I Install the client installer I am Showing simple UI for configure the port number, Server machine name. In that UI I have one button like "Start". I am starting the serivce through this button click event. Below are the codes present in under this event.
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "MyClient";
string status = controller.Status.ToString();
if (btnStart.Text == "Start")
{
if (status != "Stopped")
{
controller.Stop();
}
System.Threading.Thread.Sleep(5000);
controller.Start();
System.Threading.Thread.Sleep(500);
MessageBox.Show(this, "Started", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnStart.Text = "Stop";
}
else
{
if (status != "Stopped")
Thread.Sleep(500);
MessageBoxEx.Show(this, "Stopped", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
controller.Start();
System.Threading.
MessageBoxEx.Show(this, "stopped", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Thread.Sleep(500);
controller.Stop();
System.Threading.Thread.Sleep(500);
Some times Service is starting successfully and some times it is throwing an exception like "Service cannot be started. The service process could
not connect to the service controller
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp." The codings are executed even the MyClient.exe is running also.Can you anyone help me how can i run the client as a windows service successfully?
Thanks in Advance,
Jayahar
Continue reading...