EDN Admin
Well-known member
Hi,<br/><br/>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.<br/><br/><span style="font-size:x-small <span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small partial<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small class<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small WindowsService<span style="font-size:x-small : <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceBase<br/><span style="font-size:x-small {<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceHost<span style="font-size:x-small ClientInfoService = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small null<span style="font-size:x-small ;<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small WindowsService()<br/>{<br/>InitializeComponent();<br/>ServiceName = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "ClientService"<span style="font-size:x-small ;<br/>}<br/><br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small protected<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small override<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small OnStart(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small [] args)<br/>{<br/><span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small //// TODO: Add code here to start your service.<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small try<br/><span style="font-size:x-small {<br/><span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Process<span style="font-size:x-small process = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Process<span style="font-size:x-small ();<br/>process.StartInfo.FileName = <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small AppDomain<span style="font-size:x-small .CurrentDomain.BaseDirectory + <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "MyClient.exe"<span style="font-size:x-small ;<br/>process.Start();<br/>}<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small catch<span style="font-size:x-small <span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small //Do Nothing<br/><span style="font-size:x-small {<br/>}<br/>}<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small protected<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small override<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small OnStop()<br/>{<br/><span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small // TODO: Add code here to perform any tear-down necessary to stop your service.<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (ClientInfoService != <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small null<span style="font-size:x-small )<br/>{<br/>ClientInfoService.Close();<br/>ClientInfoService = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small null<span style="font-size:x-small ;<br/>}<br/>}<br/>}<br/><br/>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.<span style="font-size:x-small <span style="font-size:x-small
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceController<span style="font-size:x-small controller = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceController<span style="font-size:x-small ();<br/>controller.MachineName = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "."<span style="font-size:x-small ;<br/><span style="font-size:x-small controller.ServiceName = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "MyClient"<span style="font-size:x-small ;<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small status = controller.Status.ToString();<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (btnStart.Text == <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Start"<span style="font-size:x-small )<br/>{<br/> <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (status != <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stopped"<span style="font-size:x-small )<br/>{<br/>controller.Stop();<br/>}<br/>System.Threading.<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(5000);<br/>controller.Start();<br/>System.Threading.<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500);<br/><span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBox<span style="font-size:x-small .Show(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Started"<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Sample"<span style="font-size:x-small , <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxButtons<span style="font-size:x-small .OK, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxIcon<span style="font-size:x-small .Information);<br/>btnStart.Text = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stop"<span style="font-size:x-small ;<br/>}<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small else
<span style="font-size:x-small
{<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (status != <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stopped"<span style="font-size:x-small )
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500);<span style="font-size:x-small
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxEx<span style="font-size:x-small .Show(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stopped"<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Sample"<span style="font-size:x-small , <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxButtons<span style="font-size:x-small .OK, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxIcon<span style="font-size:x-small .Information);
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small else
<span style="font-size:x-small
{
controller.Start();
<span style="font-size:x-small
System.Threading.
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxEx<span style="font-size:x-small .Show(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "stopped"<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Sample"<span style="font-size:x-small , <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxButtons<span style="font-size:x-small .OK, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxIcon<span style="font-size:x-small .Information);
}
}
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500); <br/> controller.Stop();<br/><span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#000000 System.Threading.Thread<span style="font-size:x-small .Sleep(500);<span style="font-size:x-small
Some times Service is starting successfully and some times it is throwing an exception like "<span style="font-size:xx-small Service cannot be started. The service process could <br/>not connect to the service controller<br/>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?<br/><br/>Thanks in Advance,<br/>Jayahar<span style="font-size:xx-small
View the full article
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small partial<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small class<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small WindowsService<span style="font-size:x-small : <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceBase<br/><span style="font-size:x-small {<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceHost<span style="font-size:x-small ClientInfoService = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small null<span style="font-size:x-small ;<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small WindowsService()<br/>{<br/>InitializeComponent();<br/>ServiceName = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "ClientService"<span style="font-size:x-small ;<br/>}<br/><br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small protected<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small override<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small OnStart(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small [] args)<br/>{<br/><span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small //// TODO: Add code here to start your service.<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small try<br/><span style="font-size:x-small {<br/><span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Process<span style="font-size:x-small process = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Process<span style="font-size:x-small ();<br/>process.StartInfo.FileName = <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small AppDomain<span style="font-size:x-small .CurrentDomain.BaseDirectory + <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "MyClient.exe"<span style="font-size:x-small ;<br/>process.Start();<br/>}<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small catch<span style="font-size:x-small <span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small //Do Nothing<br/><span style="font-size:x-small {<br/>}<br/>}<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small protected<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small override<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small OnStop()<br/>{<br/><span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small // TODO: Add code here to perform any tear-down necessary to stop your service.<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (ClientInfoService != <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small null<span style="font-size:x-small )<br/>{<br/>ClientInfoService.Close();<br/>ClientInfoService = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small null<span style="font-size:x-small ;<br/>}<br/>}<br/>}<br/><br/>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.<span style="font-size:x-small <span style="font-size:x-small
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceController<span style="font-size:x-small controller = <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small ServiceController<span style="font-size:x-small ();<br/>controller.MachineName = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "."<span style="font-size:x-small ;<br/><span style="font-size:x-small controller.ServiceName = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "MyClient"<span style="font-size:x-small ;<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small status = controller.Status.ToString();<br/><span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (btnStart.Text == <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Start"<span style="font-size:x-small )<br/>{<br/> <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (status != <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stopped"<span style="font-size:x-small )<br/>{<br/>controller.Stop();<br/>}<br/>System.Threading.<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(5000);<br/>controller.Start();<br/>System.Threading.<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500);<br/><span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBox<span style="font-size:x-small .Show(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Started"<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Sample"<span style="font-size:x-small , <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxButtons<span style="font-size:x-small .OK, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxIcon<span style="font-size:x-small .Information);<br/>btnStart.Text = <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stop"<span style="font-size:x-small ;<br/>}<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small else
<span style="font-size:x-small
{<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (status != <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stopped"<span style="font-size:x-small )
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500);<span style="font-size:x-small
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxEx<span style="font-size:x-small .Show(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Stopped"<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Sample"<span style="font-size:x-small , <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxButtons<span style="font-size:x-small .OK, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxIcon<span style="font-size:x-small .Information);
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small else
<span style="font-size:x-small
{
controller.Start();
<span style="font-size:x-small
System.Threading.
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxEx<span style="font-size:x-small .Show(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "stopped"<span style="font-size:x-small , <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Sample"<span style="font-size:x-small , <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxButtons<span style="font-size:x-small .OK, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBoxIcon<span style="font-size:x-small .Information);
}
}
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500); <br/> controller.Stop();<br/><span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#000000 System.Threading.Thread<span style="font-size:x-small .Sleep(500);<span style="font-size:x-small
Some times Service is starting successfully and some times it is throwing an exception like "<span style="font-size:xx-small Service cannot be started. The service process could <br/>not connect to the service controller<br/>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?<br/><br/>Thanks in Advance,<br/>Jayahar<span style="font-size:xx-small
View the full article