Problem with Windows Service Application

V

vinoth

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",BindingFlags.InvokeMethod,null,wrapperInst,Parameters);
if(stream.CanWrite)
{
byte[] sendByteData =
System.Text.Encoding.Default.GetBytes(result.ToString());
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
 
W

Willy Denoyette [MVP]

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.


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",BindingFlags.InvokeMethod,null,wrapperInst,Parameters);
if(stream.CanWrite)
{
byte[] sendByteData =
System.Text.Encoding.Default.GetBytes(result.ToString());
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
 
V

vinoth

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
 
W

Willy Denoyette [MVP]

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?



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

Ignacio Machin \( .NET/ C# MVP \)

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.AppSettings["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;
}

}
 
B

Bennie Haelen

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
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",BindingFlags.InvokeMethod,null,wrapperInst,Parameters);
if(stream.CanWrite)
{
byte[] sendByteData =
System.Text.Encoding.Default.GetBytes(result.ToString());
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top