Windows Services

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I created a Minimal Windows Service Program in Visual Studio.NET VC# using
the Wizard...i just added the MessageBox Display in OnStart() function and my
service is installed properly...but when i started the Service it display
that "Local Service started and stopped , Serivces should have Log Support
and Alerts Isssued"..

what is this error??...am running Windows XP Home Edition,

i have given the AccountType as "LocalService",i tried with everything
excpet with User...i no need to run as User...

i would be happy if anyone could help me sort out this ...

with regards,
C.C.Chakkaradeep
 
The errors is that you should not show a message box from a service.
Services are meant to run in the background without any user interaction
requirements.

Willy.
 
Hi Willy,

so then how to check it out whether it is running or not..and is Socket
Communication possible in Services.....that is,..my service should act as a
Multithreaded Server monitoring a Port Number and if any informations come to
the port it should accept the sockets and exeute something so that the
Service has completed it's job...

am a newbie to to this Service...

with regards,
C.C.Chakkaradeep
 
Socket communications are no issue. But you should be aware that services
are (should be) running in a restricted security context:
- They should not assume the presence of an interactive desktop nor an
interactive user (logon session).
- For security reasons, they should run with the least privileges possible
(run as NetworkService or LocalService).
Running as LocalSystem gives the service high privileges on the local system
but no access to remote system resources, it's the only account that enables
a service to interact with the desktop (to enable this - run services applet
and check/enable this property).
While it's possible to enable this option for quick and dirty debugging, I
would suggest you use one of the other facilities offered by .NET for
debugging and tracing (write to log file or eventlog, output to debugger
window etc.).

Willy.
 
Hi Willy,

so u mean to say that i can accept a socket and receive messages but i cant
send messages back to the client??

with regards,
C.C.Chakkaradeep
 
Chakkaradeep said:
Hi Willy,

so u mean to say that i can accept a socket and receive messages but i
cant
send messages back to the client??

No, I said Sockets are no problem.
Willy.
 
hi,

so then Sockets are not issue...hmm...nice to hear...so i can just create
Sockets using TcpListener and accept TcpClients,rite?.....and how to check
for OnStart() function...by storing some informations in files???

with regards,
C.C.Chakkaradeep
 
Chakkaradeep said:
hi,

so then Sockets are not issue...hmm...nice to hear...so i can just create
Sockets using TcpListener and accept TcpClients,rite?.....and how to check
for OnStart() function...by storing some informations in files???

Just write a message to the eventlog.

Willy.
 
hi willy ,

here is my code for Service...

/*********Service OnStart( ) function starts here**********************/
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
System.Net.IPAddress localAdd=System.Net.IPAddress.Parse("127.0.0.1");
const int portNumber = 3000;
TcpListener tcpListener = new TcpListener(localAdd,portNumber);
tcpListener.Start();
//try
{
//Accept the pending client connection and return a TcpClient initialized
for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream =
tcpClient.GetStream();
string responseString = "You have successfully connected to me";

Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);

tcpClient.Close();
tcpListener.Stop();

}
}
/*********Service OnStart( ) function ends here**********************/

I tried with evry Account....when i start the service,it is taking much time
and then finally tells that the servie cannot be installed becoz it is out of
time.....what is my error???

The client progrm is here...

/*********Client Program Starts here**********************/
static void Main(string[] args)
{
TcpClient tcpClient = new TcpClient();
try {
tcpClient.Connect("localhost", 3000);
NetworkStream networkStream = tcpClient.GetStream();

if(networkStream.CanWrite && networkStream.CanRead){
// Does a simple write.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
networkStream.Write(sendBytes, 0, sendBytes.Length);

// Reads the NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.ASCII.GetString(bytes);
Console.WriteLine("This is what the host returned to you: " +
returndata);

}
else if (!networkStream.CanRead)
{
Console.WriteLine("You can not write data to this stream");
tcpClient.Close();
}
else if (!networkStream.CanWrite)
{
Console.WriteLine("You can not read data from this stream");
tcpClient.Close();
}
}
catch (Exception e )
{
Console.WriteLine(e.ToString());
}
}
/*********Client Program ends here**********************/

The Server and Client Samples were taken from MSDN only.....

what would be the problem...

with regards,
C.C.Chakkaradeep
 
hi willy ,

here is my code for Service...

/*********Service OnStart( ) function starts here**********************/
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
System.Net.IPAddress localAdd=System.Net.IPAddress.Parse("127.0.0.1");
const int portNumber = 3000;
TcpListener tcpListener = new TcpListener(localAdd,portNumber);
tcpListener.Start();
//try
{
//Accept the pending client connection and return a TcpClient initialized
for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream =
tcpClient.GetStream();
string responseString = "You have successfully connected to me";

Byte[] sendBytes =
Encoding.ASCII.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);

tcpClient.Close();
tcpListener.Stop();

}
}
/*********Service OnStart( ) function ends here**********************/

I tried with evry Account....when i start the service,it is taking much
time
and then finally tells that the servie cannot be installed becoz it is out
of
time.....what is my error???

This code is correct, but it can't work; the SCM calls the OnStart()
function and expects it to return, or it will report the timout you're
experiencing; so you can do only initializations there, not your main
processing (i.e. waiting for connections). You'll have to run the service on
a different thread than the one who calls OnStart. You have two choiches
here: spawn the worker thread manually and have it run the same code you
tried to run inside OnStart(), or use async sockets..

Massimo
 
Chakkaradeep said:
hi willy ,
The Server and Client Samples were taken from MSDN only.....

what would be the problem...

with regards,
C.C.Chakkaradeep
You should not do anything that takes longer than 30 seconds in your OnStart
method. What you should do in your OnStart is spawn a background threads and
run your code on that thread. When your thread has started you simply return
from OnStart.

Willy.
 
Hi,

at last i did write a Service which acts a Multithreaded Server...thanks for
ur help Willy and others.....

i did these steps...

In OnStart() spawned a thread to Start Listening over a port,when a socket
is received...accept it and spawn another thread so that i can be executed in
it's own area...thus there wil be 2 threads created in my program .....
a)One thread for Listening,done only once
b)For each client , a thread will be spawned

With regards,
C.C.Chakkaradeep.
 
Yes, there is.

Add following class to the cs file that contains the Installer class (class
derived from System.Configuration.Install.Installer).
Change the class ServiceInstaller into ServiceInstallerEx and create an
instance of ServiceInstallerEx instead of ServiceInstaller .
Finally add - using System.Management;

class ServiceInstallerEx : System.ServiceProcess.ServiceInstaller
{
public ServiceInstallerEx() : base()
{
// Set eventhandler to call UpdateServiceConfig when install commited
base.Committed += new InstallEventHandler( this.UpdateServiceConfig );
}
// Eventhandler, called after service installer committed.
// This method uses System.Management classes to change service config.
// Note that enabling "DesktopInteract" will fail if Service account is not
LocalSystem !!
void UpdateServiceConfig(object sender, InstallEventArgs e)
{
int ret;
ManagementBaseObject inParams = null;
ManagementObject srvc = new ManagementObject("Win32_Service=" + "\"" +
this.ServiceName + "\"");
inParams = srvc.GetMethodParameters("Change");
inParams["DesktopInteract"] = true; // Enable interactive mode (Interact
With Desktop)
ManagementBaseObject outParams = srvc.InvokeMethod("Change", inParams,
null);
if((ret = Convert.ToInt32(outParams.Properties["ReturnValue"].Value)) !=
0)
Console.WriteLine("Failed to set option: {0}", ret);
else
Console.WriteLine("Option set");
}
}

Willy.
 
Back
Top