I have to use a system file Watcher into a Windows service.
I write a wrap class and I put it into the service class as shown below :
public partial class CaptureRecordingsService : ServiceBase
{
FileSystemWatcherWrapper myFSWrapper;
public CaptureRecordingsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
myFSWrapper = new FileSystemWatcherWrapper();
}
protected override void OnStop()
{
}
}
}
The class works done in Windows form but in the service the methods are not called.
Can someone help me?
Related
My application is not firing the event AfterSessionComplete. Code Below
fiddler.cs
namespace proj
{
public static class Fiddler
{
public static void start()
{
startProxy();
}
public static void startProxy()
{
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.Startup(8888, true, true, true);
}
public static void FiddlerApplication_AfterSessionComplete(Session sess)
{
//Working aftersessioncomplete
}
}
}
Service1.cs
namespace Proj
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Fiddler.start();
}
protected override void OnStop()
{
}
internal void TestStartupAndStop(string[] args)
{
this.OnStart(args); //use to debug
//For commandLine
}
}
}
program.cs
namespace Proj
{
static class Program
{
static void Main(string[] args)
{
if (Environment.UserInteractive)
{
Service1 service1 = new Service1();
service1.TestStartupAndStop(args);
}
else
{
}
}
}
}
I'm creating a windows service but I was facing a debugging issue, that's why I use the console application debug to check my code working or not.
I added break point at aftersessioncomplete event when I get to know that FiddlerApplication.AfterSessionComplete is not firing. It stops the application without going on public static void FiddlerApplication_AfterSessionComplete(Session sess)
Anyone can help? or faced same issue?
After Session cannot fire in window service because of the Certificate popup the certificate as for GUI which windows service cannot provide so the code stuck at certificate installation and not firing after session events.
To work out with this one keep in mind to use console app and hide console app after installation of certificate
I have created a custom event log and would like all my applications to write to the same event log. As you can see below in the image attached, DistributedCOM and Svc Ctrl Mgr are 2 sources writing to the same event log System.
Similarly, I have 2 services that I want to write to the same eventLog.
I tried doing that by creating one event log and passing different source names from the 2 Windows Services that I have created. But I find only one Service writing to the log while the other doesn't.
Below is the class library that I created for Event Log.
public class EventLogger
{
private EventLog eventLog1 = new System.Diagnostics.EventLog();
public EventLogger(string logSource)
{
if (!System.Diagnostics.EventLog.SourceExists(logSource))
{
System.Diagnostics.EventLog.CreateEventSource(logSource, "SampleLog");
}
eventLog1.Source = logSource;
eventLog1.Log = "SampleLog";
}
public void WriteLog(string message)
{
eventLog1.WriteEntry(message);
}
Created 1st Windows Service
public partial class Service1 : ServiceBase
{
private EventLogger.EventLogger eventLog;
public Service1()
{
InitializeComponent();
eventLog = new EventLogger.EventLogger("WinService1");
}
protected override void OnStart(string[] args)
{
try
{
eventLog.WriteLog("Service started in 1st");
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.ToString());
}
}
protected override void OnStop()
{
eventLog.WriteLog("Service stopped");
}
}
Created the 2nd windows Service as well, as above.
public partial class Service2 : ServiceBase
{
private EventLogger.EventLogger eventLog;
public Service2()
{
InitializeComponent();
eventLog = new EventLogger.EventLogger("WinService2");
}
protected override void OnStart(string[] args)
{
try
{
eventLog.WriteLog("Service started in 2nd");
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.ToString());
}
}
protected override void OnStop()
{
eventLog.WriteLog("Service stopped");
}
}
Service1 doesn't seem to log anything, whereas, I can see the logs for Service2. I might be doing a lot of things incorrectly here. Please help me in finding a solution to this. Also, if this can be achieved by using log4Net then solutions with respect to that are welcome as well. thanks in advance.
EDIT: Also, when I try to stop the services, Service 1 fails to stop and throws an error. Image given below.
EDIT 2: Just changed the constructor of the EventLogger class as below and then it worked!! I am not entirely sure if this was the actual cause for the improper functioning. And I'm not quite sure if it had anything to do with the setting of the Log property either. Any light thrown on this by any one of you would be appreciated. I would like to understand better as to what exactly happened here. Thanks.
string logName = "NewLog";
public EventLogger(string logSource)
{
if (!System.Diagnostics.EventLog.SourceExists(logSource))
{
System.Diagnostics.EventLog.CreateEventSource(logSource, logName);
}
eventLog1.Source = logSource;
}
just wondering where to put my code so that it executes after my service starts.
currently i have my code like this
namespace sendMailService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.sendMail();
}
protected override void OnStop()
{
}
private void sendMail()
{
// My actual service code
}
}
}
When i start the service, it wouldn't get to "Running" mode till the this.sendMail() completes. Incidentally, the last line of the sendMail() function is to stop() the service. So, the service crashes by the time it executes the OnStart method.
I know why its happening like this (because the code is in OnStart). The question is where to call the sendMail() so that the Service gets to running mode and then execute the function.
You can decorate your code as to fix your problem.
namespace sendMailService
{
public partial class Service1 : ServiceBase
{
private System.Threading.Timer IntervalTimer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
TimeSpan tsInterval = new TimeSpan(0, 0, Properties.Settings.Default.PollingFreqInSec);
IntervalTimer = new System.Threading.Timer(
new System.Threading.TimerCallback(IntervalTimer_Elapsed)
, null, tsInterval, tsInterval);
}
protected override void OnStop()
{
IntervalTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
IntervalTimer.Dispose();
IntervalTimer = null;
}
private void IntervalTimer_Elapsed(object state)
{
// Do the thing that needs doing every few minutes...
sendMail();
}
}
}
Is it possible to have an application that runs as a service if it is registered as such but if it is double clicked simply starts a regular interactive application?
Yes. You can use the Environment.UserInteractive variable. You will need to create a small wrapper around your service to expose the OnStart() and OnStop() methods since they are protected.
var service = new MyService();
if (Environment.UserInteractive)
{
service.Start(args);
Console.WriteLine("Press any key to stop program");
Console.Read();
service.Stop();
}
else
{
ServiceBase.Run(service);
}
Wrapper Class (Make sure to extend ServiceBase)
public partial class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
//start code
}
protected override void OnStop()
{
//stopcode
}
public void Start(string[] args)
{
OnStart(args);
}
}
I'm trying to write simple wcf service self hosted in windows service
But ServiceHandle of windows service is always 0
I need to detect hardware change using RegisterDeviceNotification
One of it's parameters is Handle, in my case is ServiceHandle
public partial class MyService : ServiceBase, IMyService
{
private ServiceHost host;
public static void Main()
{
ServiceBase.Run(new MyService());
}
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
host = new ServiceHost(typeof(MyService), new Uri(#"net.pipe://localhost/MyService"));
host.Open();
}
catch (Exception e)
{
EventLog.WriteEntry("MyService:", e.Message);
}
}
protected override void OnStop()
{
host.Close();
}
#region IMyService Members
public void Register()
{
//Here the ServiceHost is 0
}
#endregion
}
What can cause the problem?
Thanks
The ServiceHandle - no matter what value - is not required to host a WCF service as a Windows service. Simply instantiate the ServiceHost in OnStart and close it in OnStop and you should be fine.
This is why ServiceHandle is always 0 in your case:
Your Windows service class implements your WCF service contract. This is not a good thing and is also the cause for the ServiceHandle property being 0. For every call to the WCF service a new instance of the MyService class is instantiated (if you didn't change the defaults). This instance is a normal instance of the class that doesn't know it's a Windows Service, so all Windows Service related properties have their default values. Only the instance that's created by the Windows Service manager has all the appropriate properties set.
You can try for yourself: In OnStart, insert the following line and inspect the value for myServiceVar.ServiceHandle. You'll see it is 0:
MyService myServiceVar = new MyService();
What you really want to do is the following: Have a different class implement the service contract, for example like that:
public class MyWCFService : IMyService
{
public static IntPtr ServiceHandle;
public void Register()
{
// Use MyWCFService.ServiceHandle here
}
}
In the OnStart method, set the ServiceHandle variable of MyWCFService:
protected override void OnStart(string[] args)
{
try
{
MyWCFService.ServiceHandle = this.ServiceHandle;
host = new ServiceHost(typeof(MyWCFService), new Uri(#"net.pipe://localhost/MyService"));
host.Open();
}
catch (Exception e)
{
EventLog.WriteEntry("MyWCFService:", e.Message);
}
}