3

I have a WebAPI which is having the following piece of code in which there is Null reference exception in the code written in try block which is getting logged using my logger.

But in the TargetSite of the Exception getting logged, I am receiving Void MoveNext() instead of the method name in which this code is written.

What could be the reason for the same??

public async Task<ResponseStatus> ProcessRCSCNotification(IList<RecurringSubscriptionModelV2> recurringSubscriptionList, string appCD)
    {
            foreach (var model in modelList)
{
  // Some code
  try
  {
     // Exception occurs here
  }
  catch (Exception ex)
  {
    // Logger is logging exception here
  }
}
        return null;
    }
Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
  • are you sure the `foreach` is around (outside) the `try/catch`? – René Vogt Jun 12 '18 at 11:15
  • Yes sir that is for sure :) – Ipsit Gaur Jun 12 '18 at 11:15
  • How are you getting your method name? – SᴇM Jun 12 '18 at 11:17
  • I guess you need to post some more of the code. The first `void MoveNext()` I can think of that could be called here could be the `MoveNext` of a compiler-generated state-machine if you `await` something in the `try` block. – René Vogt Jun 12 '18 at 11:17
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – SᴇM Jun 12 '18 at 11:18
  • 4
    @SeM I don't think OP is asking about the NRE itself, but about the confusing `TargetSite` property of the exception. – René Vogt Jun 12 '18 at 11:19
  • @RenéVogt I have updated code and error is getting logged from the inside catch in the code – Ipsit Gaur Jun 12 '18 at 11:21
  • The method that crashes is either a generator (using `yield return`) or an async/await method. Both of these are rewritten to a state machine where the method that does the legwork is named `MoveNext`. – Lasse V. Karlsen Jun 12 '18 at 11:22
  • @RenéVogt Yes, I was on my way to close the question for the same reason when I reread it. This question is only motivated by an exception being thrown in the code but is not about the cause of the exception, only about the odd stacktrace/TargetSite being reported. As such it is not a NRE duplicate at all. – Lasse V. Karlsen Jun 12 '18 at 11:26

2 Answers2

9

You have an async method with several awaits. The compiler transform this whole method into a state machine and your method ends up actually only calling this state machine.

This state machine class has the mentioned MoveNext() method that now contains all the work you actually wanted to do.

To analyze your NullReferenceException you should rather check the StackTrace property of the exception than the TargetSite.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
5

The method is an async/await method.

This kind of method is rewritten to a state machine with a MoveNext method. That's why your stack trace will identify this method as the one throwing that exception.

.NET Core 2.0 or 2.1 have either built-in or an extra nuget package that will fix stack traces like this to mention the actual method instead of the generated one.

You can read more about this here: Age of Ascent: Stacktrace improvements in .NET Core 2.1.

It might not fixup the TargetSite however, and I don't think it will handle .NET Framework.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825