ExcelDna icon indicating copy to clipboard operation
ExcelDna copied to clipboard

How to pack external dlls in sdk-style projects?

Open lilkui opened this issue 3 years ago • 3 comments

Hi,

How do I pack external dlls (installed via nuget package manager) in sdk-style projects (without creating .dna file)? Maybe add something in .csproj file? (Sorry I could not find any documentation on this.)

Thanks for any help.

lilkui avatar Nov 09 '22 07:11 lilkui

You can specify a semi-colon (;) delimited list of extra asesmblies to pack, inside a project property called ExcelAddInInclude, like this:

  <PropertyGroup>
    <ExcelAddInInclude>ExtraFile.dll;OtherFile.dll</ExcelAddInInclude>
  </PropertyGroup>

If you want to include all the .dll files found in the output directory, you can set it up like this:

  <Target Name="PackedReferences" AfterTargets="AfterBuild" BeforeTargets="ExcelDnaBuild">
    <ItemGroup>
      <References Include="$(OutDir)*.dll" Exclude="$(OutDir)$(TargetFileName)"/>
    </ItemGroup>

    <PropertyGroup>
      <ExcelAddInInclude>@(References)</ExcelAddInInclude>
    </PropertyGroup>
  </Target>

You can check the .dna files that are generated in the output directory for the extra <Reference> tags created.

govert avatar Nov 09 '22 22:11 govert

I am trying to upgrade from .net framework 4.7.2 to .NET6. I have trouble with config file only. I found a partial work around.

var path = Assembly.GetExecutingAssembly().Location + ".config";
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);

This works fine except if you have any custom configSection types defined. The assembly for custom configSection will not load due to this issue: https://github.com/dotnet/runtime/issues/84244

Turhan8 avatar Apr 03 '23 13:04 Turhan8

@Turhan8 For configuration under .NET 6, I think your life will be better if you migrate to Microsoft.Extensions.Configuration.XXX. The support for System.Configuration.ConfigurationManager is very problematic.

For a start, under .NET 6 there is only one AppDomain, so overriding configuration there would interfere with other add-ins trying to do the same. You might rather load the file explicitly. Next, for the type loading to work right, there is some help by wrapping your code in

using (var ctx = System.Runtime.Loader.AssemblyLoadContext.EnterContextualReflection(this.GetType().Assembly))

But even then. you'll probably still be frustrated and waste a lot of time on System.Configuration.ConfigurationManager for your Excel add-ins under .NET 6. Avoid.

govert avatar Apr 03 '23 13:04 govert