本文介绍了使绑定重定向适用于 Office 加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Word 插件中使用 Microsoft.Bcl.Async,我的插件被编译为一个 exe (test_addin.exe) 文件,该文件作为一个程序集从 Microsoft Word 加载,当我直接启动可执行文件时,一切正常很好,但是当我从 Word 运行它时,我收到一条错误消息,指出它无法加载 Systems.Threading.Tasks 程序集.

I'm using Microsoft.Bcl.Async in my Word addin, my addin is compiled as an exe (test_addin.exe) file, that is loaded as an assembly from Microsoft Word, when I start the executable directly, everything's working fine, but when I run it from Word, I'm getting an error saying that it failed to load the Systems.Threading.Tasks assembly.

Could not load file or assembly System.Threading.Tasks...

看起来它与绑定重定向有关,当我尝试从 Word 运行应用程序时,它希望配置文件位于 'C:Program Files (x86)Microsoft OfficeOffice15' 文件夹并命名为 WINWORD.exe.config,不幸的是,这是不可能的,因为我可能无法访问该文件夹.

It looks like that its related to the binding redirects, when I try to run the application from Word it expects the config file to be located in the 'C:Program Files (x86)Microsoft OfficeOffice15' folder and be named WINWORD.exe.config, that is unfortunately impossible because I might not have access to that folder.

我的 test_addin.exe.config 文件:

My test_addin.exe.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我已尝试将 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 设置为指向正确的路径,但似乎没有帮助,是否还有其他方法可以使其适用于 Office 添加 -在?

I have tried setting AppDomain.CurrentDomain.SetupInformation.ConfigurationFile to point to the correct path, but it doesn't seem to help, are there other ways to make it work for an Office add-in?

推荐答案

我已经通过实现自定义的 AssemblyResolve 处理程序解决了这个问题

I have solved this problem by implementing a custom AssemblyResolve handler

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
    {
        try
        {
            if (!e.Name.ToLower().StartsWith("system.threading.tasks"))
                return null;

            AddoDebug.Instance.WriteLine("Assembly_Resolve");
            var assemblyDetail = e.Name.Split(',');
            var assemblyBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var assembly = Assembly.LoadFrom(assemblyBasePath + @"" + assemblyDetail[0] + ".dll");

            return assembly;
        }
        catch (Exception ex)
        {
            AddoDebug.Instance.WriteLine("An exception occurred: " + ex, ADDOTraceStatus.Exception);
            return null;
        }
    }

但我不确定这是一个好的解决方案,所以我将这个问题留给新的答案.

这篇关于使绑定重定向适用于 Office 加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:37