By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

Problem with Windows Service Application

P: n/a
Hi,

I have created WindowsService Project.In that Project OnStart Method
i have written the following Code. In this code the Server is waiting
for the connection from client. When the Client connects to the Server,
the server will process and send result to the client.(This is Client
Server Application. The Server side Code is implementd in th OnStart
method Of Windows Service).

When i tried the Client Server Application in Console Application it's
working fine. But When i make the Server side Code in to Windows
Service Application(OnStart method), i can able to create Service
also.(I given the Service Automatic in the Properyt Window and Account
is LocalSystem). But when i try to start the Service its Giving the
Following Error.

Error:-
Could not Start the Listener Service on Local Computer.
"Error 1053: The service did not respond to the start or control
request in a timely fashion"

How should i solve this issue. If anybody knows the solution please let
me know.
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("Service Started...");
try
{
Int32 port = 56789;
IPAddress localAddr = IPAddress.Parse("192.168.1.10");
TcpListener server = new TcpListener(localAddr, port);
server.Start();
Socket tcpSocket = server.AcceptSocket();
if(tcpSocket.Connected)
{
EventLog.WriteEntry("Connected... " + DateTime.Now);
NetworkStream stream = new NetworkStream(tcpSocket);
XmlSerializer xs = new XmlSerializer(typeof(ArrayList));
ArrayList clientReqAL = null;
byte[] buffer = new byte[1024];
int i;
MemoryStream mStream = new MemoryStream();
while((i = stream.Read(buffer,0,buffer.Length)) != 0)
{
mStream.Write(buffer,0,i);
if(!stream.DataAvailable)
break;
}
mStream.Seek(0, SeekOrigin.Begin);
clientReqAL = (ArrayList)xs.Deserialize(mStream);
string methodName = (string)clientReqAL[0];
clientReqAL.RemoveAt(0);
object wrapperInst = GetWrapperInstance(methodName);
object[] Parameters = new object[2];
Parameters[0] = methodName;
Parameters[1] = clientReqAL;
if(wrapperInst!=null)
{
object result =
wrapperInst.GetType().InvokeMember("CallMethod",Bi ndingFlags.InvokeMethod,null,wrapperInst,Parameter s);
if(stream.CanWrite)
{
byte[] sendByteData =
System.Text.Encoding.Default.GetBytes(result.ToStr ing());
stream.Write(sendByteData,0,sendByteData.Length);
}
stream.Close();
}
}
EventLog.WriteEntry("Before Close "+DateTime.Now);
tcpSocket.Close();
}
catch (Exception e)
{
EventLog.WriteEntry(e.Message);
}
}

Thanks,
Vinoth

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Your service startup violates one of the primary requirements of the
"Windows service - Service Control Manager (SCM) protocol contract", that
is, you have to return from OnStart as soon as possible (within 30 secs. by
default).
OnStart is there to initialize the Service environment and to start an
auxiliary thread that runs your actual Service code, once done with this you
have to return.

Willy.
<vi****@gsdindia.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

I have created WindowsService Project.In that Project OnStart Method
i have written the following Code. In this code the Server is waiting
for the connection from client. When the Client connects to the Server,
the server will process and send result to the client.(This is Client
Server Application. The Server side Code is implementd in th OnStart
method Of Windows Service).

When i tried the Client Server Application in Console Application it's
working fine. But When i make the Server side Code in to Windows
Service Application(OnStart method), i can able to create Service
also.(I given the Service Automatic in the Properyt Window and Account
is LocalSystem). But when i try to start the Service its Giving the
Following Error.

Error:-
Could not Start the Listener Service on Local Computer.
"Error 1053: The service did not respond to the start or control
request in a timely fashion"

