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.

1 Answers1

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.

Kevin Lu-MSFT
  • 8,502
  • 2
  • 3
  • 11
  • You are right about the fix. Method 1 worked for me. Method 2 didn't work, however, for the unit test project. It seems like I need to add an app.config every time containing these three dependencies all the time. This is a Microsoft Bug then, isn't it? I have never seen any NuGet package requiring to add manual references. Why these three and not the rest? They should be added transitively and automatically if necessary, shouldn't they? – tomwaitforitmy Aug 11 '20 at 07:00
  • Hi @tomwaitforitmy. In `BuildClientExample->app.config`, there are also such dependencies. The `ConsoleExample` has referenced the BuildClientExample, but it does not seem to be able to use dependent assemblies or references. Therefore, the same dependency needs to be added to the ConsoleExample project. This is why these three and not the rest. – Kevin Lu-MSFT Aug 11 '20 at 07:07
  • Exactly: It is not able to re-use. So it's a bug right? – tomwaitforitmy Aug 11 '20 at 10:53
  • Hi @tomwaitforitmy No. This is how C# works. When the project B refernce Project A, Project B cannot indirectly reference the assembly referenced by Project A. In Project B, Project A is explicitly referenced. Since project B doesn't actually contain any code that explicitly uses assembly referenced by Project A. So they will not be added to Project b and they couldn't be able to re-use. So you need to declare in project B. Here is a ticket: https://stackoverflow.com/a/21055664/13464420 – Kevin Lu-MSFT Aug 12 '20 at 08:02
  • I cannot make it work for the unit test example. Both of your solutions don't seem to work there. Any suggestions? – tomwaitforitmy Aug 13 '20 at 13:23
  • Hi @tomwaitforitmy. From you unit test sample, it has no app.config file. So you could try to add an app.config file to your unit test sample. Please check the update. – Kevin Lu-MSFT Aug 14 '20 at 01:50
  • I tried exactly your two proposed solutions. They work for the console example, but not for the unit test example. I copied the app.config from console to unit test. It doesn't work somehow or I am blind :). – tomwaitforitmy Aug 14 '20 at 06:08
  • @tomwaitforitmy Sorry, I can reproduce it now. AND to solve this issue, you need to add `app.config file` and `manually add reference` at the same time. In other words, you need to do both methods at the same time. And you need to make sure that the assemblies referenced by these two methods are the **same** version. – Kevin Lu-MSFT Aug 14 '20 at 06:23
  • Hi @tomwaitforitmy. Could you please check if it could work when you use both two solutions at the same time? Feel free to let me know if it could solve this issue. – Kevin Lu-MSFT Aug 17 '20 at 01:02
  • 1
    Hey Kevin, thanks for your answer. I will check this and come back to you. Seems still weird to me that a Console application can be fixed easy while a Unit Test project needs both. This never happened to me before with any other dependency, but well. – tomwaitforitmy Aug 24 '20 at 13:12
  • @tomwaitforitmy.I also have a little doubt about this. But you can try my answer and check if it can solve this problem. Feel free to let me know the result. – Kevin Lu-MSFT Aug 25 '20 at 01:05
  • Hey Kevin, your solution works if I add references to newtonsoft and http.formatting. I don't have to add "System.IdentityModel.Tokens.Jwt" in the test project for reasons unknown to me. Neither as reference nor in the app.config. While in contrast for the console app, I have to add all 3 only in the app.config. *If I downgrade Microsoft.TeamFoundation.Build.WebApi to 5.2.3 (from 5.2.7) nothing at all is necessary in neither console nor test assembly.* – tomwaitforitmy Aug 28 '20 at 12:10