Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Cannot evaluate a security function" when using conditional breakpoints and in immediate window

Tags:

c#

.net

.net-core

When developing .NET Core 2.0 applications, I'm having issues with conditional breakpoints, watch evaluations, and immediate window evaluations. I'm receiving the following error:

modifiers.GroupBy(c => c.Modifier.Group).ToList()
threw an exception of type 'System.ArgumentException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147024809
HelpLink: null
InnerException: null
Message: "Cannot evaluate a security function."
ParamName: null
Source: null
StackTrace: null
TargetSite: null

What may cause this issue? Is there a workaround? This works just fine in a .NET Framework 4.x application.


Edit: Adding code sample

Using the following class:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Group { get; set; }
}

In a normal console application, targeting .NET Core 2.0:

static void Main(string[] args)
{
    var people = new List<Person>
    {
        new Person { Age = 17, Name = "Person A", Group = "Group A" },
        new Person { Age = 20, Name = "Person B", Group = "Group A" },
        new Person { Age = 23, Name = "Person C", Group = "Group A" },
        new Person { Age = 17, Name = "Person D", Group = "Group B" },
        new Person { Age = 25, Name = "Person E", Group = "Group B" },
        new Person { Age = 40, Name = "Person F", Group = "Group B" },
    };
    // Make sure that System.Linq gets loaded
    people.Where(c => c.Name == "Person A").First();
}

Set a breakpoint after the initialization of the List<Person>. In the immediate window, trying to evaluate a GroupBy or a Where which is not a simple equality check, it will throw an ArgumentException with the message "Cannot evaluate a security function."

A couple of examples would be:

people.Where(c => c.Name.Contains("F")).ToList()

people.GroupBy(c => c.Group).ToList()

Note: The watch statements seems to be working in Visual Studio Code, but not in Visual Studio 2017 Preview 3.

like image 875
Andre Andersen Avatar asked Jul 07 '17 03:07

Andre Andersen


1 Answers

That error seems to be due to the immediate window disallowing anything that may cause side-effects. Usually side-effects are allowed in the immediate window... but it does not like GroupBy on List<T> (while GroupBy on T[] is tolerated)

I reproduced using your example.

people.GroupBy(c => c.Group).ToList() throws
people.Where(c => c.Name.Contains("F")).ToList() does not.

It did not require a conditional breakpoint. My test was with a brand new .net core 2.0 project Debug Any CPU build. This was the stable release of VS Pro 2017 v15.3.1

To get around this issue, make a copy of your list into an array:

people.ToArray().GroupBy(c => c.Group).ToList()

I also recommend filing a bug report with MS.

like image 94
Graeme Wicksted Avatar answered Nov 13 '22 17:11

Graeme Wicksted