How should i solve this issue. If anybody knows the solution please let
me know.
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("Service Started...");
try
{
Int32 port = 56789;
IPAddress localAddr = IPAddress.Parse("192.168.1.10");
TcpListener server = new TcpListener(localAddr, port);
server.Start();
Socket tcpSocket = server.AcceptSocket();
if(tcpSocket.Connected)
{
EventLog.WriteEntry("Connected... " + DateTime.Now);
NetworkStream stream = new NetworkStream(tcpSocket);
XmlSerializer xs = new XmlSerializer(typeof(ArrayList));
ArrayList clientReqAL = null;
byte[] buffer = new byte[1024];
int i;
MemoryStream mStream = new MemoryStream();
while((i = stream.Read(buffer,0,buffer.Length)) != 0)
{
mStream.Write(buffer,0,i);
if(!stream.DataAvailable)
break;
}
mStream.Seek(0, SeekOrigin.Begin);
clientReqAL = (ArrayList)xs.Deserialize(mStream);
string methodName = (string)clientReqAL[0];
clientReqAL.RemoveAt(0);
object wrapperInst = GetWrapperInstance(methodName);
object[] Parameters = new object[2];
Parameters[0] = methodName;
Parameters[1] = clientReqAL;
if(wrapperInst!=null)
{
object result =
wrapperInst.GetType().InvokeMember("CallMethod",Bi ndingFlags.InvokeMethod,null,wrapperInst,Parameter s);
if(stream.CanWrite)
{
byte[] sendByteData =
System.Text.Encoding.Default.GetBytes(result.ToStr ing());
stream.Write(sendByteData,0,sendByteData.Length);
}
stream.Close();
}
}
EventLog.WriteEntry("Before Close "+DateTime.Now);
tcpSocket.Close();
}
catch (Exception e)
{
EventLog.WriteEntry(e.Message);
}
}

Thanks,
Vinoth

Nov 17 '05 #2

P: n/a
Hi Willy,

Could you please tell me more about the following. How in OnStart i can
initialize the service Environment and how can i start an auxiliary
thread. Is there anyother way to do this?
OnStart is there to initialize the Service environment and to start anauxiliary thread that runs your actual Service code, once done withthis you have to return.

Thanks,
Vinoth

Nov 17 '05 #3

P: n/a

<vi****@gsdindia.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi Willy,

Could you please tell me more about the following. How in OnStart i can
initialize the service Environment and how can i start an auxiliary
thread. Is there anyother way to do this?
>OnStart is there to initialize the Service environment and to

start an
>auxiliary thread that runs your actual Service code, once done

with
>this you have to return.

Thanks,
Vinoth


Initializing is optional and depends on your specific scenario, but in
general you should check your pre-conditions if any before starting a
service, if these are not met you can throw an exception to indicate the
failure, a message will automatically be put in the eventlog when throwing.
If pre-conditions are met, simply create/start a thread to run your service
code in.
Something like this will do.....

MyServiceClass {
...
void Run() {
EventLog.WriteEntry("Service Started...");
Int32 port = 56789;
IPAddress localAddr = IPAddress.Parse("192.168.1.10");
TcpListener server = new TcpListener(localAddr, port);
...
// Must keep this running until requested to stop the service
}
}

