Invalid stack trace line number inside foreach block

I am having trouble understanding the stack trace in .net 4. I get different line numbers for identical code running in a console application or web service hosted in IIS 7.5 (both use .net 4).

Console Application:

static void Main(string[] args)
{
    try
    {
        var test = new List<int>() { 1, 2, 3 };
        foreach (var root in test)
        {
            throw new Exception("foobar");
        }
    }
    catch(Exception e)
    {
        throw;
    }
}

When checking the number of stack trace lines of the β€œe” stack inside the catch block, I get β€œ8”, which I excluded (throw new Exception (β€œfoobar” line))

Web service

[WebMethod]
public void Test()
{
    try
    {
        var test = new List<int>() { 1, 2, 3 };
        foreach (var root in test)
        {
            throw new Exception("foobar");
        }
    }
    catch (Exception e)
    {
        throw;
    }
}

Here the situation becomes strange - when checking the stack trace line number, I get "7" , which is the beginning of the foreach block . The same thing happens with the classic one, but if the operator, for example, works fine.

Any ideas?

EDIT:

Machine Learning runtime pdb. , , . :

[WebMethod]
public string Test()
{
    try
    {
        var test = new List<int>() { 1, 2, 3 };

        var a = 1;
        var b = 2;
        var c = 3;

        foreach (var root in test)
        {
            var d = 4;
            var e = 5;
            var f = 6;
            throw new Exception("foobar" + (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber());
            var g = 7;
        }
        return null;
    }
    catch (Exception e)
    {
        return e.Message + e.StackTrace;
    }
}

e.StackTrace "12" (foreach line) e.Message "17", .

+4
1

, Windows. , , , , - , [1]. ,.NET , .

, 64- - , 64-, . , , , IIS ( , , 32-, , , debug=false). EDIT: . , , , , JITters - a foreach 64- foreach, foreach using, GetEnumerator .., 32- .

, , , . throw new ... ( , , throw AFAIK MS). , a foreach imlicit using, a finally.

[1] , . () - ​​ , . , SEH Google, :)

+1

Source: https://habr.com/ru/post/1656189/


All Articles