473,378 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem with Windows Service Application

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
5 4245
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
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

<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Programatix | last post by:
Hi, I'm working on a project which includes WebServices and Windows Form application. The Windows Form application will call the WebServices to retrieve data from database. The data will be...
2
by: Eric Caron | last post by:
Hi Here's my problem, I create a windows service with Config.xml file. All setup for my service is in this file. I create a windows application to manage these settings (Config.XML File). This...
0
by: Linesh Gajera | last post by:
Hi, I have unique problem. I have configured RemotingServer running as Console Application and my Remoting object access Oracle database. My remoting object make call to Oracle database and...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
2
by: epaetz | last post by:
I'm getting Not associated with a trusted SQL Server connection errors on a .Net windows service I wrote, when it's running on my application server. It's not a problem with mixed mode...
1
by: Yoshitha | last post by:
HI I am calling exe (vb.net application) from web application for this i wrote code like this system.diagnosis.process.start("e:\...") i wrote above code in button click event.
3
by: Jay | last post by:
Hello, I have Windows Forms application which uses MS Access Database and a Pocket PC application which uses Datasets, Iam trying to pass Datasets between these applications using WebServices. ...
2
by: pvl | last post by:
Hi We have the following scenario: 1. SQL server 2000 on Windows 2003 Server 2. Web server 1, running web service 1 on Windows 2003 Server 3. Web server 2, running web service 2 on Windows 2000...
1
by: PankajLohani | last post by:
hello, This is very challenging problem for me. basically my windows service monitor the application when my application hang due any reason then my service kill that process and application .Then...
2
by: =?Utf-8?B?aGVsZmk=?= | last post by:
Hi all, I have replied with my own questions to an older post entry but after a while I thought it's better to start a new thread based on the previous one. Perhaps with the new thread I will...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.