VSTO Binding redirect not working in Excel Addin deployed
Hello !!
We are working in a VSTO Addin Excel project, with many dependencies references.
We also have an Setup Project for create installer for this project.
We configured the binding redirect to be auto generated in the project csproj file, like
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
When we install the addin in Program files/MyAddin folder I can see that MyAddin.dll.config has the redirects in place, so I understand that the installer worked fine in that.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.17.0" newVersion="2.0.17.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.1" newVersion="8.0.0.1" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Options" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.2" newVersion="8.0.0.2" />
</dependentAssembly>
...
...
</runtime>
However, when we start Excel the addin is not loading, and after reviewing the Fusion logs I could see messages like this one i.e
*** Assembly Binder Log Entry (23/06/2024 @ 19:36:01) ***
The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
(Fully-specified)
LOG: Appbase = file:///C:/Program Files/MyAddin/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = EXCEL.EXE
Calling assembly : Microsoft.Extensions.Logging, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files/MyAddin/Microsoft.Extensions.DependencyInjection.Abstractions.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Program Files\MyAddin\Microsoft.Extensions.DependencyInjection.Abstractions.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
if I'm not wrong the binding redirect is not working and some dependencies are looking for the wrong library version
I guess because Excel maybe looking the redirects in another place?
I tried to write my own Assembly loader in the ThisAddin.cs file
private static ILogger<ThisAddIn> _logger;
static ThisAddIn()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string assemblyPath = string.Empty;
if (args.Name.Contains("Microsoft.Extensions.DependencyInjection.Abstractions"))
assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Microsoft.Extensions.DependencyInjection.Abstractions.dll");
if (args.Name.Contains("Microsoft.Extensions.Options"))
assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Microsoft.Extensions.Options.dll");
if (!string.IsNullOrEmpty(assemblyPath))
{
_logger.LogInformation($"resolving assembly {assemblyPath}/{args.Name}");
return LoadAssembly(assemblyPath, args.Name);
}
_logger.LogInformation($"assembly {assemblyPath}/{args.Name} NOT resolved");
return null;
}
private static Assembly LoadAssembly(string assemblyPath, string name)
{
if (File.Exists(assemblyPath))
{
var assembly = Assembly.LoadFrom(assemblyPath);
var assemblyName = assembly.GetName();
var requestedName = new AssemblyName(name);
if (assemblyName.Version >= requestedName.Version)
{
_logger.LogInformation($"resolved {assembly.FullName}");
return assembly;
}
}
_logger.LogInformation($"assembly {assemblyPath}/{name} NOT resolved");
return null;
}
This code works fine when running the addin in Visual studio debugging (actually the entire addin works fine when launching it from VS2022), but seems to not work/log in the deployed version.
Is there a way to make the redirects work for the deployed version? Is there any suggestion on how to approach for distribute the Addin? Maybe a better option than a Setup project ?
Thank you