I am trying to follow this walkthrough.
I'm using Visual Studio 2010 Premium.
The only events I see in Server Explorer or in Event Viewer are "Service started successfully." and "Service stopped successfully."
Here is the service code:
namespace MyNewService
{
public partial class MyNewService : ServiceBase
{
public MyNewService()
{
InitializeComponent();
if (!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
}
}
I had the same issue with Visual Studio 2010 Professional on Win 7 64. My newly compiled service was installed with InstallUtil.exe running as Administrator. It started and stopped correctly. No exceptions but no amount of refreshing the Event Viewer revealed a new event log. Fran71's suggestion worked for me: I closed the Event Viewer and reopened. VoilĂ , the newly created application log file appeared.
FWIW, I ran into this problem in Win8 and found that Refreshing the Event Viewer did not cause my newly created Event Log to show up, but closing and reopening Event Viewer did. Was driving me insane since no exceptions were being thrown when Creating the Event source or writing a log entry.
Probably a permissions problem. Like the commenter said, try running VStudio or your compiled service as an Administrator.
Also, you can still debug a service - put a thread.Sleep(up to 20 seconds) as your first line (to give yourself time to attach the debugger), then put a break point on the line after that. Then go Tools --> Attach To Process and select your .exe. If you get that done before the end of your Thread.Sleep() then you should break.
Keep in mind that the service must complete the OnStart within IIRC 30 seconds or Windows will think it is not responding and kill your process, so move your code out of the service start if you are going to do much debugging. Just throw a timer in there or something.
To make it easier to debug, consider moving your functionality to a DLL and then keep your service to just enough code to call into your DLL - this will ease unit testing and debugging - but don't forget that lots of things change when you're actually running it as a service.
Related
I created a windows service which watches a directory. When a file is dumped into it, it takes the data and puts it into a database. Then this file is moved to another directory and deleted. It works fine in debug mode. But when i install it on my computer it stops after throwing the data into the database and the file in question is neither moved or deleted. I suspect a permission issue is involved. I tried to create a event log:
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
So i have three questions.
(1) What could be causing my service to work as described in debug but fail when installed on my computer.(2) I have initiated a event log as shown above. But do i need to add other code to record the event of my service stopping. I presume this would be done in a 'override onShutdown' method.(3) Finally when my service stops, i want to look at the event log. But i do not know how to do this, is in administrative tools? stored as a file on some directory?
Here is edit to this post in lieu of the grateful advice given below.
try
{
File.Move(e.FullPath, finalString);
File.Delete(e.FullPath);
}
catch(Exception q)
{
EventLog.WriteEntry("MySource", q.ToString(), EventLogEntryType.Error);
using (StreamWriter w = new StreamWriter(ConfigurationManager.AppSettings["fmd"], true))
{
w.Write(DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss"));
w.Write(q.ToString());
}
}
As per suggestion i put a try-catch around the file move and delete plus i added a OnShutdown method:
protected override void OnShutdown()
{
using (StreamWriter w = new StreamWriter(ConfigurationManager.AppSettings["ond"], true))
{
w.Write("stop OnShutdown");
}
//EventLog.WriteEntry("MySource", message, EventLogEntryType.Error);
}
I do not know how to pass any system error message to the shutdown method, so any advice appreciated. When i installed my modified code as a service, it again stopped before moving or deleting the files. Neither of my two logs accessed by a stream recorded anything. Plus the event viewer showed nothing either?
You can write as following,
if (!EventLog.SourceExists("MySource"))
EventLog.CreateEventSource("MySource", "Application");
EventLog.WriteEntry("MySource", message, EventLogEntryType.Error);
to view the event log messages, Goto Administrator Tools -> Event Viewer and look for the source you have created. or Just simply type eventvwr in run window.
When Services installed, it works under SYSTEM User account where service might not have access to some resources. Please put logs and see where exactly the issue is.
If you service installed in your development machine, use attach to process option under DEBUG Menu in Visual Studio to find out.
What could be causing my service to work as described in debug but fail when installed on my computer?
Permissions. The service is likely running under LocalSystem or Network Service if you didn't provide a different identity.
I have initiated a event log as shown above. But do i need to add other code to record the event of my service stopping. I presume this would be done in a 'override onShutdown' method?
Yes, you're assumption is correct.
Finally when my service stops, i want to look at the event log. But i do not know how to do this, is in administrative tools?
Just hit Windows Key+R to get the Run dialog and type eventvwr.
Well i found the reason for all the commotion. I eventually found some logs in the event viewer. They were listed in Administrative events in custom logs. There were three error logs: .Net runtime; Application error & Service Control Manager. In '.Net Runtime' the stack showed a unhandled exception for system.windows.forms. I stupidly included a pop up box in my release version. But even when i commented this away; i got a error. So i went back and found other message boxes, primarily in try catch statements. Removed these and solved the issue.
I was following the instructions in official msdn to debug my windows service:
http://msdn.microsoft.com/en-us/library/7a50syb3.aspx
however, after I attached the process to debugger and tried to Stop the service in Service Control Manager, the process just disappeared and the debugger thus exit without hitting any break point.
I chose the process name as the executable of my window service application.
Did i do anything wrong?
When the debugger is attached to a process, clicking the blue Stop button usually terminates the process, that's why you saw the service process disappear. Of course at that point, breakpoints won't be hit since all the modules for that process are unloaded. If you want to detach from the service and still let it run, in the Debug menu in Visual Studio click Detach All.
It feels like though you're having issue debugging your service. Debugging Windows services is a little different than debugging other projects in Visual Studio. Just clicking the green "play" button doesn't automatically put the service in debug mode. In many cases, the breakpoint needs to be set in the OnStart() method since that is where the problem occurs. They way I always debug Windows services is by putting a Thread.Sleep() statement on the top of the OnStart() method, before the program attempts to do anything else. You can specify how long you want the main thread to be asleep, and then use this time to attach the VS debugger to the Windows service. For example:
protected override void OnStart(string[] args)
{
Thread.Sleep(10000);
// Rest of the code
}
Will give you 10 seconds to attach to the service. Install the service, start in in the Services window, and then attach to it.
Can you try adding this at the top of your service....
System.Diagnostics.Debugger.Launch();
or if possible, convert your service to use http://topshelf-project.com/. Which supports debugging in visual studio. And has an added benfit of allowing the service to "run as console app" for debugging.
What would be a short example with a Windows service and how to install and run it?
I've searched on the Internet, but what I've tried didn't have anything written on the On Start method. Plus, when I've tried to install it the error OpenSCManager keeps popping up.
Find install util at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe
Then run InstallUtil.exe "c:\myservice.exe"
Go to services.msc and then locate and start your service
Here are some examples about how to write and install a Windows service in C#:
A Windows Service Application
Simple Windows Service Sample
ASP.NET Tutorials : Creating Windows Service in C#
Creating a Windows Service in C#
How to Create and Work With Windows Services in C#
Creating a Simple Windows Service in C#
My answer to this question gives you step-by-step instructions for creating a Windows service in C#.
Easiest language for creating a Windows service
My answer to this question shows you have to modify the service so that it can install and uninstall itself from the command line.
How to make a .NET Windows Service start right after the installation?
InstallUtil.exe has been part of .NET since 1.1, so it should be on your system. However, you likely can't use it from a 'normal' command prompt. If you have Visual Studio installed, open the Visual Studio command prompt. That'll define the appropriate environment variables that make InstallUtil accessible without path information.
The OnStart() callback gives you an opportunity to start the business logic of your service. If you don't do anything in the OnStart() callback, your service will immediately shut down. Typically, you'll start a thread that performs the work you're interested in. Here is a small example to show you what it looks like.
private static System.Timers.Timer _timer;
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Write a message to the event log.
string msg = String.Format("The Elapsed event was raised at {0}", e.SignalTime);
EventLog.WriteEntry(msg, EventLogEntryType.Information);
}
protected override void OnStart(string[] args)
{
// Create a timer with a 10-econd interval.
_timer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Signal the timer to raise Elapsed events every 10 seconds.
_timer.Start();
}
protected override void OnStop()
{
// Stop and dispose of the timer.
_timer.Stop();
_timer.Dispose();
}
Doing something like this will effectively keep your service running until it shuts down. Hope this helps.
I'm not using a Windows Form Timer--I'm using a System.Threading.Timer. No exceptions are raised when instantiating the timer, and to my understanding, System.Threading.Timer's do not need to be explicitly started.
I've tried using a System.Timers.Timer (See comments in code) and changing the signature of the callback with no luck. Inside the callback method, I stubbed in some EventLog writes, and not even the first writes to the event log. All I see in the Event Log is MyService.OnStart fired followed by MyService started (these are both from the OnStart event). Why aren't the timer callback events firing?
public partial class MyService : ServiceBase
{
private static System.Threading.Timer timer;
public MyService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MyService"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MyService", "MyServiceLog");
}
eventLog1.Source = "MyService";
eventLog1.Log = "MyServiceLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("MyService.OnStart event fired");
// setup timer to poll and execute an event
//Timer timer = new Timer(new TimerCallback(CheckCalls), null, 0, 10000);
try
{
timer = new System.Threading.Timer(new System.Threading.TimerCallback(CheckCalls), null, 0, 10000);
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message + " Stacktrace: " + ex.StackTrace);
}
eventLog1.WriteEntry("MyServicestarted");
GC.KeepAlive(timer);
}
}
and the callback method, CheckCalls:
private static void CheckCalls(object objectState)
{
EventLog eventLog1 = new EventLog("MyServiceLog", "DAVIDWIN7", "MyService");
eventLog1.WriteEntry("MyService is polling");
}
Arg--For some reason when you Build->Clean Solution, then Build->Build Solution, Visual Studio does not rebuild your setup project. I believe my code above works fine, but I had tried many different fixes without explicitly right-clicking the setup project and selecting Build.
I thought Clean Solution would force all projects to be rebuilt on next build???
it's got to be an error with the timer thread writing to the event log.
I got it working on my computer but I replaced all the eventlog with Console.writeline because I didn't want to mess with my event log permissions.
Did you check if the account under which the service is running has access to the eventlog?
Please can you go and download this test application which mirrors exactly the scenario you are talking about:
https://docs.google.com/leaf?id=0Bw_NnV9fhgmgMThjNDgzOTgtODNiOC00NDE1LWEyMTYtNzVhOTMyNzlmZjZk&hl=en&authkey=CMuupNkC
I've just created this. It's a service that effectively uses the same code as you. The only difference is that I have used the toolbox to drag and drop an eventlog on to the service designer - setting the log to 'Application' and the source to 'Service1'.
Open a VS command prompt at the project folder, go to bin\debug and run installutil windowsservice1.exe - specifying some credentials (so you're installing the dev service directly from your bin output folder).
Start the service and monitor the application log - it will write every ten seconds as you expect to see from your code.
Assuming this works on your machine(tm) like it does on my machine(tm) then I would suggest that the problem is the event logging itself, or, horror of horrors - your service isn't running from your build output folder (most of the devs in my team insist on running their dev services from a different folder and then swear when they forget to actually update the binaries when they start debugging it!).
As I suggested in my comment, you really need to be able to attach the debugger to this and breakpoint the callback to verify that it's not getting fired. I never rely on the eventlog for diagnostics messages simply because the log might be full, or permissions might prevent you etc etc - it might be a windows platform component, but it's not as reliable as I would like.
The debugger, however, rarely lies (unless the deployed binaries are out of date ;) ). Compile a debug build of your project and make sure it's the one that is installed in the services list, start it up and then attach directly to the service exe (there's no point trying to attach to [service].vshost.exe - VS cannot auto-attach or host windows services as the only way they can run is via svchost.exe shelling them).
Hope this helps.
I installed my Windows Forms .NET 3.5 application and it installed correctly. When trying to run it the application crashes and the typical Microsoft Windows error dialog shows. You know the one that asks you to send an error report.
My question is, how can I see what actually caused the program to fail to launch?
The application runs well on my development machine, the problem is when running on another computer when installed with the Setup file I created.
Is there a way to see the 'innerException' when not running on a development machine?
Besides checking the Windows Event Viewer (from Computer Management) you could also try to build some error logging around your program. If you extend your Main() method to contain the following lines you will be able to get some further information about the cause of the program failure:
[STAThread]
static void Main()
{
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new Form1());
}
private static void Application_ThreadException(
object sender, ThreadExceptionEventArgs e)
{
//Log error here using e.Exception
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//Log error here using (Exception)e.ExceptionObjecte.Exception
}
You could for example log the error to a simple text file using a StreamWriter:
string dateStr = DateTime.Now.ToString("yyyy-MM-dd");
StreamWriter sw = File.AppendText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrorLog_" + dateStr + ".log"));
sw.WriteLine(exception);
These are the default options you have:
(If possible) change the application so that it logs errors and warnings, using e.g. log4net or the windows event log, then redistribute your app.
If the first option is not possible, you'll have to check out more advanced debugging: run-time debugging on the client machine. One way is to use WinDbg with the .NET extensions (SOS) or related tools from Debugging Tools for Windows. You can set it up on a client machine without running an installer, so it should have little or no side effects (as opposed to the non-option of setting up Visual Studio). One article on this is here, where they're debugging a crash dump file. Here is another article on the topic. You'll find endless resources on this googling - the topic is not simple but I recommend you look into it.
(If you need the results now, and don't have time to dig into advanced debugging with WinDbg and related tools at the moment, I would just add some tracing into the application.)
Did you check the EventLog? If your program starts, and then begin crash. put EventLog.WriteEntry(exceptionMessage). If not, best way to see it is EventLog.