Hi Willy and thanks for your reply,
Hi everybody,
Willy Denoyette [MVP] ha scritto:
Please upload all of your code
[cut]
I'll post my code here.
Please note that's been compiled on the FW1.1 (using MSBee) and that I used
c:\Windows\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe MyService.exe
to install it and then >sc start MyService to start it.
Thanks again,
Kind regards,
Giulio
--
//Service class
public class MyService : ServiceBase
{
private MyListener listener;
private DefaultTraceListener dtl;
public MyService()
{
dtl = new DefaultTraceListener();
dtl.WriteLine("creating a new listener...");
listener = new MyListener();
InitializeComponents();
}
private void InitializeComponents()
{
dtl.WriteLine("initializing service components...");
AutoLog = true;
CanPauseAndContinue = true;
CanShutdown = true;
CanStop = true;
ServiceName = "MyService";
if (!EventLog.SourceExists("MyService"))
EventLog.CreateEventSource("MyService", "MyService_Log");
}
protected override void OnStart(string[] args)
{
dtl.WriteLine("OnStart() --> starting listening...");
Thread startListening = new Thread(new
ThreadStart(listener.StartListening));
startListening.IsBackground = true;
startListening.Start();
return;
}
protected override void OnStop()
{
dtl.WriteLine("OnStop() --> stopping listening...");
listener.StopListening();
}
protected override void Dispose(bool disposing)
{
dtl.WriteLine("Dispose() --> stopping listening...");
listener.StopListening();
dtl.WriteLine("disposeing components...");
base.Dispose(disposing);
dtl.WriteLine("done");
}
static void Main(string[] args)
{
#if (!DEBUG)
//ServiceBase.Run(new ServiceBase[] { new MyService() });
System.ServiceProcess.ServiceBase.Run(new MyService());
#else
LicenseManagerService service = new LicenseManagerService();
service.OnStart(null);
Thread.Sleep(Timeout.Infinite);
#endif
}
}
//listener class;
public class LicenseManagerListener
{
private ManualResetEvent acceptDone; //a manual reset event
protected Thread listeningThread;
private System.Diagnostics.DefaultTraceListener dtl;
public LicenseManagerListener()
{
dtl = new System.Diagnostics.DefaultTraceListener();
dtl.WriteLine("-- creating a new listener...");
acceptDone = new ManualResetEvent(false);
}
public void StartListening()
{
dtl.WriteLine("starting the listening thread...");
listeningThread = new Thread(new ThreadStart(ListeningThread));
listeningThread.IsBackground = true;
listeningThread.Start();
}
public void StopListening()
{
dtl.WriteLine("stopping the listening thread...");
listeningThread.Abort();
}
private void ListeningThread()
{
dtl.WriteLine("listening thread");
dtl.WriteLine("creating endpoint...");
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 6677);
dtl.WriteLine("creating a new socket...");
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
dtl.WriteLine("binding socket to endopoint...");
listener.Bind(localEndPoint);
listener.Listen(10000);
dtl.WriteLine("socket :: start listening...");
while (true)
{
acceptDone.Reset();
dtl.WriteLine("acceptDone -- > reset");
dtl.WriteLine("waiting for a new connection...");
listener.BeginAccept(new
AsyncCallback(AcceptCallback), listener);
dtl.WriteLine("acceptDone --> wait one");
acceptDone.WaitOne();
}
}
catch (Exception e)
{
dtl.WriteLine("exception : \n " + e.Message);
Console.WriteLine(e.Message);
throw e;
}
}
/*
* the AcceptCallback method implements a callback to accepting
incoming connections;
*/
private void AcceptCallback(IAsyncResult ar)
{
dtl.WriteLine("accept callback...");
acceptDone.Set();
dtl.WriteLine("acceptD-one --> set");
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
dtl.WriteLine("finished");
}
}
//service installer
[RunInstaller(true)]
public class LicenseManagerServiceInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller serviceProcessInstaller;
private Container components;
public LicenseManagerServiceInstaller()
{
InitializeComponents();
}
private void InitializeComponents()
{
serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller = new ServiceInstaller();
serviceInstaller.ServiceName = "SpeechDrawLicenseManager";
serviceInstaller.StartType = ServiceStartMode.Manual;
Installers.AddRange(new Installer[] { serviceInstaller,
serviceProcessInstaller });
Committed += new
InstallEventHandler(LicenseManagerServiceInstaller_Committed);
Committing += new
InstallEventHandler(LicenseManagerServiceInstaller_Committing);
}
void LicenseManagerServiceInstaller_Committing(object sender,
InstallEventArgs e)
{
Console.WriteLine("...committing event occurred...");
}
void LicenseManagerServiceInstaller_Committed(object sender,
InstallEventArgs e)
{
Console.WriteLine("...committed event occurred...");
}
public override void Install(System.Collections.IDictionary
stateSaver)
{
base.Install(stateSaver);
}
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);
}
public override void Rollback(System.Collections.IDictionary
savedState)
{
base.Rollback(savedState);
}
}