Hi Terry,
.NET core (for our purposes .NET 6+) has three different 'runtime' editions, which include sets of libraries that are loaded and work together:
* .NET Runtime
* .NET Desktop Runtime (includes the .NET Runtime)
*
ASP.NET Core Runtime (includes the .NET Runtime)
When .NET core is first loaded into a process, the load call specifies which of these runtimes to load (together with the version). After the .NET core runtime is loaded with whatever version and Runtime options are given, that process cannot load other .NET core versions or runtime options.
Excel-DNA targets the .NET Desktop Runtime, and this is what it loads into the process. So as long as your add-in only targets the .NET Runtime or .NET Desktop Runtime, things work fine.
But if your add-in targets the
ASP.NET Core Runtime, as indicated by the presence of the "
Microsoft.AspNetCore.App" in your output runtimeconfig.json file, then it probably won't work with since some of the assemblies it might depend on won't be available given the runtime that is loaded.
The target runtime of your library is set by either having a <FarmeworkReference> entry in the project file, or by setting the project file Sdk at the top like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
From what I understand, your library itself is not targeting the
ASP.NET Core Runtime (which means you don't have either a FrameworkReference or the 'Web' Sdk. But you are referencing other projects that do target the
ASP.NET Core Runtime, and that results in the build process adding this additional runtime entry in your output. So, by referencing a library that targets the
ASP.NET Core Runtime, your own library now implicitly also targets that runtime.
A consequence of your library targeting the
ASP.NET Core Runtime is that some assemblies in packages will _not_ be copied to the output, since the version that is part of the Runtime is assumed to be available. So that's why Microsoft.Extensions.Configuration etc. are not in the output anymore. However, there may be many other assemblies that your library dependencies need, that are also not put in the output directory because they are part of the
ASP.NET Core Runtime. So just copying the M.E.C. assemblies to the output folder are not likely to be a good fix.
Excel-DNA does not check what runtime your library targets, so the error you run into would likely arise only when some type is loaded that now has a missing reference.
One of the snags in this is what I said earlier - "After the .NET core runtime is loaded with whatever version and Runtime options are given, that process cannot loaded other .NET core versions or runtime options." So, if different add-ins will be loaded into the same Excel process, it's hard to decide what to do in terms of both the .NET version to load, and the exact Runtime to load. The first add-in loaded decides, and if any other add-in needs a different version or runtime option, it's too late to change and it will fail to load. Up to Excel-DNA version 1.7.0 we've been conservative, and only support one .NET core version with one runtime configuration - we support only .NET 6 with the .NET Desktop Runtime. (This is in addition to the .NET Framework 4.x which we continue to support, which can happily run in parallel, which only has a single version installed on the computer, which we load, and which does not have these Runtime flavours.)
For the next Excel-DNA version, we're making things more flexible, but then also exposing more of the complexity of these decisions to the add-in author. We now support other .NET core versions (so you can target .NET 7 or .NET 8 instead of .NET 6). But the danger if you are targeting .NET 8 is that another add-in loads .NET 6 first, and then your add-in will fail to load. You need to deal with that complexity. You can also use the <RollForward> project option to set some more subtle options. E.g. you can target .NET 6 but say that the newest installed version of the runtime should be loaded. That might break other add-ins that need .NET 6. But you need to deal with that complexity.
Now in the latest pre-release version of Excel-DNA - version 1.8.0-alpha3 - we also support loading the runtime using the runtime configuration options from your project. To enable this you add the <ExcelAddInCustomRuntimeConfiguration>true</ExcelAddInCustomRuntimeConfiguration> option to your project file. With this option enabled, and if your add-in is the first to load .NET core into the Excel process, it will load the version of the runtime, _and the Runtime option_ that is specified in your project's output runtimeconfig.json. So, your add-in can load the
ASP.NET Core Runtime in this way. But if another add-in was loaded first, then it might have loaded a different .NET core version, or might have loaded with the default runtime option, and your add-in will fail to load. So, you get this additional option, but you need to deal with the complexity in add-in interactions which follows.
That gives you different ways of dealing with the problem of dependencies pulling in the ASP.ENT Core Runtime requirement. You can try take the above approach with the experimental Excel-DNA 1.8.0-alpha3, or you can try to get rid of the
ASP.NET Core Runtime dependency in the other projects.
For example, with SeriLog, you can probably keep the core SeriLog library, but should not have the additional SeriLog.AspNetCore involved, which (from its name) probably uses and needs the extra runtime.
I hope that gives you some ideas on what path to take.
-Govert