You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ExcelDNA插件绑定重定向机制及手动配置方案咨询

Excel-DNA插件结合Azure SDK的绑定重定向问题

我正在开发一个基于Excel-DNA的插件,通过Azure SDK连接Blob存储,目前陷入依赖版本冲突的困境。相关Azure SDK问题的建议解决方案是启用自动绑定重定向,但Excel-DNA存在未解决的相关Bug,尝试了其中的临时解决方法也无效。

核心疑问:

  • 有没有能让插件识别的手动配置绑定重定向的方法?
  • 自动绑定重定向现在是否能在Excel-DNA中正常工作?

错误信息

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

我需要将绑定重定向到已打包进XLL的6.0.1.0版本,但尝试的方法均无效。原本期望启用自动绑定重定向修复Azure SDK的问题,但Excel-DNA似乎不支持该功能,问题仍未解决。


手动配置绑定重定向的有效方案

1. 编辑Excel-DNA的.dna配置文件

在项目的.dna文件中,直接添加<Runtime>节点定义绑定重定向规则,确保规则嵌套在<DnaLibrary>下:

<DnaLibrary Name="你的插件名称" RuntimeVersion="v4.0">
  <ExternalLibrary Path="你的插件.dll" Pack="true" />
  <!-- 其他已有配置 -->
  <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.0.0" 
                        newVersion="6.0.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </Runtime>
</DnaLibrary>

确认newVersion与你打包进XLL的程序集版本完全一致,打包时确保该程序集被正确嵌入。

2. 在代码中手动处理程序集解析

在插件的初始化方法(如AutoOpen)中,注册AssemblyResolve事件,手动引导加载正确版本的程序集:

using System;
using System.Reflection;
using ExcelDna.Integration;

public class AddIn : IExcelAddIn
{
    public void AutoOpen()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            var assemblyName = new AssemblyName(args.Name);
            if (assemblyName.Name.Equals("System.Diagnostics.DiagnosticSource", StringComparison.OrdinalIgnoreCase) &&
                assemblyName.Version <= new Version("6.0.0.0"))
            {
                // 如果程序集是内嵌资源,读取资源并加载
                var resourceName = "你的命名空间.System.Diagnostics.DiagnosticSource.dll";
                using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
                if (stream == null) return null;
                var assemblyBytes = new byte[stream.Length];
                stream.Read(assemblyBytes, 0, assemblyBytes.Length);
                return Assembly.Load(assemblyBytes);
            }
            return null;
        };

        // 其他插件初始化逻辑
    }

    public void AutoClose()
    {
        // 清理逻辑
    }
}

如果程序集是作为独立文件打包,可替换为Assembly.LoadFrom("System.Diagnostics.DiagnosticSource.dll"),但需确保文件路径正确。

3. 结合项目配置与Excel-DNA打包

在项目的.csproj中启用自动绑定重定向生成:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

编译后,将生成的插件名称.dll.config文件中的<assemblyBinding>节点内容,复制到.dna文件的<Runtime>节点下,再重新打包XLL。


自动绑定重定向的现状

目前Excel-DNA的相关Bug仍未完全解决,自动绑定重定向无法直接生效——因为Excel进程不会像普通.NET应用那样读取插件的.config文件。即使项目启用了自动生成绑定重定向,也需要手动将配置内容迁移到.dna文件中,才能被Excel-DNA识别并应用。

内容的提问来源于stack exchange,提问作者OJones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:14:54