C# TFS Api throws exception when called from another assembly?

0

I have a minimal working exmaple on github to reproduce my case. The following code throws an exception:

        var wc = new WindowsCredential(new NetworkCredential(userName, password, domain));
        var credentials = new VssCredentials(wc);
        var buildHttpClient = new Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient(new Uri(tfsUrl), credentials);

The exception is as follows (I apologize for the German message, it means "File or dependency not found"):

System.IO.FileNotFoundException
HResult=0x80070002
Message=Die Datei oder Assembly "System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.
Source=Microsoft.VisualStudio.Services.WebApi

This exception is only thrown, if the code is called from another assembly such as my ConsoleExample or TestExample. The code works fine in the assembly where it is located. It does not seem important what you insert as credentials. It seems like you don't even need a running TFS server.

Any help is appreciated. I could reproduce this with Visual Studio 2019 and 2017 and on two computers. Note: The code works with an older version of the respective NuGet packages.

c#
tfs
asked on Stack Overflow Aug 10, 2020 by tomwaitforitmy

1 Answer

0

Test with the sample, I could reproduce this issue. The root cause of this issue could be that the ConsoleExample project lacks dependencies or references.

Assembly name: Newtonsoft.Json System.Net.Http.Formatting System.IdentityModel.Tokens.Jwt

You need to add the dependent Assembly or assembly reference in ConsoleExample project.

Here are two methods to solve this issue:

Method1: Add the dependent Assembly in ConsoleExample -> App.config.

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.7.1.0" newVersion="6.7.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Method2: You could manually add the references to the ConsoleExample Project via Add-> References -> Browser

enter image description here

Since the BuildClientExample project could run successfully, you can find these assemblies on your local machine

Update:

unit test example

I run the Unit test example and get the same issue.

Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, ...

To solve this issue, you could add the app.config file to the example and manually add the target References at the same time.

enter image description here

Then you need to add the same dependentAssembly to the app.config file.

And you need to make sure that the assemblies referenced by these two methods are the same version.

answered on Stack Overflow Aug 11, 2020 by Kevin Lu-MSFT • edited Aug 14, 2020 by Kevin Lu-MSFT

User contributions licensed under CC BY-SA 3.0