lundi 3 août 2020

Load WPF assembly from console application

How can I load a WPF User Control Library dynamically from a console application?

Problem description:

I have an assembly that is compiled as WPF User Control Library and want to load it from a console application using reflection. Unfortunately, I receive a System.Reflection.ReflectionTypeLoadException with this message:

Unable to load one or more of the requested types. Could not load file or assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Loading "normal" Class Library assemblies works without problems. Even if they have dependencies, everything is loaded correctly. However, loading fails as soon as the assembly contains WPF.

What I tried:

I tried different ways to load the assembly, but all of them fail with above exception:

  1. Using System.ComponentModel.Composition with imports and exports.
  2. Using Assembly.LoadFrom().
  3. Using a custom AssemblyLoadContext as described at docs.microsoft.com.

I made sure that the path to the assembly is correct. If I specify invalid paths, the error message is different.

Details:

The assembly has target framework .NET Core 3.1 and output type Class Library. It uses the Microsoft.NETCore.App and the Microsoft.WindowsDesktop.App.WPF frameworks.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

The console application has target framework .NET Core 3.1 and output type Console Application. It uses the Microsoft.NETCore.App framework.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
</Project>




Aucun commentaire:

Enregistrer un commentaire