Search code examples
c#.netwixwindows-installeranglesharp

Could not load file or assembly System.Memory, Version=4.0.1.1, Culture=neutral,


TL;DR

I was tasked to migrate the Office add-in (VSTO) installer from Windows Installer to WiX.

As usual, I created a WiX project (HearWave), added all the project files to the xml-config of the component. But after installing the add-in and launching Outlook I get this error:

Could not load file or assembly 'System.Memory, Version=4.0.1.1, 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).

Windows Installer works. Same result if no changes have been made (except adding WiX) to the Add-in project.

Whole story

Last week I was tasked to migrate from legacy Windows Installer to WiX.

First I just created a copy of the solution, deleted the old installer project and added the WiX project. I duplicated from the old project what files are copied to the XML-config of the component.

The System.Memory and others are libs from NuGet. They are one of AngleSharp's dependencies.

This is how the config looks like now (after various attempts to solve the problem):

<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
    <Fragment>
        <ComponentGroup Id="AddinComponent" Directory="INSTALLFOLDER">
            <Component Guid="*" Id="AngleSharp.dll">
                <File Source="AngleSharp.dll" />
            </Component>
            <Component Guid="*" Id="System.Buffers.dll">
                <File Source="System.Buffers.dll" />
            </Component>
            <Component Guid="*" Id="System.Memory.dll">
                <File Source="System.Memory.dll" />
            </Component>
            <Component Guid="*" Id="System.Numerics.Vectors.dll">
                <File Source="System.Numerics.Vectors.dll" />
            </Component>
            <Component Guid="*" Id="System.Runtime.CompilerServices.Unsafe.dll">
                <File Source="System.Runtime.CompilerServices.Unsafe.dll" />
            </Component>
            <Component Guid="*" Id="System.Text.Encoding.CodePages.dll">
                <File Source="System.Text.Encoding.CodePages.dll" />
            </Component>
            
            <Component Guid="{8C1D1C47-3D46-4104-ACE7-73F70B898316}">
                <File Id="VSTOFile" Source="nophish.vsto" />
                <File Source="nophish.dll" />
                <File Id="DLLManifest" Source="nophish.dll.manifest" />
                <File Source="Microsoft.Office.Tools.Common.v4.0.Utilities.dll" />
                <File Source="Microsoft.Office.Tools.Outlook.v4.0.Utilities.dll" />

                <!--<File Source="C:\GitHub\nophish-outlook-addin-new\packages\AngleSharp.1.1.2\lib\net472\AngleSharp.dll" />
                <File Source="C:\GitHub\nophish-outlook-addin-new\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll" />
                <File Source="C:\GitHub\nophish-outlook-addin-new\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll" />
                <File Source="C:\GitHub\nophish-outlook-addin-new\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll" />
                <File Source="C:\GitHub\nophish-outlook-addin-new\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll" />
                <File Source="C:\GitHub\nophish-outlook-addin-new\packages\System.Text.Encoding.CodePages.8.0.0\lib\net462\System.Text.Encoding.CodePages.dll" />-->
                
                <!--<File Source="..\packages\AngleSharp.1.1.2\lib\net472\AngleSharp.dll" />
                <File Source="..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll" />
                <File Source="..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll" />
                <File Source="..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll" />
                <File Source="..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll" />
                <File Source="..\packages\System.Text.Encoding.CodePages.8.0.0\lib\net462\System.Text.Encoding.CodePages.dll" />-->

                <!--<File Source="AngleSharp.dll" />
                <File Source="System.Buffers.dll" />
                <File Source="System.Memory.dll" />
                <File Source="System.Numerics.Vectors.dll" />
                <File Source="System.Runtime.CompilerServices.Unsafe.dll" />
                <File Source="System.Text.Encoding.CodePages.dll" />-->
                

                <RegistryKey Root="HKLM" Key="..." >
                    ...
                </RegistryKey>
            </Component>
        </ComponentGroup>
    </Fragment>
</Wix>

After examining it, it turned out that accessing code that uses AngleSharp leads to the exception mentioned above.

I tried to solve the problem in various ways I found. What I tried:

  • add <dependentAssembly> to app.config.
  • reinstall AngleSharp and its dependencies from NuGet
  • downgrade libraries
  • and other things.

After nothing helped, I decided that maybe the problem was that for the new installer I had to edit the code of the add-on itself and add a project with CustomAction.

So I made another copy of the original project. In it, I added the WiX installer project along with the Windows Installer without making any additional changes to the add-on project itself.

As a result, when I build the Win Installer project, the add-in works as it should. When I build a WiX project, however, the add-in does not work because of the above error.


The weirdest thing is that when building a Windows Installer project, this error appears as a warning in the Output window but Add-in works as expected.

But when building WiX, no errors or warnings appear in the build output but Add-in throws an exception, the same as warning shown while building Win Installer.

