使绑定重定向适用于Office加载项 [英] Making binding redirects work for office add-ins

查看:106
本文介绍了使绑定重定向适用于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 Office\Office15'文件夹中并命名为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 Office\Office15' 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加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