29485

Exception line number in CSharpScript.EvaluateAsync

Question:

I'm evaluating an script using the CSharpScript.EvaluatyAsync<T> method and passing some C# code. I can easily see the line number of errors when there is a parsing issue, e.g. a syntax error, but when there is a runtime exception all I get is an AggregateException wrapping my exception (NullReferenceException) in this case, but there is no clue of how to get the line number for me (3 in this example below).

Console.WriteLine(CSharpScript.EvaluateAsync<int>( @"string s = null; // some comment at line 2 var upper = s.ToUpper(); // Null reference exception at line 3 // more code").Result);

EDIT:

I've been researching this and found that the Scripting API creates an assembly without pdb information <a href="http://source.roslyn.io/#Microsoft.CodeAnalysis.Scripting/ScriptBuilder.cs,d25524edca91053f,references" rel="nofollow" title="here line 127">here line 127</a> so this will be impossible to know where the exception happened. Am I right?

Answer1:

In some version of CSharpScript the team added a solution: now you can add ScriptOptions.Default.WithEmitDebugInformation(true) to the EvaluateAsync method.

See my test cases below on how to extract the exception line number:

[TestMethod] public void LineNumberInStackTrace() { try { var result = CSharpScript.EvaluateAsync<int>( @"string s = null; // some comment at line 2 var upper = s.ToUpper(); // Null reference exception at line 3 // more code", ScriptOptions.Default.WithEmitDebugInformation(true)).Result; } catch (AggregateException e) { if (e.InnerException is NullReferenceException inner) { var startIndex = inner.StackTrace.IndexOf(":line ", StringComparison.Ordinal) + 6; var lineNumberStr = inner.StackTrace.Substring( startIndex, inner.StackTrace.IndexOf("\r", StringComparison.Ordinal) - startIndex); var lineNumber = Int32.Parse(lineNumberStr); Assert.AreEqual(3, lineNumber); return; } } Assert.Fail(); } [TestMethod] public void LineNumberNotInStackTrace() { try { var result = CSharpScript.EvaluateAsync<int>( @"string s = null; // some comment at line 2 var upper = s.ToUpper(); // Null reference exception at line 3 // more code").Result; } catch (AggregateException e) { if (e.InnerException is NullReferenceException inner) { var startIndex = inner.StackTrace.IndexOf(":line ", StringComparison.Ordinal); Assert.AreEqual(-1, startIndex); return; } } Assert.Fail(); }

Answer2:

In this circumstance, you'll probably want to look at the information inside of the AggregateException.InnerExceptions property.

Recommend

  • How to read key/value in xml file
  • NSubstitute When…Do definition is not getting overriden by subsequent definitions
  • The process cannot access the file because it is being used by another process
  • Set network configuration programmatically c#
  • Should try-catch be avoided for known cases
  • Call C# class from Java class
  • GetInterfaces() returns generic interface type with FullName = null
  • Create CSV file from c#: extra character in excel
  • What is using connection in the code
  • Split a string while preserving values wrapped in quotes (.NET)
  • FOSUserBundle force user to write a different password
  • Shuffling a 2D array without using Collections
  • C# where to add a method
  • VB.NET 2012 Property Set on Property Get
  • Retrieve IP address of device
  • Casting between Interfaces and Classes
  • Conversion from string “a” to type 'Boolean' is not valid
  • What and where is mdimport
  • Custom Tabgroup Appcelerator
  • $wpdb not working in file of WordPress plugin
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • How to set my toolbar fixed while scrolling android
  • How to make Safari send if-modified-since header?
  • Google cloud sdk not working when python points python3
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to pass list parameters for each object using Spring MVC?
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • JaxB to read class hierarchy
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal