Excel-DNA / ExcelDna

Excel-DNA - Free and easy .NET for Excel. This repository contains the core Excel-DNA library.
https://excel-dna.net
zlib License
1.26k stars 270 forks source link

FrameworkReference issue with xll (.NET 6) #671

Open Sandoli opened 5 months ago

Sandoli commented 5 months ago

I want to build a xll from a .csproj in .NET 6.

  <ItemGroup>
    <PackageReference Include="ExcelDna.AddIn" Version="1.7.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="6.0.0" />
    <FrameworkReference Include="Microsoft.AspNetCore.App" Version="6.0.0" />
  </ItemGroup>

This xll depends on Microsoft.Extensions.Configuration because I want to read the .xll.config file. The xll also transitively depends on FrameworkReference "Microsoft.AspNetCore.App" because of other mandatory stuff.

Now when packing the xll, I observes that Microsoft.Extensions.Configuration.dll and other needed dlls are not packed. They are not copied in the output folder by MSBuild, this works as intended as the dlls are part of Microsoft.AspNetCore.App. I understand this is the reason of their absence in the packed xll.

Now, while trying to load the xll with Excel, I have this error :

ComAddIn [Error] The Ribbon/COM add-in helper required by add-in TestAddin Add-In could not be registered.
This may be due to the helper add-in being disabled by Excel.
To repair, open Disabled items in the Options->Add-Ins page and re-enable target add-in, then restart Excel.
Error details: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
   --- End of inner exception stack trace ---
   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at ExcelDna.Integration.ExcelComAddInHelper.LoadComAddIn(ExcelComAddIn addIn) : TargetInvocationException - Exception has been thrown by the target of an invocation.

I can confirm that if I get rid of the FrameworkReference, all the dlls are packed as intended and the xll loads successfully. But this is not my use case. I tried to play with the runtimeconfig.json file but I did not manage to make it work.

My runtimeconfig.json file looks like below:

  "runtimeOptions": {
    "tfm": "net6.0",
    "rollForward": "LatestMinor",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "6.0.0"
      },
      {
        "name": "Microsoft.AspNetCore.App",
        "version": "6.0.0"
      }
    ]
  }
}

Could you tell me what I missed please?

govert commented 5 months ago

With Excel-DNA (at least at the moment) you can't target the ASP.NET version of the framework. I can't remember whether there is a serious problem, or it just worked out in the way we load the runtime. Anyway, we currently always load the Microsoft.WindowsDesktop.App framework, and your project's .runtimeconfig.json is ignored. Since all add-ins targeting .NET 6+ shared the runtime environment, your add-in is not as isolated as with .NET Framework 4.x.

The Microsoft.Extensions.Configuration libraries should, however, work fine with just the Desktop runtime. You might try to see how things look without the ASP.NET bits. I expect the right dependencies will then get copied to the output and packed in the add-in.

If you need to host some http server from the add-in, then you can directly get an HttpListener working, like I mentioned:https://groups.google.com/g/exceldna/c/aIK6qLflaDQ/m/cT4kc_nYAQAJ

Sandoli commented 5 months ago

Hi Govert,

Thank you. As you suggested I am heading towards a removal of the ASP.NET bits.

Thanks also for the link on the HttpListener, it could help.

govert commented 3 months ago

We’ve now added an option to Excel-DNA to load the runtime using the add-in project’s .runtimeconfig.json file. This feature is added to the preview version 1.8.0-alpha3.

I’ve made a small test project that shows how the ASP.NET Core SDK can now be loaded and used from an add-in. govert/TestAspNetAddIn (github.com)