{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/7043","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/7043/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/7043/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/7043/events","html_url":"https://github.com/dotnet/coreclr/issues/7043","id":174855620,"number":7043,"title":"MissingMethodException for dynamically loaded Assembly","user":{"login":"matthid","id":1866463,"avatar_url":"https://avatars.githubusercontent.com/u/1866463?v=3","gravatar_id":"","url":"https://api.github.com/users/matthid","html_url":"https://github.com/matthid","followers_url":"https://api.github.com/users/matthid/followers","following_url":"https://api.github.com/users/matthid/following{/other_user}","gists_url":"https://api.github.com/users/matthid/gists{/gist_id}","starred_url":"https://api.github.com/users/matthid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthid/subscriptions","organizations_url":"https://api.github.com/users/matthid/orgs","repos_url":"https://api.github.com/users/matthid/repos","events_url":"https://api.github.com/users/matthid/events{/privacy}","received_events_url":"https://api.github.com/users/matthid/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/coreclr/labels/Assembly%20Loader","name":"Assembly Loader","color":"fef2c0"},{"url":"https://api.github.com/repos/dotnet/coreclr/labels/bug","name":"bug","color":"fc2929"}],"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":8,"created_at":"2016-09-02T21:58:29Z","updated_at":"2016-09-07T16:59:41Z","closed_at":null,"body":"In my situation I'm trying to compile an application at runtime and loading it into a custom `AssemblyContext` as suggested by another issue as workaround: https://github.com/dotnet/coreclr/issues/6411\r\n\r\nWith this workaround in place I noticed another error. The runtime doesn't seem to load a required `System.IO.Compression.ZipFile` assembly. Instead it fails when using the `ZipArchive` extension methods defined in `ZipFilExtensions`.\r\n\r\nI hooked into the `AssemblyResolve` event and the following is what I'm seeing:\r\n```\r\nStarting Target: DotnetCoreCreateZipPackages (==> BootstrapAndBuildDnc)\r\nTrying to resolve: System.IO.Compression, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\r\nRedirect assembly load to known assembly: System.IO.Compression, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (Some\r\n  \"C:\\PROJ\\FAKE\\nuget\\dotnetcore\\Fake.netcore\\current\\System.IO.Compression.dll\")\r\nFinished Target: DotnetCoreCreateZipPackages\r\nTrying to resolve: System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\r\nUnable to find assembly {FullName =\r\n  \"System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\";\r\n Version = \"4.0.0.0\";\r\n Location =\r\n  \"C:\\PROJ\\FAKE\\.fake\\build.fsx\\packages\\Microsoft.NETCore.Portable.Compatibility\\ref\\netstandard1.0\\System.Core.dll\";}. (Error: System.BadImageFormatException: Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)\r\nFile name: 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.\r\n   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)\r\n   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)\r\n   at Fake.Runtime.CoreCache.realLoadAssembly@205(AssemblyLoadContext loadContext, Boolean printDetails, AssemblyInfo assemInfo)\r\n   at Fake.Runtime.CoreCache.loadAssembly(AssemblyLoadContext loadContext, Boolean printDetails, AssemblyInfo assemInfo)) Could not resolve: System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\r\nError:\r\nSystem.MissingMethodException: Method not found: 'System.IO.Compression.ZipArchiveEntry System.IO.Compression.ZipFileExtensions.CreateEntryFromFile(System.IO.Compression.ZipArchive, System.String, System.String)'.\r\n   at Fake.IO.Zip.createZipP[a](String fileName, a comment, Int32 level, IEnumerable`1 items)\r\n   at Fake.IO.Zip.CreateZip[a](String workingDir, String fileName, a comment, Int32 level, Boolean flatten, IEnumerable`1 files)\r\n   at FSI_0002.clo@587-37.Invoke(Unit _arg2)\r\n   at Fake.Core.Targets.runSingleTarget(TargetTemplate`1 target)\r\n```\r\n\r\nSo it is not even trying to load the required assembly. Interestingly when [I add](https://github.com/matthid/FAKE/commit/80c3d2da3eaacf0c119423b0e46e4922653b6670)\r\n\r\n```fsharp\r\n    let lc = System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(typeof<MyClass>.GetTypeInfo().Assembly)\r\n    let n = AssemblyName \"System.IO.Compression.ZipFile\"\r\n    lc.LoadFromAssemblyName(n) |> ignore\r\n```\r\n\r\njust before using the failing method everything works as it should...\r\n\r\nYou can reproduce this by typing the following into a git bash (windows 10 x64):\r\n\r\n```bash\r\ngit clone git@github.com:matthid/FAKE.git\r\nexport APPDATA=$HOME LocalAppData=$HOME # Only when on unix\r\ncd FAKE\r\ngit checkout 8a59f34\r\nSkipTests=true NO_DOTNETCORE_BOOTSTRAP=true ./build.sh # Builds all the stuff we need, will take a while\r\nrm nuget/dotnetcore/Fake.netcore/fake-dotnetcore*.zip\r\nCOREHOST_TRACE=1 ./nuget/dotnetcore/Fake.netcore/current/Fake.netcore.exe -v run build.fsx -t DotnetCoreCreateZipPackages --singletarget # NOTE Works first time\r\nrm nuget/dotnetcore/Fake.netcore/fake-dotnetcore*.zip\r\nCOREHOST_TRACE=1 ./nuget/dotnetcore/Fake.netcore/current/Fake.netcore.exe -v run build.fsx -t DotnetCoreCreateZipPackages --singletarget # Now fails every run, I think in the uncached run the required assembly is loaded in another path\r\n```\r\n\r\nTo add the [workaround](https://github.com/matthid/FAKE/commit/80c3d2da3eaacf0c119423b0e46e4922653b6670)\r\n\r\n```bash\r\ngit merge 80c3d2d\r\nrm -rf .fake/build.fsx # just to be save 2nd time\r\nrm -rf ~/.nuget/packages/Fake.* # when running second time, just to be safe\r\nSkipTests=true NO_DOTNETCORE_BOOTSTRAP=true ./build.sh # Builds all the stuff we need, will take a while\r\nrm nuget/dotnetcore/Fake.netcore/fake-dotnetcore*.zip\r\nCOREHOST_TRACE=1 ./nuget/dotnetcore/Fake.netcore/current/Fake.netcore.exe -v run build.fsx -t DotnetCoreCreateZipPackages --singletarget # NOTE Works first time\r\nrm nuget/dotnetcore/Fake.netcore/fake-dotnetcore*.zip\r\nCOREHOST_TRACE=1 ./nuget/dotnetcore/Fake.netcore/current/Fake.netcore.exe -v run build.fsx -t DotnetCoreCreateZipPackages --singletarget # NOTE Works second time\r\n```\r\n\r\nThis uses the latest RTM packages of the dotnetcore runtime. I couldn't reproduce this on a smaller sample but I tried to make it as easy as possible to reproduce.\r\n\r\nI noticed that AssemblyResolve is not triggered for ZipFile in the first (working) run either. Any help is welcome. Maybe related https://github.com/dotnet/corefx/issues/10787\r\n\r\nPlease ask if you have further questions about this setup.\r\n\r\nTLDR: Runtime is throwing `MissingMethodException` without even trying to load the required assembly. When loading the assembly by hand it works."},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/245347996","html_url":"https://github.com/dotnet/coreclr/issues/7043#issuecomment-245347996","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/7043","id":245347996,"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-09-07T16:59:41Z","updated_at":"2016-09-07T16:59:41Z","body":"> Ok, so I am required to keep the ALC instance along while trying to instantiate stuff? \r\n\r\nYes, since attempt to get a Type may require creating the type and thus, loading the assembly in which the type is defined (and any other assemblies defining the types that are dependencies of the type being created)."}}
{"actor":{"display_login":"gkhanna79"}}