{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/6317","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/6317/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/6317/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/6317/events","html_url":"https://github.com/dotnet/coreclr/issues/6317","id":166128241,"number":6317,"title":"Assembly.Load and Type.GetType fail with ExecutionEngineException unless the assembly has already been loaded with AssemblyLoadContext.Default.LoadFromAssemblyName","user":{"login":"mlorbetske","id":1414993,"avatar_url":"https://avatars.githubusercontent.com/u/1414993?v=3","gravatar_id":"","url":"https://api.github.com/users/mlorbetske","html_url":"https://github.com/mlorbetske","followers_url":"https://api.github.com/users/mlorbetske/followers","following_url":"https://api.github.com/users/mlorbetske/following{/other_user}","gists_url":"https://api.github.com/users/mlorbetske/gists{/gist_id}","starred_url":"https://api.github.com/users/mlorbetske/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mlorbetske/subscriptions","organizations_url":"https://api.github.com/users/mlorbetske/orgs","repos_url":"https://api.github.com/users/mlorbetske/repos","events_url":"https://api.github.com/users/mlorbetske/events{/privacy}","received_events_url":"https://api.github.com/users/mlorbetske/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"gkhanna79","id":9451457,"avatar_url":"https://avatars.githubusercontent.com/u/9451457?v=3","gravatar_id":"","url":"https://api.github.com/users/gkhanna79","html_url":"https://github.com/gkhanna79","followers_url":"https://api.github.com/users/gkhanna79/followers","following_url":"https://api.github.com/users/gkhanna79/following{/other_user}","gists_url":"https://api.github.com/users/gkhanna79/gists{/gist_id}","starred_url":"https://api.github.com/users/gkhanna79/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gkhanna79/subscriptions","organizations_url":"https://api.github.com/users/gkhanna79/orgs","repos_url":"https://api.github.com/users/gkhanna79/repos","events_url":"https://api.github.com/users/gkhanna79/events{/privacy}","received_events_url":"https://api.github.com/users/gkhanna79/received_events","type":"User","site_admin":false},"assignees":[{"login":"gkhanna79","id":9451457,"avatar_url":"https://avatars.githubusercontent.com/u/9451457?v=3","gravatar_id":"","url":"https://api.github.com/users/gkhanna79","html_url":"https://github.com/gkhanna79","followers_url":"https://api.github.com/users/gkhanna79/followers","following_url":"https://api.github.com/users/gkhanna79/following{/other_user}","gists_url":"https://api.github.com/users/gkhanna79/gists{/gist_id}","starred_url":"https://api.github.com/users/gkhanna79/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gkhanna79/subscriptions","organizations_url":"https://api.github.com/users/gkhanna79/orgs","repos_url":"https://api.github.com/users/gkhanna79/repos","events_url":"https://api.github.com/users/gkhanna79/events{/privacy}","received_events_url":"https://api.github.com/users/gkhanna79/received_events","type":"User","site_admin":false}],"milestone":null,"comments":2,"created_at":"2016-07-18T15:55:52Z","updated_at":"2016-07-19T14:43:56Z","closed_at":null,"body":"`Assembly.Load` appears to take some action on assemblies returned by the handler for the `AssemblyLoadContext.Resolving` event that causes an `ExecutionEngineException` - if the assembly has been loaded in to `AssemblyLoadContext.Default` prior to the call to `Assembly.Load` the load succeeds.\r\n\r\nIn the trivial repro below, simply calling the `AssemblyLoadContext.LoadFromAssemblyName(AssemblyName)` method prior to attempting to get the Assembly via `Assembly.Load(AssemblyName)` or a type in the assembly via `Type.GetType(string)` or `Assembly.GetType(string)` resolve the issue. Unfortunately, if a type in the loaded assembly depends on types defined in other non-referenced, by the executing program, assemblies (assemblies that sit next to the dynamically loaded on on disk for example), they also cause the unexpected behavior by invoking the resolver, succeeding in finding the assembly, but then failing after returning to the caller. The solution to this problem is to walk the dependency graph of any dynamically loaded assembly in the handler for `AssemblyLoadContext.Resolving` loading each of them within that call.\r\n\r\nRepro program:\r\n``` c#\r\nusing System;\r\nusing System.IO;\r\nusing System.Reflection;\r\nusing System.Runtime.InteropServices;\r\nusing System.Runtime.Loader;\r\n\r\nnamespace LoaderQuirkRepro\r\n{\r\n    public class Program\r\n    {\r\n        public const string PackageVersion = \"9.0.1\";\r\n        public static readonly string ProfileDir = Environment.GetEnvironmentVariable(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)\r\n                ? \"USERPROFILE\"\r\n                : \"HOME\");\r\n\r\n        public static readonly string NugetCacheDir = Path.Combine(ProfileDir, \".nuget\", \"packages\");\r\n        public static readonly string NewtonsoftJsonDllPath = Path.Combine(NugetCacheDir, \"Newtonsoft.Json\", PackageVersion, \"lib\", \"netstandard1.0\", \"Newtonsoft.Json.dll\");\r\n\r\n        public static void Main(string[] args)\r\n        {\r\n            AssemblyLoadContext.Default.Resolving += AssemblyResolving;\r\n            AssemblyName newtonsoftJsonAssemblyName = new AssemblyName(\"Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed\");\r\n\r\n            //If this is uncommented, the calls below succeed\r\n            //AssemblyLoadContext.Default.LoadFromAssemblyName(newtonsoftJsonAssemblyName);\r\n\r\n            //This is exceptional unless AssemblyLoadContext.Default.LoadFromAssemblyName is uncommented even though the handler to find the\r\n            //  assembly is run (and finds/loads the assembly)\r\n            Assembly newtonsoftJson = Assembly.Load(newtonsoftJsonAssemblyName);\r\n\r\n            //This is also exceptional in the same way\r\n            //Type jsonConvertType = Type.GetType(\"Newtonsoft.Json.JsonConvert, Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed\");\r\n        }\r\n\r\n        private static Assembly AssemblyResolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)\r\n        {\r\n            //FileNotFoundException - OK, might be nice to say that the assembly with identity X couldn't be loaded instead, but correctly exceptional\r\n            //return null;\r\n\r\n            //ExecutionEngineException - It feels like when this is invoked by Assembly.Load and the assembly must be resolved, that the caller\r\n            //  attempts to perform some action on the assembly, expecting it to be in a different state than it is (either the assembly object\r\n            //  is in some sort of \"initialized\" state and a second attempt to initialize it causes a fault, or the assembly object is in an\r\n            //  uninitialized state and attempt to use it without initialization causes a fault)\r\n            return assemblyLoadContext.LoadFromAssemblyPath(NewtonsoftJsonDllPath);\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nproject.json\r\n```\r\n{\r\n  \"version\": \"1.0.0-*\",\r\n  \"buildOptions\": {\r\n    \"emitEntryPoint\": true\r\n  },\r\n\r\n  \"dependencies\": {\r\n    \"Microsoft.NETCore.App\": {\r\n      \"type\": \"platform\",\r\n      \"version\": \"1.0.0\"\r\n    },\r\n    \"System.Runtime.Loader\": \"4.0.0\"\r\n  },\r\n\r\n  \"frameworks\": {\r\n    \"netcoreapp1.0\": {\r\n      \"imports\": \"dnxcore50\"\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nEnvironment info:\r\n```\r\n> dotnet\r\n\r\nMicrosoft .NET Core Shared Framework Host\r\n\r\n  Version  : 1.0.1\r\n  Build    : cee57bf6c981237d80aa1631cfe83cb9ba329f12\r\n\r\nUsage: dotnet [common-options] [[options] path-to-application]\r\n\r\nCommon Options:\r\n  --help                           Display .NET Core Shared Framework Host help.\r\n  --version                        Display .NET Core Shared Framework Host version.\r\n\r\nOptions:\r\n  --fx-version <version>           Version of the installed Shared Framework to use to run the application.\r\n  --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.\r\n\r\nPath to Application:\r\n  The path to a .NET Core managed application, dll or exe file to execute.\r\n\r\nIf you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in your environment.\r\n\r\nTo get started on developing applications for .NET Core, install .NET SDK from:\r\n  http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409\r\n\r\n```\r\n\r\n```\r\n> dotnet --info\r\n.NET Command Line Tools (1.0.0-preview2-003121)\r\n\r\nProduct Information:\r\n Version:            1.0.0-preview2-003121\r\n Commit SHA-1 hash:  1e9d529bc5\r\n\r\nRuntime Environment:\r\n OS Name:     Windows\r\n OS Version:  10.0.10586\r\n OS Platform: Windows\r\n RID:         win10-x64\r\n```"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/233655223","html_url":"https://github.com/dotnet/coreclr/issues/6317#issuecomment-233655223","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/6317","id":233655223,"user":{"login":"gkhanna79","id":9451457,"avatar_url":"https://avatars.githubusercontent.com/u/9451457?v=3","gravatar_id":"","url":"https://api.github.com/users/gkhanna79","html_url":"https://github.com/gkhanna79","followers_url":"https://api.github.com/users/gkhanna79/followers","following_url":"https://api.github.com/users/gkhanna79/following{/other_user}","gists_url":"https://api.github.com/users/gkhanna79/gists{/gist_id}","starred_url":"https://api.github.com/users/gkhanna79/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gkhanna79/subscriptions","organizations_url":"https://api.github.com/users/gkhanna79/orgs","repos_url":"https://api.github.com/users/gkhanna79/repos","events_url":"https://api.github.com/users/gkhanna79/events{/privacy}","received_events_url":"https://api.github.com/users/gkhanna79/received_events","type":"User","site_admin":false},"created_at":"2016-07-19T14:43:56Z","updated_at":"2016-07-19T14:43:56Z","body":"This is a duplicate of https://github.com/dotnet/coreclr/issues/5837 (which has been fixed for our upcoming release) and similar to https://github.com/dotnet/coreclr/issues/6171."}}
{"actor":{"display_login":"gkhanna79"}}