立即调用 C# 方法 VS Window,获取 'System.Threading.ThreadAbortException'

Calling C# Method VS in Immediate Window, Getting 'System.Threading.ThreadAbortException'

我正在尝试 运行 在 Visual Studio 即时 window 中编写一些相当简单的代码。所有代码所做的就是从文件中读取一些 JSON 输入,并使用它来调用一些其他方法,以使用值加载数据库。这是代码块:

using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace POST.API
{
    public class Initialization
    {
        public const string JSON_DATA_FILE = "C:\OHA_SDD_POST_Development\POST\POST.API\Services\Setup\InitializationData.json";
        public const string JSON_OBJKEY_DOMAIN = "Domain";
        public const string JSON_OBJKEY_ACCOUNTDOMAINTYPE = "AccountDomainType";
        public const string JSON_OBJKEY_ORGLOCTYPE = "OrganizationLocationType";

        public JObject POSTDataInitJObject;

        public JArray Domains;
        public JArray AccountDomainRoles;
        public JArray OrganizationLocationTypes;

        public API.Services.Domain SvcDomain;
        public API.Services.Organization SvcOrganization;
        public API.Services.Location SvcLocation;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="JsonDataFile"></param>
        public Initialization(string JsonDataFile = JSON_DATA_FILE)
        {
            string JsonData = File.ReadAllText(JsonDataFile);
            POSTDataInitJObject = JObject.Parse(JsonData);

            Domains = (JArray)POSTDataInitJObject[JSON_OBJKEY_DOMAIN];
            AccountDomainRoles = (JArray)POSTDataInitJObject[JSON_OBJKEY_ACCOUNTDOMAINTYPE];
            OrganizationLocationTypes = (JArray)POSTDataInitJObject[JSON_OBJKEY_ORGLOCTYPE];
        }
        /// <summary>
        /// 
        /// </summary>
        public void Load()
        {
            LoadDomains();
            LoadOrganizationLocationTypes();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Replace"></param>
        public void LoadDomains(bool Replace = true)
        {
            SvcDomain = new API.Services.Domain();

            if (Replace)
            {
                SvcDomain.ClearAllDomains(true);
            }

            foreach (var i in Domains)
            {
                SvcDomain.AddDomain(new API.Models.Domain
                {
                    Code = (string)i["Code"],
                    Definition = new API.Models.TypeDefinition
                    {
                        Name = (string)i["Name"],
                        Description = (string)i["Description"],
                        Order = Int32.Parse((string)i["Order"])
                    }
                });
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Replace"></param>
        public void LoadOrganizationLocationTypes(bool Replace = true)
        {
            SvcLocation = new API.Services.Location();

            if (Replace)
            {
                SvcLocation.ClearAllOrganizationLocationTypes();
            }

            foreach (var i in OrganizationLocationTypes)
            {
                SvcLocation.AddOrganizationLocationType(new API.Models.OrganizationLocationType
                {
                    Definition = new API.Models.TypeDefinition
                    {
                        Name = (string)i["Name"],
                        Description = (string)i["Description"],
                        Order = Int32.Parse((string)i["Order"])
                    }
                });
            }
        }
    }
}

我可以立即成功实例化对象 window,但是当我随后尝试调用该 Load() 方法时,在那个实例上,我得到:

'System.Threading.ThreadAbortException' 类型的第一次机会异常发生在 mscorlib.dll

评价暂时需要线程运行。使用手表window进行评估。

我已经关闭选项 -> 调试 -> 启用 属性 评估和其他隐式函数调用。

难倒我了...看起来超级简单,我完全过不去。

您正在直接或间接调用一些包含此代码的代码:

System.Diagnostics.Debugger.NotifyOfCrossThreadDependency()

为什么代码会有那个?

Some of you might have ran into this situation where you try to evaluate a property or a method during debug time, and get this message in the value cell of the watch:

这个问题有一个快速的“解决方法”。 如果仔细看,您会在消息后看到一个小圆形图标:

按下此图标将强制继续评估, 这很可能会给你你想要的结果:

When we break the debugger and try to evaluate a property which requires running code, we can only run code in the current thread. All other threads are frozen to minimize the system state impact… But, what if my property requires another thread to run in order to complete evaluation? For example, if I am doing a remote call, I will most likely need the ThreadPool threads for that… But all other threads are frozen… which will result in a “deadlock”. The debugger has a protection measure against that, which gives a max of 5 seconds to the evaluation code to run. If the code did not finish to run within the time limit, For example, when we are blocked due to a frozen thread, the watch will be “hanged” for 5 seconds, and after that evaluation will be aborted. We will get this message to notify us of the problem:

所以那行代码会导致您收到消息:

The debugger will not allow it automatically, to avoid state changes, and instead, will give the user the “The function evaluation requires all threads to run” message.

如果您打开了调试器,则:

If the user decides it’s OK to let all threads run, he can push the icon next to the message, which will cause the debugger to run the property code a second time, this time with all threads running, and not aborting on the NotifyOfCrossThreadDependency method.

请阅读 this 文章了解更多详情。

此外,我不确定您对 运行 立即 window 中的所有代码的推理是什么。或许您可以编写一个小型控制台应用程序,然后 运行 应用程序。

看来这个问题是偶然的。我只是没有在项目中提供参考,我试图从中 运行 代码到 EntityFramework。不知道为什么它会抛出这样的错误,但这就是发生的事情。对我来说似乎很奇怪,但我会接受。现在可以使用了。