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

NotFound
  • 5,005
  • 2
  • 13
  • 33

1 Answers1

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.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    In my original attempt I already had the reference added with hintpath to a local instance resulting in the same error. I've also tried changing the build configuration explicitely to x86 and x64; also both yielding the same errors. – NotFound May 18 '20 at 13:11
  • @404, Thanks for your reply. Would please share your `HintPath` for Y.dll, and if the y.dll in the output of the unit test project after building? You could use copy task and publish build artifact to output the bin folder to check it. – Leo Liu May 19 '20 at 09:49
  • The HintPath is `..\dll\Y.dll`, which is the same for `X.dll` (so basically a folder in the root of the `.sln` file). In a build where I skip the unit test the file can be found in the root of the compressed published file/artifact next to all other libraries. – NotFound May 19 '20 at 11:12
  • @404, Thanks for you reply. So, the Y.dll file is added to source control, can you see it in your repo? In addition, did you build your unit test project and check if the Y.dll is in the output directory of your unit test project? – Leo Liu May 21 '20 at 09:44
  • 1
    I can see the dll in my repository. I've checked the build output and indeed the dll was not included. In my project I changed `Embed interop types` to false and `Copy local` to true. The result of that is however the same `System.Runtime.InteropServices.COMException` as mentioned in the OP. – NotFound May 22 '20 at 08:19
  • 1
    @404, Thanks you reply. So, you have resolved the first error, Could not load file or assembly? And still have the second error `System.Runtime.InteropServices.COMException`? How about the result when you build it in your local without azure devops? This error should not related to Azure devops, but related to the build configuration, check the build configuration when you build on your local, or you can share it. – Leo Liu May 22 '20 at 08:35
  • 1
    Correct, the first error seems to be resolved with that, but I still run into the second error. If I run the tests locally using the test explorer it passes 84 out 84. I've also tried using a command prompt with `vstest.console.exe` on the build dll of the unit test which also results with all tests passing. – NotFound May 22 '20 at 08:52
  • @404, OK, glad to know, we have resolved the first error. Would you please share the configuration manager on your local? Try to set `x64` in a .runsettings file.https://stackoverflow.com/questions/1496214/how-to-solve-com-exception-class-not-registered-exception-from-hresult-0x80040 – Leo Liu May 22 '20 at 09:26
  • Hey. Sorry for the late reply. I've added an image of he configuration manager on my local to the question (where I've tried all options). I've also added a `.runsettings` file to the build and adjusted the pipeline to use it. I've tried setting it both to x64 and x86, both yielding the same results – NotFound May 26 '20 at 08:25