Dynamically loading ASP .Net Core assemblies into regular .Net Core assemblies

Viewed 113

I have a project which targets ASP.Net Core 3.1, as such:

<Project Sdk="Microsoft.NET.Sdk.Web">

I have another project (which targets .Net Core 3.1 and does not have a framework reference to ASP.Net Core) which needs to dynamically load the above's assembly:

var assembly = Assembly.LoadFrom(filePath);
var references = assembly.GetExportedTypes();

This fails on the second line with following exception:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'

I don't recall ever having this issue when the assembly being loaded referenced normal assemblies, but for some reason it fails with ASP. This succeeds if the project doing the loading has a framework reference to ASP.Net Core.

How can I load ASP.Net Core assemblies into regular .Net Core assemblies?

Quick demonstration

1 Answers
Related