none
Exceptions in OnValidate lose their stacktrace

    Question

  • Hi everyone

    I have a problem. When an exception is thrown within the OnValidate method of a record object, the stacktrace does not show the line where the exception was thrown, but rather it shows the line of MyBase.SubmitChanges in the overridden SubmitChanges.

    Here is a sample stack trace

       at System.Data.Linq.ChangeProcessor.SendOnValidate(MetaType type, TrackedObject item, ChangeAction changeAction)
       at System.Data.Linq.ChangeProcessor.ValidateAll(IEnumerable`1 list)
       at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
       at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
       at DataClassesDataContext.SubmitChanges(ConflictMode failureMode) in D:\MySite\DataContext.vb:line 44

    This is pretty useless. I need to know where the exception was actually thrown.

    Thank you very much,

    Wednesday, April 04, 2012 10:50 AM

All replies

  • Hi YismanOI,

    Welcome!

    OnValidate method is a partial method that will be invoked prior to the entity's values being persisted into the database. Within this validation method I can check any of the entity's property values (and even obtain read-only access to its associated objects), and raise an exception as needed if the values are incorrect.  Any exceptions raised from the OnValidate() method will abort any changes from being persisted in the database, and rollback all other changes in the transaction.

    I'd like to queto @ScottGu's reply:  http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx

    -----------------------------------------

    OnValidate is a partial method, and is called from the LINQ to SQL framework automatically.  If you look in the code-generated by the LINQ to SQL designer I think you'll see the code where this happens.

    -----------------------------------------

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 05, 2012 8:12 AM
    Moderator
  • hi

    thanks for posting

    i remember you names from other threads, and youve always been very helpful!

    i dont quite get your answer though, when im running in debug by me, then VS crashes on the correct line, so its not a problem. my issue is when it runs live on the server, my error logger always gives me the non-descript stacktrace as in my first post. which makes it very hard to find the issue

    to debug , i have to connect my local copy of the site, to the remote db, and manually retrace the steps, instead of just looking in my log and find the offending line of code

    thank you very much

    Friday, April 06, 2012 6:44 AM
  • Hi YismanOI,

    Thanks for your feedback.

    OnValidate method is called by LINQ to SQL(SubmitChanges). If there is an exception in validate, it will break the SubmitChanges method. I think you can try to view is there any messages innerexception. I know a debugging tool, I'd like to share some to you:

    Capture Dump and use SOS.dll to debug: http://msdn.microsoft.com/en-us/library/d5zhxt22(v=vs.100).aspx 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 10, 2012 8:28 AM
    Moderator
  • hi

    thanks for posting again

    the problem is that there is no inner exception

    the inner exception is nothing

    when the debugger crashes, its on the correct line, and the details have the correct stacktrace

    but once it bubbles up to submitchanges, even though the exception message stays intact, the stacktrace is lost

    so the question remains how can we preserve the stacktrace, while it bubbles up to submitchanges?

    either in the main stacktrace or an innerexception or some other mechanism?

    about sos.dll, im aware of all that, but that wont help me in production, i must have my production app logging all exception, with their CORRECT stacktrace

    thank you very much

    awaiting your insight....

    Monday, April 16, 2012 1:17 PM
  • Hi YismanOI,

    Thanks for your feedback.

    I will do some pending research and come back A.S.A.P, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 19, 2012 8:24 AM
    Moderator
  • found anything?

    meanwhile im forced to add a try catch to each and every OnValidate

    in the catch i do a rethrow http://weblogs.asp.net/fmarguerie/archive/2008/01/02/rethrowing-exceptions-and-preserving-the-full-call-stack-trace.aspx

    this way i get the correct stacktrace in my err log

    if you have a idea that is more general/clean, id be very interested in hearing about it

    thank you very much!

    dunno what id do without ye! :-)

    Tuesday, April 24, 2012 6:47 AM