Azure DevOps pipeline unit tests with library dependency not working

0

If a C# project with a unit tests that uses MS fakes and a 3rd party library in .dll I'll refer to as X. In my unit test project the .dll is referenced locally and is packed in the Azure GIT. Now when building the solution via an Azure DevOps pipeline using a MS hosted agent everything works fine, but the units tests keep failing at every test.

The unit test yaml looks as follows:

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*UnitTests*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'

The error:

System.IO.FileNotFoundException : Could not load file or assembly 'Y, Version=4.0.0.0, Culture=neutral, PublicKeyToken=...' or one of its dependencies. The system cannot find the file specified.

Y is another .dll which I assume the first library depends on, although using dumpbin /dependents X.dll doesn't show it. I've tried adding Y to the project as reference yielding the same result. I've tried using the following Powershell script to add it:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $dllpath = 'dll'
      $files = Get-ChildItem -Path $dllpath -Name
      [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
      $publish = New-Object System.EnterpriseServices.Internal.Publish
      Foreach ($file in $files)
      {
        $publish.GacInstall($dllpath + "\" + $file)
      }

which changes the errors as follows:

System.Runtime.InteropServices.COMException : Retrieving the COM class factory for component with CLSID {...} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

I've checked online for the error, but couldn't find any solution to solve the issue.

Anybody have any idea's how I can get the unit tests working on the MS Agent?

Configuration Manager

unit-testing
dll
azure-devops
azure-pipelines
asked on Stack Overflow May 15, 2020 by 404 • edited May 26, 2020 by 404

1 Answer

1

I've tried adding Y to the project as reference yielding the same result.

When you add the Y to the unit test project, make sure the path of the dll file in the project file is relative path:

  <ItemGroup>
    <Reference Include="Y">
      <HintPath>..\Y.dll</HintPath>
    </Reference>
  </ItemGroup>

And confirm the properties Copy Local is set as True:

which changes the errors as follows:...

It seems dll is registered on the 32/64 bit version of the windows registry, but the application is using the different bit version. Try to change your project build properties Platform target from Any CPU to x86 or x64 based on your system and it may fix the issue.

Check the similar thread for some more details.

Hope this helps.

answered on Stack Overflow May 18, 2020 by Leo Liu-MSFT

User contributions licensed under CC BY-SA 3.0