protected override void OnStart(string[] args)
{
// (optionally) Initialize and check your service pre-conditions prior to
start the service activity
....
if(failedToInitialize)
throw (new Exception("Service failed to initialize"));
else {
MyServiceClass msc = new ServiceClass ();
msc = new Thread(new ThreadStart(msc.Run));
msc.IsBackground = true;
msc.Start();
return; // exit OnStart
}
Willy.
Nov 17 '05 #4

P: n/a
Hi,
Find below a code I use in such an escenario, I create a thread in the
onLoad as will said, also my server accept multi clients, so I hse another
thread to handle each particular connection, I use a Synced queue to store
the connections that are waiting (you could also use a thread with
parameters for the same thing )

Let me know if you have any doubt,

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Thread listenerThread;
Queue connectionQueue= null;

protected override void OnStart(string[] args)
{
listenerThread = new Thread( new ThreadStart( ListenerMethod));
listenerThread.Start();
}

protected void ListenerMethod()
{
try
{
Queue unsyncq = new Queue();
connectionQueue = Queue.Synchronized( unsyncq);
}
catch( Exception e)
{
eventLog1.WriteEntry( "Error in Listener method ( queue related ) : " +
e.Message, EventLogEntryType.Error);
return;
}
try
{
TcpClient socket;
TcpListener listener = new TcpListener( Convert.ToInt32(
System.Configuration.ConfigurationSettings.AppSett ings["Port"]) );
listener.Start();
while( true)
{

socket = listener.AcceptTcpClient();
connectionQueue.Enqueue( socket);

Thread workingthread = new Thread( new ThreadStart(
TheConnectionHandler));
workingthread.Start();
}
}
catch( Exception e)
{
eventLog1.WriteEntry( "Error in Listener method ( connection related ) :
" + e.Message, EventLogEntryType.Error);
return;
}

}
public void TheConnectionHandler()
{
try
{
TcpClient socket= (TcpClient)connectionQueue.Dequeue();

if ( socket != null )
{
//DO YOUR WORK HERE
}
}
catch( Exception e)
{
eventLog1.WriteEntry( "Error in Listener method ( connection related ) :
" + e.Message, EventLogEntryType.Error);
return;
}

}
<vi****@gsdindia.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi Willy,

Could you please tell me more about the following. How in OnStart i can
initialize the service Environment and how can i start an auxiliary
thread. Is there anyother way to do this?
>OnStart is there to initialize the Service environment and to

start an
>auxiliary thread that runs your actual Service code, once done

with
>this you have to return.

Thanks,
Vinoth

Nov 17 '05 #5

P: n/a
Hello vinoth,

The OnStart() method is called by the Service Control Manager (SCM)
whenever you start the service. You should only put initialization code
in the OnStart() method, because if has to return within a given timeout.

I recommend that you move your code into a seperate method, and activate
this method on a seperate thread from the OnStart() method. That way,
the OnStart() method will complete in a timely fashion, while your
service can execute the socket code in your seperate thread.

Regards,

Bennie Haelen
vi****@gsdindia.com wrote:
Hi,

I have created WindowsService Project.In that Project OnStart Method
i have written the following Code. In this code the Server is waiting
for the connection from client. When the Client connects to the Server,
the server will process and send result to the client.(This is Client
Server Application. The Server side Code is implementd in th OnStart
method Of Windows Service).

When i tried the Client Server Application in Console Application it's
working fine. But When i make the Server side Code in to Windows
Service Application(OnStart method), i can able to create Service
also.(I given the Service Automatic in the Properyt Window and Account
is LocalSystem). But when i try to start the Service its Giving the
Following Error.

Error:-
Could not Start the Listener Service on Local Computer.
"Error 1053: The service did not respond to the start or control
request in a timely fashion"

How should i solve this issue. If anybody knows the solution please let
me know.
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("Service Started...");
try
{
Int32 port = 56789;
IPAddress localAddr = IPAddress.Parse("192.168.1.10");
TcpListener server = new TcpListener(localAddr, port);
server.Start();
Socket tcpSocket = server.AcceptSocket();
if(tcpSocket.Connected)
{
EventLog.WriteEntry("Connected... " + DateTime.Now);
NetworkStream stream = new NetworkStream(tcpSocket);
XmlSerializer xs = new XmlSerializer(typeof(ArrayList));
ArrayList clientReqAL = null;
byte[] buffer = new byte[1024];
int i;
MemoryStream mStream = new MemoryStream();
while((i = stream.Read(buffer,0,buffer.Length)) != 0)
{
mStream.Write(buffer,0,i);
if(!stream.DataAvailable)
break;
}
mStream.Seek(0, SeekOrigin.Begin);
clientReqAL = (ArrayList)xs.Deserialize(mStream);
string methodName = (string)clientReqAL[0];
clientReqAL.RemoveAt(0);
object wrapperInst = GetWrapperInstance(methodName);
object[] Parameters = new object[2];
Parameters[0] = methodName;
Parameters[1] = clientReqAL;
if(wrapperInst!=null)
{
object result =
wrapperInst.GetType().InvokeMember("CallMethod",Bi ndingFlags.InvokeMethod,null,wrapperInst,Parameter s);
if(stream.CanWrite)
{
byte[] sendByteData =
System.Text.Encoding.Default.GetBytes(result.ToStr ing());
stream.Write(sendByteData,0,sendByteData.Length);
}
stream.Close();
}
}
EventLog.WriteEntry("Before Close "+DateTime.Now);
tcpSocket.Close();
}
catch (Exception e)
{
EventLog.WriteEntry(e.Message);
}
}

Thanks,
Vinoth

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.