What is the reason that the exception thrown by the NHibernate LINQ provider will not be caught by the catch ? As far as I know, this is not a thread. This is a .NET 4.5.2 x64 application for Windows Service running on Windows Server 2008 R2 + SQL Server 2008 R2.
The code basically does a little URL reduction, so this method just checks to see if there is a mapping for the URL.
The call code looks something like this:
// triggered by a System.Timers.Timer event try { // the purpose of this hash is to let SQL Server // use a simple index, instead of a full-text search var hash = UrlMapping.CalculateHash(url); var id = Session // <-- NHibernate.ISession .Query<UrlMapping>() .Where(map => map.UrlHash == hash && map.Url == url) .Select(map => (long?)map.Id) .FirstOrDefault(); return id; } catch (Exception ex) { Log.Error(ex); }
I donβt even see what could fail in the LINQ Provider, since the request is quite simple, but in any case the application crashed and the exception was not caught by the handler (I also did not have an AppDomain.UnhandledException attached in this application), so the only information The one I got after this was from the event log:
Application: MyApp.ServiceRunner.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.Reflection.TargetInvocationException Stack: at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[]) at System.Delegate.DynamicInvokeImpl(System.Object[]) at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NHibernate.Linq.NhLinqExpression, NHibernate.IQuery, NHibernate.Linq.NhLinqExpression) at NHibernate.Linq.DefaultQueryProvider.Execute(System.Linq.Expressions.Expression) at System.Linq.Queryable.FirstOrDefault(System.Linq.IQueryable`1<System.Nullable`1<Int64>>) at MyApp.Data.Repositories.UrlMappingRepo.GetKeyForUrl(System.String) at MyApp.Data.Repositories.UrlMappingRepo.GetOrCreateKeyForUrl(System.String, System.String) at MyApp.Timers.Timer.OnElapsed(System.DateTime) at MyApp.Timers.Timer.FireWithExceptionHandling(System.DateTime)
(Update)
Thanks to the @ starlight54 comment pointing to this thread (System.Reflection.TargetInvocationException was not caught), I noticed that an additional entry appeared in the event log, from the Windows Error Reporting Service, which said that AccessViolationException actually occurred:
Fault bucket , type 0 Event Name: CLR20r3 Response: Not available Cab Id: 0 Problem signature: P1: MyApp.ServiceRunner.exe P2: 1.1.0.1436 P3: 57ed0aa0 P4: System.Core P5: 4.0.30319.34209 P6: 53489a70 P7: 39c P8: 54 P9: System.AccessViolationException P10:
(Update 2)
It seems that the first exception is registered as a "red herring", i.e. is not the actual point where the exception occurs, and since the .NET 4 AccessViolationException does not get caught unless the exception handler method is decorated with both [SecurityCritical] and [HandleProcessCorruptedStateExceptions] . So I added them according to the answers in this thread , and now I'm waiting for the exception to occur.