Skip to content

Running tasks compiled for netcore throws ReflectionTypeLoadException #403

Closed

Description

@matkoch

When I simply run my task as part of MSBuild, it works as expected. AFAIR, I was able to run/debug it specifically StructuredLogViewer, but yesterday it failed on me.

Unhandled Exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at TaskRunner.Executor.Execute(Task task) in C:\MSBuildStructuredLog\src\TaskRunner\Executor.cs:line 32
   at TaskRunner.Program.Run(String binlog, Int32 index, String taskName) in C:\MSBuildStructuredLog\src\TaskRunner\Program.cs:line 114
   at TaskRunner.Program.Main(String[] args) in C:\MSBuildStructuredLog\src\TaskRunner\Program.cs:line 61

One of the assemblies is System.Runtime:

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Users/matthias.koch/AppData/Local/MSBuildStructuredLogViewer/app-2.1.208/
LOG: Initial PrivatePath = NULL
Calling assembly : CustomTasks, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\matthias.koch\AppData\Local\MSBuildStructuredLogViewer\app-2.1.208\TaskRunner.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/Users/matthias.koch/AppData/Local/MSBuildStructuredLogViewer/app-2.1.208/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/matthias.koch/AppData/Local/MSBuildStructuredLogViewer/app-2.1.208/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/matthias.koch/AppData/Local/MSBuildStructuredLogViewer/app-2.1.208/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/matthias.koch/AppData/Local/MSBuildStructuredLogViewer/app-2.1.208/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/code/custom-msbuild-task/src/CustomTasks/bin/Debug/netcoreapp2.1/publish/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/code/custom-msbuild-task/src/CustomTasks/bin/Debug/netcoreapp2.1/publish/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/code/custom-msbuild-task/src/CustomTasks/bin/Debug/netcoreapp2.1/publish/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/code/custom-msbuild-task/src/CustomTasks/bin/Debug/netcoreapp2.1/publish/System.Runtime/System.Runtime.EXE.

Anything else I can provide to give more insight?

Activity

  1. KirillOsenkov commented on Oct 12, 2020

    @KirillOsenkov
    Owner

    What's the Task name? What's the .dll file that the task is in? Can you share the .dll file with me? First dot last at my employer dot com.

  2. KirillOsenkov commented on Oct 12, 2020

    @KirillOsenkov
    Owner

    OK, I understand what's going on. The task .dll is targeting .NET Core, whereas TaskRunner.exe is a net472 desktop app.

    I'm going to have to write logic to detect whether the task assembly is targeting .NET Core, and start TaskRunner.exe via "dotnet TaskRunner.exe". I'm also going to need to generate TaskRunner.runtimeconfig.json with this contents next to TaskRunner.exe:

    {
      "runtimeOptions": {
        "framework": {
          "name": "Microsoft.NETCore.App",
          "version": "3.0.0"
        }
      }
    }
    

    Depending on which framework is installed on the machine.

  3. KirillOsenkov commented on Oct 12, 2020

    @KirillOsenkov
    Owner

    As a temporary workaround, you can view the task Id in binlog viewer by choosing "Search in subtree" and seeing which ID it adds to the search box:

    image

    Then write this content to C:\Users\kirillo\AppData\Local\MSBuildStructuredLogViewer\app-2.1.208\TaskRunner.runtimeconfig.json:

    {
      "runtimeOptions": {
        "framework": {
          "name": "Microsoft.NETCore.App",
          "version": "3.0.0"
        }
      }
    }
    

    Then run

    dotnet "%localappdata%\MSBuildStructuredLogViewer\app-2.1.208\TaskRunner.exe" "C:\custom-msbuild-task\src\TestProject\msbuild.binlog" 616
    

    where 616 is the unique ID of the task.

    Unfortunately I'm not sure when I'll have time to implement the above logic so you don't have to do this manually. But it would consist of:

    1. Read the assembly attributes on the task dll and see whether it's targeting .NET Core or desktop framework. If desktop, nothing to be done.
    2. If targeting Core, detect whether 32-bit or 64-bit dotnet.exe is installed into Program Files (C:\Program Files\dotnet\dotnet.exe or C:\Program Files\dotnet\dotnet.exe)
    3. detect which frameworks are installed at C:\Program Files (x86)\dotnet\shared\Microsoft.NETCore.App\
    4. Generate a TaskRunner.runtimeconfig.json next to TaskRunner.exe if it doesn't exist yet (and write the snipped above, make sure the framework exists from step 2)
    5. When calling Process.Start() for TaskRunner.exe, prepend the path to "dotnet.exe" from step 1
  4. KirillOsenkov commented on Oct 12, 2020

    @KirillOsenkov
    Owner

    This is where the logic needs to be added:

    Process.Start(taskRunner.QuoteIfNeeded(), $"{logFilePath} {task.Index} pause{(debug ? " debug" : "")}");

  5. KirillOsenkov commented on Oct 12, 2020

    @KirillOsenkov
    Owner

    Quick repro steps to get the binlog:

    1. git clone https://github.com/matkoch/custom-msbuild-task
    2. edit kirill.bat to make sure this path exists: C:/Program Files/dotnet/sdk/3.1.401/MSBuild.dll and change to whatever you have installed
    3. run kirill.bat, it will produce C:\custom-msbuild-task\src\TestProject\msbuild.binlog
    4. open msbuild.binlog in the viewer, find $task CustomTask, right-click and Run or Debug

    Expected: should work

    Actual: ReflectionTypeLoadException because trying to load a task assembly targeting .NET Core on a .NET Framework process.

  6. matkoch commented on Oct 12, 2020

    @matkoch
    Author

    @KirillOsenkov arg! I actually thought about this but must have forgotten to update one of the references. Can confirm that it's working with net472 just fine. Thanks for your quick response! ❤️

  7. changed the title [-]Run/debug task throws ReflectionTypeLoadException[/-] [+]Running tasks compiled for netcore throws ReflectionTypeLoadException[/+] on Oct 15, 2020
  8. added a commit that references this issue on Dec 9, 2021
    049a214
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions