I have a windows service. It was working fine until I added code for it to begin logging. Now when I try to start the service I receive the following error:
The GBBService service on local computer started then stopped. Some
services stopped automatically if they have no work to do, for
example, the performance logs and alert service
Here is the code for my service:
- From inside project installer
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
string eventSource = "GBBServiceLog";
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
{
installer.Source = eventSource;
installer.Log = "My GBBServiceLog";
}
}
private EventLogInstaller FindInstaller(InstallerCollection installers)
{
foreach (Installer installer in installers)
{
if (installer is EventLogInstaller)
{
return (EventLogInstaller)installer;
}
EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
if (eventLogInstaller != null)
return eventLogInstaller;
}
return null;
}
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
// Start the service after installation
using (ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName))
{
sc.Start();
}
}
}
From within my service:
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBB Service Log";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("GBBService Service Started");
}
Did I do something wrong in my code?
I think the problem is you are trying to write to a EventLog that does not exist.
In the ServiceInstaller you create the EventLog My GBBServiceLog
if (installer != null)
{
installer.Source = eventSource;
installer.Log = "My GBBServiceLog";
}
But in the Service you try to write to My GBB Service Log
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBB Service Log"; <--------------
}
I think it should be:
public GBBService()
{
InitializeComponent();
EventLog.Source = eventSource;
EventLog.Log = "My GBBServiceLog";
}
Related
I am new in windows service c#. I have a class library called JobAdminLib which has a class call ArchiveAutomationAdministrator. This class has a method called CountJobs(). I have created a windows service which would run this particular method at the scheduled interval of time. But it does not seems to work for me. Log reports are saying its running but the function that the method is supposed to perform is not working.
I have attached code for reference
public class ArchiveAutomationAdministrator
{
JobRepository repository = new JobRepository();
public IEnumerable<LiveJobs> GetCurrentlyRetentionJobs(Func<LiveJobs, bool>
criteria = null)
{
return from job in repository.GetCurrentlyRetentionJobs() select job;
}
public void countJobs()
{
var count = from job in repository.GetCurrentlyRetentionJobs() select job;
int[] JobCount = new int[count.Count()];
for (int i = 1; i <= JobCount.Length; i++)
{
string jobnumber = repository.GetCurrentlyRetentionJobs().First().JobNumber;
JobAdministrator admin = new JobAdministrator(repository);
admin.ArchiveJob(jobnumber);
}
}
}
Following is my windows service
public partial class Scheduler : ServiceBase
{
private Timer timer1 = null;
public Scheduler()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("test windows service started");
}
public void timer1_Tick(object sender, ElapsedEventArgs e)
{
this.task();
Library.WriteErrorLog("Job running successfully");
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Service Stopped");
}
public void task()
{
Library.WriteErrorLog("Inside task");
ArchiveAutomationAdministrator admin = new ArchiveAutomationAdministrator();
admin.countJobs();
}
}
check if windows servers has the authority to run
if it Ok
go to windows service list
1- open run cmd
2- type services.msc
3- right click on your service name
4- in login tab click on Local System Account and Check Allow Service To Interact with desktop
That countJobs method has a crazy way of enumerating a list. Its hard to tell if this will work but try the below...
public void countJobs()
{
foreach (var job in repository.GetCurrentlyRetentionJobs())
{
Library.WriteErrorLog("Archiving job " + job.JobNumber);
string jobnumber = job.JobNumber;
JobAdministrator admin = new JobAdministrator(repository);
admin.ArchiveJob(jobnumber);
}
}
that way you will get logging inside the loop and you'll be able to tell if there is anything to actually process.
I have developed a Windows Service whose task is actually to start a host with particular url and port. Below is what I have now.
Program.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
ProjectInstaller.cs
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
WindowsDxService.cs
public partial class WindowsDxService : ServiceBase
{
public WindowsDxService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var url = "http://127.0.0.1:9000";
using (var host = new NancyHost(new Uri(url)))
{
host.Start();
}
}
}
Configuration on serviceProcessInstaller1 and serviceInstaller1 in ProjectInstaller.cs [Design] file.
serviceProcessInstaller1
Account=LocalSystem
serviceInstaller1
StartType=Automatic
Library.cs
public class Library : NancyModule
{
public Library()
{
Get["/"] = parameters =>
{
return "Hello world";
};
Get["jsontest"] = parameters =>
{
var test = new
{
Name = "Guruprasad Rao",
Twitter="#kshkrao3",
Occupation="Software Developer"
};
return Response.AsJson(test);
};
}
}
Basically I followed this tutorial which actually shows how to do it with Console application which I succeeded though, but I wanted to have this as Windows Service which actually starts a host with specified port whenever the system starts. The service is started successfully and running but whenever I browse the url in the same system its not showing up the page, which means our basic This webpage is not available message. What else configuration I have to do so as to start the host? Hoping for a help.
You are disposing the host when you start your service. I would suggest something like this:
public partial class WindowsDxService : ServiceBase
{
private Host host;
public WindowsDxService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.host = new NancyHost(...)
this.host.Start();
}
protected override void OnStop()
{
this.host.Stop();
this.host.Dispose();
}
}
You'd probably find it a lot easier to write the service if you used TopShelf library.
I have searched and searched for Windows Service information and it is mostly non-existent or outdated. Further, there is no Windows Service template in VS 2013 (that I can find).
I am making a simple performance monitor that logs to a text file the CPU and RAM. I followed a couple outdated tutorials and came up with stuff on my own.
When I try running via F5 (a coworker's suggestion) the command prompt flashes open, closes and then the program ends. I don't think the OnStart method is ever invoked.
I can get the service installed fine from the VS command prompt but when trying to start the process I get an error that it does not start in a timely manner. I have even tried enabling interaction with the desktop in the Service Manager.
I have also tried both Debug and Release builds.
I have looked at other SO questions that suggested to do all initialization in the OnStart method, which I think I do (though I may be wrong -- I am obviously still learning).
The relevant code:
namespace SystemMonitorD
{
public class SystemMonitorD : ServiceBase
{
private Timer StateTimer { get; set; }
private TimerCallback TimerDelegate { get; set; }
private SystemMonitorL SysMon { get; set; }
public SystemMonitorD()
{
ServiceName = "SystemMonitorD";
CanStop = true;
CanPauseAndContinue = true;
AutoLog = true;
}
protected override void OnStart(string[] args)
{
SysMon = new SystemMonitorL();
TimerDelegate = SysMon.Log;
StateTimer = new Timer(TimerDelegate, null, SysMon.WaitTime, SysMon.WaitTime);
}
protected override void OnStop()
{
SysMon.StatusLog("Stop");
StateTimer.Dispose();
}
protected override void OnPause()
{
SysMon.StatusLog("Pause");
StateTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
protected override void OnContinue()
{
SysMon.StatusLog("Continue");
StateTimer.Change(SysMon.WaitTime, SysMon.WaitTime);
}
public static void Main()
{
}
}
public class SystemMonitorL
{
private readonly String _fileLocation = #"C:\Users\ian.elletson\Desktop\logD.txt";
public int WaitTime { get; private set; }
private IOutput Logger { get; set; }
private List<SystemMonitor> SystemMonitors { get; set; }
public SystemMonitorL()
{
WaitTime = 1000;
Logger = new Logger(_fileLocation);
SystemMonitors = new List<SystemMonitor>
{
SystemMonitorFactory.MakeSystemMonitor("CPU"),
SystemMonitorFactory.MakeSystemMonitor("RAM")
};
Logger.WriteLine(string.Format("Polling every {0} second(s)", WaitTime / 1000));
}
public void Log(Object stateObject)
{
foreach (var monitor in SystemMonitors)
{
Logger.WriteLine(monitor.ToString());
}
}
public void StatusLog(String status)
{
String message;
switch (status)
{
case "Stop" :
message = "stopped";
break;
case "Pause" :
message = "paused";
break;
case "Continue":
message = "continued";
break;
default:
message = "ERROR";
break;
}
Logger.WriteLine(string.Format("Logging {0} at {1}", message, TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Now)));
}
}
[RunInstaller(true)]
public class SystemMonitorDInstaller : Installer
{
ServiceProcessInstaller ProcessInstaller { get; set; }
ServiceInstaller ServiceInstaller { get; set; }
public SystemMonitorDInstaller()
{
ProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller = new ServiceInstaller();
ProcessInstaller.Account = ServiceAccount.LocalSystem;
ServiceInstaller.StartType = ServiceStartMode.Manual;
ServiceInstaller.ServiceName = "SystemMonitorD";
Installers.Add(ServiceInstaller);
Installers.Add(ProcessInstaller);
}
}
}
One thing that makes life easier dealing with while debugging windows services is to use the Debug\Release flag for your service. To step through the logic as a non-service.
static void Main()
{
#if (!DEBUG)
//RELEASE FLAG
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
//DEBUG
MyService service = new MyService(); //<--Put breakpoint here before you run your service
service.OnStart(null);
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}
I discovered my problem. I was missing
ServiceBase.Run(new SystemMonitorD()); in my Main() method. That solved the problem. I found this from this MSDN link.
I have a WCF service library (MyWCFService), which uses MEF to load plugins and hosted by Windows services (All .NET 4.0). I am now trying to run it in a new AppDomain and to enable ShadowCopyFiles in a hope that I can update plugins in the runtime. Here is the code in the Windows service project.
Program.cs
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
}
MyService.cs
public partial class MyService: ServiceBase
{
internal static ServiceHost MyServiceHost = null;
public MyService()
{
// this works but is deprecated..
AppDomain.CurrentDomain.SetShadowCopyFiles();
//this is not working.. DLLs still get locked. Require for a new AppDomain
//AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if(MyServiceHost !=null)
{
MyServiceHost.Close();
}
try
{
MyServiceHost= new ServiceHost(typeof(MyWCFService));
MyServiceHost.Open();
}
catch(Exception)
{
}
}
protected override void OnStop()
{
if (MyServiceHost!= null)
{
MyServiceHost.Close();
MyServiceHost= null;
}
}
}
Is there any way of doing it? I have done a lot of search, but still don't know how to make it work with my current settings (or I just can't understand...)
I have tried to create a new AppDomain inside Main() and used
domain.DoCallBack(new CrossAppDomainDelegate(() => { ServiceBase.Run(ServicesToRun); })) to start the service but I can't start it and keep getting "Error 1053: The service did not respond to the start or control request in a timely fashion".
And then I tried to just enable Shadow copy for the current appdomain by setting AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true"; in MyWCFService.cs just before InitializeComponent(); I can start the service but the dlls are still locked. however, if I use AppDomain.CurrentDomain.SetShadowCopyFiles(); (a deprecated method) to enable the shadow copy, everything works. I'm more confused.
OK, I ended up with creating a shell/proxy class inherited from MarshalByRefObject and start the service from there, and here is the code:
ServiceShell.cs
public class ServiceShell:MarshalByRefObject
{
internal static ServiceHost MyServiceHost = null;
public void Run()
{
if (MyServiceHost != null)
{
MyServiceHost.Close();
}
try
{
MyServiceHost = new ServiceHost(typeof(MyWCFService));
MyServiceHost.Open();
}
catch (Exception)
{
}
}
public void Stop()
{
if (MyServiceHost!= null)
{
MyServiceHost.Close();
MyServiceHost = null;
}
}
}
MyService.cs
public partial class MyService: ServiceBase
{
AppDomain domain;
ServiceShell runner;
public MyService()
{
var setup = new AppDomainSetup
{
ShadowCopyFiles = "true"
};
domain = AppDomain.CreateDomain("MyServiceHostDomain", AppDomain.CurrentDomain.Evidence, setup);
runner = (ServiceShell)domain.CreateInstanceAndUnwrap
(typeof(ServiceShell).Assembly.FullName, typeof(ServiceShell).FullName);
InitializeComponent();
}
protected override void OnStart(string[] args)
{
runner.Run();
}
protected override void OnStop()
{
runner.Stop();
AppDomain.Unload(domain);
}
}
I'm working with a windows service where i want to add a GUI too it. I made a proof of concept with my service creating a ServiceHost object and hosting the WCF Named pipes service and then i Console application to consume the WCF service and also getting callback responses from the service (messages sent from server to connected clients). This works great my console application runs and get responses from the service with no interruption or delays.
However when doing the same thing in my WPF GUI application when clicking a button that then calls the WCF service it freezes the whole UI thread and then after a couple of minutes throws an exception and then the UI is updated with the message callback (server sends message to connected clients) but any return values from service is lost since the exception was thrown.
The two exception messages i have gothen is theses (the most common is the first):
1: The requesting action sent to net.pipe :/ / localhost / PipeGUI did not receive a response within the specified timeout (00:00:59.9989999). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Raise the deadline for action (by entering the channel / proxy to Icon Text Channel and set the property Operation Timeout) and verify that the service can connect to the client.
2: Communication object System.ServiceModel.Channels.ServiceChannel, can not be used for communication because it has been canceled.
Anyone got any ideas why this is happeing ?
I can post more code if neccessary.
UPDATE , added code for reference
public interface IClientCallback
{
[OperationContract(IsOneWay = true)]
void MessageRecived(string message);
}
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IClientCallback))]
public interface IPipeServiceContract
{
[OperationContract]
string Hello();
[OperationContract]
void Message(string msg);
[OperationContract(IsInitiating = true)]
void Connect();
[OperationContract(IsTerminating = true)]
void Disconnect();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true, UseSynchronizationContext = false)]
public class PipeService : IPipeServiceContract
{
List<IClientCallback> _clients = new List<IClientCallback>();
public string Hello()
{
PublishMessage("Hello World.");
return "Return from method!";
}
public void Connect()
{
_clients.Add(OperationContext.Current.GetCallbackChannel<IClientCallback>());
}
public void Disconnect()
{
IClientCallback callback = OperationContext.Current.GetCallbackChannel<IClientCallback>();
_clients.Remove(callback);
}
void PublishMessage(string message)
{
for (int i = _clients.Count - 1; i > 0; i--)
{
try
{
_clients[i].MessageRecived(message);
}
catch (CommunicationObjectAbortedException coae)
{
_clients.RemoveAt(i);
}
catch(CommunicationObjectFaultedException cofe)
{
_clients.RemoveAt(i);
}
}
}
public void Message(string msg)
{
PublishMessage(msg);
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged, IClientCallback
{
public ServiceController Service { get; set; }
protected IPipeServiceContract Proxy { get; set; }
protected DuplexChannelFactory<IPipeServiceContract> PipeFactory { get; set; }
public ObservableCollection<ServerActivityNotification> Activity { get; set; }
public override void BeginInit()
{
base.BeginInit();
PipeFactory = new DuplexChannelFactory<IPipeServiceContract>(this, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/PipeGUI"));
}
public MainWindow()
{
InitializeComponent();
Activity = new ObservableCollection<ServerActivityNotification>();
Service = ServiceController.GetServices().First(x => x.ServiceName == "Server Service");
NotifyPropertyChanged("Service");
var timer = new DispatcherTimer();
timer.Tick += new EventHandler(OnUpdate);
timer.Interval = new TimeSpan(0, 0, 0, 0, 850);
timer.Start();
if (Service.Status == ServiceControllerStatus.Running)
{
Proxy = PipeFactory.CreateChannel();
Proxy.Connect();
}
}
void OnUpdate(object sender, EventArgs e)
{
Service.Refresh();
NotifyPropertyChanged("Service");
StartButton.IsEnabled = Service.Status != ServiceControllerStatus.Running ? true : false;
StopButton.IsEnabled = Service.Status != ServiceControllerStatus.Stopped ? true : false;
if (PipeFactory != null && Service.Status == ServiceControllerStatus.Running)
{
Proxy = PipeFactory.CreateChannel();
Proxy.Connect();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
private void OnStart(object sender, RoutedEventArgs e)
{
try
{
Service.Start();
}
catch
{
Service.Refresh();
}
}
private void OnStop(object sender, RoutedEventArgs e)
{
try
{
if (Proxy != null)
{
Proxy.Disconnect();
PipeFactory.Close();
}
Service.Stop();
}
catch
{
Service.Refresh();
}
}
public void MessageRecived(string message)
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
ServerActivityNotification log = new ServerActivityNotification { Activity = message, Occured = DateTime.Now };
Activity.Add(log);
ListBoxLog.ScrollIntoView(log);
NotifyPropertyChanged("Activity");
}));
}
private void OnHello(object sender, RoutedEventArgs e)
{
try
{
Proxy.Message(txtSendMessage.Text);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}
Try setting the UseSynchronizationContext property of the service behaviour to false:
[ServiceBehavior(UseSynchronizationContext = false)]
class MyService
{
}
[ServiceContract]
public interface IMyService
{
}
I believe that by default this is set to true, so you are currently attempting to consume and run the WCF service on the same thread resulting in a deadlock.
In any case, it sounds like you are trying to consume the WCF service on the UI thread of the WPF application. Generally it is recommended that you perform potentially long running tasks on the background thread as this keeps the interface responsive even if your service call takes a few seconds/minutes.
EDIT:
I tried and succeeded in replicating your problem. Trying to call the service on the UI thread results in the UI freezing. However, when I changed my code to call the service on a background task (see below), I was able to call the service and receive the callback:
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
var myService = DuplexChannelFactory<IMyService>.CreateChannel(new CallbackImplementation(),
new WSDualHttpBinding(),
new EndpointAddress(
#"http://localhost:4653/myservice"));
myService.CallService();
string s = "";
});
}
I have to confess, I am not entirely sure why this is so, and any clarification on exactly how WCF manages the thread hosting the service instance would be great in working out why this works.