C# windows service crashes without hitting catch block - windows

I have a c# windows service that crashes without logging almost everyday after running for a few hours. Recently I added catch blocks to literally every method and still it doesn't help. Since I am using asynchronous callbacks on MSMQ, I guess there could be some multi-threading issues, but I have no clear clue. Any insight into this problem will be very helpful. Here is the pseudo code:
public MyService : ServiceBase
{
onStart()
{
try
{
someQueue.BeginReceive()
}
catch(Exception e)
{
log error and throw
}
}
void someQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
try
{
//process the message
}
catch(Exception e)
{
//log
}
finally
{
someQueue.Refresh()
someQueue.BeginReceive();
}
}
}

You can check Event Viewer to know the reason of Service getting stopped.
Open start menu and start Event Viewer, in application section you'll find the error

I know years passed since this request. But I got the same issue and no solution worked for me, other than the following.
In my case, the Event Viewer was not giving any details about the crash, and all user rights were ok.
the issue was a line of code in the OnStart method:
Debugger.Launch();
It was trying to launch the debugger, but that was not installed on that server. As such, it never completed and no exception was caught by my code.
Got rid of that, and it started working.

Related

How to make Web API Stop working on occurance of specific exception

In some scenarios I am getting exceptions in Startup.cs file.
So I want to catch that exceptions and do not show specific errors to Users.
I tried this:
I catch exceptions and throw as Custom exceptions, even though it shows stack-trace and all things into browser.
And if I left catch block empty then Web API still remains available (working).
I want to pause or stop my service for this scenarios.
Please help me.
I've No rights to comment so I'm going to write my answer here
as far as I understood your question why don't you use Application Error in global.asax file
Just like this
private void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpAntiForgeryException)
{
Response.Clear();
Server.ClearError();
Response.Redirect("/error/errorPage", true);
}
}
Hope I understood your question

How to catch System.InvalidOperationException: Connection was disconnected before invocation result was received

I´m getting this error exactly how the exception message says.
If a mobile client loses connection before the proxy.Invoke() result arrive, the exception is raised. That´s ok, but I need to catch this to avoid an app crash.
I try/catch all the proxy.Invoke() and proxy.Invoke<T>() calls, with no effect though.
How can I catch that exception?
Note: I´m using SignalR client 2.2.0 in a Xamarin client (PCL)
If you are calling your proxy.Invoke() without await proxy.Invoke() then the exception won't bubble up from the Invoke task to your executing code.
I've had to deal with this error before (with Xamarin in a PCL), and simply executing my Invoke like such worked for me:
try
{
await hubProxy.Invoke("SomeMethod", args);
}
catch (InvalidOperationException ex)
{
// Do what you need to with the exception
}
There is also a conversation about it here on the SignalR GitHub.

Jenkins reports a test build as a success, even though there are failures caused by exceptions?

I have SeleniumWebdriver/TestNG/Maven/Java continuous integration tests that are being run every time after a deploy. Sometimes an element is missing from the user interface and the tests throw an exception (which is later caught in the code, because in the catch statement I turn off the browser), so the build is marked as a success.
The strange thing is, I had failures in tests caused by exceptions before as well, and the build was still considered a successfull one.
How can I configure my maven pom.xml file or the jenkins build in order for it to mark every test that has thrown an exception, a FAILURE?
EDIT: After getting robjohncox's responce, I now have another thing I need to do:
How exactly do I throw the error again?
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
quit(driver);
sendMail();
}
Is it done this way?
throw e;
I think the problem relates to the fact that you are catching the exception in the code. Because you are handling the exception, it doesn't propagate up to your test runner, and therefore the test runner isn't aware that an exception was thrown.
After catching the exception and turning off your browser, you should re-throw the exception and then the test failures should be reported by your testing framework. The code would look something like this:
public void myTestCase() {
try {
// Do the testing
}
catch(Exception ex) {
// Turn off the browser
throw ex
}
}

How to debug a plugin in on-line version?

I've created a plugin and registered it using hte registration tool. I've also added a step that is supposed to handle a message of creation of an instance. Sadly, the intended behavior doesn't occur.
My guess is that something inside the plugin crashes but I have no idea on how to debug it. Setting up breakpoints is not going to work agains on-line version, I understand, so I'm not even trying.
For legal and technical reasons, I won't be able to lift over the solution to an on-premise installation, neither. Is guessing my only option?
For server-side (plugins) I'm using ITracingService. For client-side I log everything to console. The downside with the first is that you actually need to crash the execution to get to see anything. The downside with the latter is that plugins sometimes get executed without GUI being invoked at all.
When it comes to heavier projects, I simply set up a WCF web service that I call from the plugin and write to that. That way, on one screen, I'm executing the plugin while on the other, I'm getting a nice log file (or just put the sent information to on the screen).
You could, for instance, start with a very basic update of a field on the instance of your entity that's being created. When you have that working, you can always fall back to the last working version. If you don't even get that to work, it mean, probably, that you're setting up the plugin registration incorrectly.
A very efficient way would be to lift over the solution to an on-premise version where you have full control but I see in your question that it's not en option.
In case you could lift the solution to an on-premise version, here's a link on how to debug plugins.
Don't forget that you also have access to the ITracingService.
You can get a reference to it in your Execute method and then write to it every so often in your code to log variables or courses of action that you are attempting or have succeeded with. You can also use it to surface more valuable information when an exception occurs.
It's basically like writing to a console. Then, if anything causes the plug-in to crash at runtime then you can see everything that you've traced when you click Download Log File on the error shown to the user.
Beware though - unless your plug-in actually throws an exception (deliberate or otherwise) then you have no access to whatever was traced.
Example:
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(
typeof(IPluginExecutionContext));
// Get a reference to the tracing service.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("Getting entity from InputParameters...");
// may fail for some messages, since "Target" is not present
var myEntity = (Entity)context.InputParameters["Target"];
tracingService.Trace("Got entity OK");
// some other logic here...
}
catch (FaultException<OrganizationServiceFault> ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = (FaultException<OrganizationServiceFault>)ex.InnerException;
_trace.Trace(ex.ToString());
}
throw new InvalidPluginExecutionException(
string.Format("An error occurred in your plugin: {0}", ex));
}
catch (Exception ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = ex.InnerException;
_trace.Trace(ex.ToString());
}
throw;
}
}

How to intercept and handle all exception of Windows phone Application?

We did't have any Test team support for our Product Development.
so.
we need intercept and handle all Exception for improve User experience.
is There have any Soluction in windows phone Application?
as Fllow in app.xaml.cs file. we found :
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is QuitException)
return;
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
Yes, this should catch all the exceptions that you missed in your app. Considering that you obviously are not catching many exception somewhere else and are looking for a simple solution, this event handler might work, but I seriously don't recommend it.
This event handler catches the exceptions and then should crash/quit your app. If you handled your exceptions only here, this would lead to a huge crash count. Sometimes exceptions happen, but the app can continue working normally. That's why I recommend that you handle them as they happen in your code, and not here. That way you have a full control of how your app continues and if it continues at all, and reduce the number of "unhandled exceptions" and app crashes.
Put your code in Try-Catch Block. I was also facing such problem, but then handled by Exception Handling Method.
try
{
// your code
}
catch (Exception ex)
{
throw (ex);
}

Resources