win installer build log

Rebuild started at 1:49 AM...
Restored C:\GitHub\nophish-outlook-addin-new\SAOA\SAOA.wixproj (in 6 ms).
1>------ Rebuild All started: Project: nophish, Configuration: Release Any CPU ------
1>  nophish -> C:\GitHub\nophish-outlook-addin-new\bin\Release\nophish.dll
------ Starting pre-build validation for project 'Setup' ------ 
WARNING: Unable to find dependency 'SYSTEM.RUNTIME.COMPILERSERVICES.UNSAFE' (Signature='B03F5F7F11D50A3A' Version='4.0.4.1') of assembly 'System.Memory.dll'
WARNING: Unable to find dependency 'SYSTEM.MEMORY' (Signature='CC7B13FFCD2DDD51' Version='4.0.1.1') of assembly 'AngleSharp.dll'
WARNING: Unable to find dependency 'SYSTEM.TEXT.ENCODING.CODEPAGES' (Signature='B03F5F7F11D50A3A' Version='6.0.0.0') of assembly 'AngleSharp.dll'
------ Pre-build validation for project 'Setup' completed ------
2>------ Rebuild All started: Project: Setup, Configuration: Release ------
Building file 'C:\GitHub\nophish-outlook-addin-new\Setup\Release\Setup.msi'...
Packaging file 'System.Memory.dll'...
Packaging file ...
...
...
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
========== Rebuild completed at 12:40 AM and took 07.902 seconds ==========

wix build log

Rebuild started at 1:47 AM...
1>------ Rebuild All started: Project: nophish, Configuration: Release Any CPU ------
Restored C:\GitHub\nophish-outlook-addin-new\SAOA\SAOA.wixproj (in 1 ms).
1>  nophish -> C:\GitHub\nophish-outlook-addin-new\bin\Release\nophish.dll
2>------ Rebuild All started: Project: SAOA, Configuration: Release x86 ------
2>SAOA -> C:\GitHub\nophish-outlook-addin-new\SAOA\bin\x86\Release\SAOA.msi
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
========== Rebuild completed at 1:47 AM and took 06.122 seconds ==========

I don't know what else can I do and can't find anything helpful. Any ideas?

Also version of System.Memory used in project is v4.5.5 in nuget and v4.0.1.2 (version of reference). And the exception reports v4.0.1.1 (that's v4.5.4 in nuget, if I'm not mistaken), which is never mentioned in any project file.

UPD

In response to Michael Liu's comment.

Files DIFF

The only difference in the file tree is the nophihs.InstallState file. But I assume that this is leftovers from Win Installer.

I wrote a simple batch script to compare the files using fc. There is:

  • no difference in references;
  • 37 bytes difference in nophihs.dll
  • differences in nophihs.dll.manifest and nophihs.vsto because signing is used.

.NET Fusion (assembly loading)

On Michael's recommendation, I ran .NET Fusion (didn't know about it).

I found that when I install the add-on using wix, it cannot find the nophihs.dll.config file. Even though this config file exists.

This file is not part of the package. It is copied in after the installation process is complete, as it contains the user settings.

This file is identical for both installers:

Checking file: C:\Program Files\nophihs\nophihs.dll.config
Comparing with: C:\Program Files (x86)\nophihs\nophihs.dll.config
Comparing text files: nophihs.dll.config
Comparing files C:\PROGRAM FILES\NOPHIHS\nophihs.dll.config and C:\PROGRAM FILES (X86)\NOPHIHS\NOPHIHS.DLL.CONFIG
FC: no differences encountered

Wix

*** Assembly Binder Log Entry  (9/5/2024 @ 10:32:52 PM) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
 (Fully-specified)
LOG: Appbase = file:///c:/Program Files (x86)/nophihs/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = OUTLOOK.EXE
Calling assembly : AngleSharp, Version=1.1.2.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea.
===
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\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///c:/Program Files (x86)/nophihs/System.Memory.DLL.
LOG: Assembly download was successful. Attempting setup of file: c:\Program Files (x86)\nophihs\System.Memory.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
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.

Win installer

*** Assembly Binder Log Entry  (9/5/2024 @ 10:35:13 PM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files/nophihs/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : AngleSharp, Version=1.1.2.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files\nophihs\nophihs.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.1.1 redirected to 4.0.1.2.
LOG: Post-policy reference: System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files/nophihs/System.Memory.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Program Files\nophihs\System.Memory.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: Binding succeeds. Returns assembly from C:\Users\t\AppData\Local\assembly\dl3\DREH4Z3W.5EE\O28WKLDZ.R43\d1ebdddc\005f960a_8c62d801\System.Memory.dll.
LOG: Assembly is loaded in default load context.

Solution

  • Summarizing Comments

    • Check the content in the installation directory for differences.
    • Use .NET Fusion to analyze the failing assembly loading process.
    • Check the registry for differences.