Search code examples
c#excelnuget-packageazure-sdk-.netexcel-dna

How do binding redirects currently work for ExcelDNA plugins?


I am currently stuck in dependency hell with an Excel DNA plugin that uses the Azure SDK to talk to a blob store.

I am encountering the issue outlined here https://github.com/Azure/azure-sdk-for-net/issues/39798 to which the suggested resolution is to enable Automatic Binding Redirects. However it's not clear to me if ExcelDNA supports this, especially given this open bug: https://github.com/Excel-DNA/ExcelDna/issues/241. I tried the workaround here and it didn't help

Is there a way to manually configure a binding redirect in such a way that the plugin is sure to pick it up or are automatic binding redirects now working?

Error Message:

System.IO.FileLoadException
  HResult=0x80131040
  Message=Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  Source=Azure.Identity
  StackTrace:
   at Azure.Core.Pipeline.DiagnosticScopeFactory..ctor(String clientNamespace, String resourceProviderNamespace, Boolean isActivityEnabled, Boolean suppressNestedClientActivities, Boolean isStable)
   at Azure.Core.Pipeline.ClientDiagnostics..ctor(ClientOptions options, Nullable`1 suppressNestedClientActivities)
   at Azure.Identity.CredentialPipeline..ctor(TokenCredentialOptions options)
   at Azure.Identity.CredentialPipeline.<>c.<.cctor>b__17_0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Azure.Identity.CredentialPipeline.GetInstance(TokenCredentialOptions options, Boolean IsManagedIdentityCredential)
   at Azure.Identity.InteractiveBrowserCredential..ctor(String tenantId, String clientId, TokenCredentialOptions options, CredentialPipeline pipeline, MsalPublicClient client)
   at Azure.Identity.InteractiveBrowserCredential..ctor()
   at DataTool.SyncConfigFiles.<Sync>d__0.MoveNext() in C:\Repros\CT---DataTool\Data Tool\SyncConfigFiles.cs:line 38

I believe I need a binding redirect to V6.0.1.0 which is packed into the xll but nothing I try seems to help

I was expecting to be able to enable Automatic Binding Redirects, as advised, to fix the issue in the Azure SDK. However ExcelDNA does not seem to support this and the problem doesn't go away


Solution

  • Is there a way to manually configure a binding redirect in such a way that the plugin is sure to pick it up or are automatic binding redirects now working?

    In Excel-DNA, automatic binding redirects are not supported, you must manually configure the binding redirects in the App.config or Web.config file.

    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.0.1.0" newVersion="6.0.1.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    
    • This configuration should include in the final .xll.config file that Excel-DNA uses.

    Add a custom target in the .csproj file to copy the App.config to the appropriate .xll.config files

    <Target Name="CopyAppConfig" AfterTargets="ExcelDnaBuild">
      <Message Importance="High" Text="Copy $(TargetPath).config to $(TargetDir)MyAddIn-AddIn[64][-packed].xll.config" />
      <Copy SourceFiles="$(TargetPath).config" DestinationFiles="$(TargetDir)MyAddIn-AddIn.xll.config" />
      <Copy SourceFiles="$(TargetPath).config" DestinationFiles="$(TargetDir)MyAddIn-AddIn-packed.xll.config" />
      <Copy SourceFiles="$(TargetPath).config" DestinationFiles="$(TargetDir)MyAddIn-AddIn64.xll.config" />
      <Copy SourceFiles="$(TargetPath).config" DestinationFiles="$(TargetDir)MyAddIn-AddIn64-packed.xll.config" />
    </Target>
    

    Sample data:

    enter image description here

    Now it can interact with blob store successfully.

    enter image description here