Search code examples
excel-dna

Excel-DNA Config Settings on .NET 6


We are transitioning from using Excel-DNA on the .NET Framework to .NET 6. Under the .NET Framework, our configuration options were specified in the .xll.config file and these options were bundled into the resulting .XLL file we distributed to our user base. It was very seamless.

My understanding is that with .NET 6 we use appsettings.json.

In using the appsetting.json, will we need to distribute this file alongside the .XLL file or will the content of the appsetting.json be folded into the .XLL file, giving us a single distributable file?

Is there any sample code available on how to use the appsettings.json with Excel-DNA?


Solution

  • Under .NET 6 you don't get config file support from Excel-DNA, but you can do it in your own code. (Under .NET Framework there was a per-AppDomain, hence per-addin, .config file that had to be loaded before the rest of the code, so we had to put the plumbing in Excel-DNA to make it work.)

    For your appsettings.json you can

    • Manage as a separate file next to the .xll, and load at runtime with code like this
    var basePath = Path.GetDirectoryName(ExcelDnaUtil.XllPath);
    IConfiguration configuration = new ConfigurationBuilder()
      .SetBasePath(basePath)
      .AddJsonFile("appsettings.json")
      .Build();
    
    • Embed in your own C# library as a resource, then load as a stream into the configuration using JsonConfigurationExtensions.AddJsonStream.