Making binding redirects work for office add-ins

拥有回忆 提交于 2019-12-23 10:49:31

问题


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...

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.

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>

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?


回答1:


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;
        }
    }

But I'm not sure it's a good solution, so I'm leaving this question open for new answers.



来源:https://stackoverflow.com/questions/25664372/making-binding-redirects-work-for-office-add-ins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!