Search code examples
c#jsonmultithreadingvisual-studioimmediate-window

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


I am attempting to run some fairly simple code in the Visual Studio immediate window. All the code does is read some JSON input, from a file, and uses that to call some other methods, for loading a database with values. Here's the code block:

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"])
                    }
                });
            }
        }
    }
}

I can successfully instantiate the object in the immediate window, but when I then try to call that Load() method, on that instance, I get:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll

Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation.

I've alraedy turned off Options -> Debug -> Enable property evaluation and other implicit function calls.

Got me stumped... Seems super simple, and I totally can't get past it.


Solution

  • So, it looks like the problem was an incidental one. I had simply failed to give a reference in the project, from which I was trying to run the code, to EntityFramework. Not sure why it would've thrown the error such as that given, but that's what happened. Seems odd to me, but I'll take it. It works now.