【问题标题】:C# TFS Api throws exception when called from another assembly?C# TFS Api 从另一个程序集调用时抛出异常?
【发布时间】:2020-11-30 04:35:49
【问题描述】:

我有一个minimal working exmaple on github 来重现我的情况。以下代码抛出异常:

        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);

异常如下(我为德语消息道歉,它的意思是“找不到文件或依赖项”):

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

仅当从另一个程序集(例如我的 ConsoleExample 或 TestExample)调用代码时,才会引发此异常。该代码在它所在的程序集中运行良好。您作为凭据插入的内容似乎并不重要。看来您甚至不需要运行 TFS 服务器。

感谢任何帮助。我可以使用 Visual Studio 2019 和 2017 以及在两台计算机上重现这一点。注意:该代码适用于相应 NuGet 包的旧版本。

【问题讨论】:

    标签: c# tfs


    【解决方案1】:

    使用示例进行测试,我可以重现此问题。此问题的根本原因可能是 ConsoleExample 项目缺少依赖项或引用。

    程序集名称:Newtonsoft.JsonSystem.Net.Http.FormattingSystem.IdentityModel.Tokens.Jwt

    您需要在ConsoleExample项目中添加依赖的Assembly或Assembly引用。

    这里有两种方法可以解决这个问题:

    方法1:在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>
    

    方法2:您可以通过添加->引用->浏览器

    手动添加对ConsoleExample Project的引用

    由于BuildClientExample 项目可以成功运行,您可以在本地机器上找到这些程序集

    更新:

    单元测试示例

    我运行单元测试示例并遇到同样的问题。

    无法加载文件或程序集'System.Net.Http.Formatting, 版本=5.2.3.0,文化=中性,...

    要解决此问题,您可以将app.config 文件添加到示例中,同时手动添加add the target References。

    那么你需要在app.config文件中添加同样的dependentAssembly

    并且你需要确保这两个方法引用的程序集是相同的版本。

    【讨论】:

    • 您对修复是正确的。方法1对我有用。但是,方法 2 不适用于单元测试项目。似乎我每次都需要添加一个 app.config 包含这三个依赖项。那么这是微软的错误,不是吗?我从未见过任何需要添加手动引用的 NuGet 包。为什么是这三个而不是其余的?如果需要,它们应该被传递并自动添加,不是吗?
    • 嗨@tomwaitforitmy。在BuildClientExample-&gt;app.config中,也有这样的依赖。 ConsoleExample 已经引用了 BuildClientExample,但它似乎无法使用依赖程序集或引用。因此,需要将相同的依赖项添加到 ConsoleExample 项目中。这就是为什么这三个而不是其余的原因。
    • 确切地说:它不能重复使用。所以这是一个错误吧?
    • 嗨 @tomwaitforitmy 不,这就是 C# 的工作原理。当项目 B 引用项目 A 时,项目 B 不能间接引用项目 A 所引用的程序集。在项目 B 中,项目 A 被显式引用。由于项目 B 实际上不包含任何显式使用项目 A 引用的程序集的代码。因此它们不会被添加到项目 b 中并且它们无法重用。所以你需要在项目B中声明。这是一张票:stackoverflow.com/a/21055664/13464420
    • 嗨,凯文,感谢您的回答。我会检查一下,然后再找你。对我来说似乎仍然很奇怪,一个控制台应用程序可以很容易地修复,而一个单元测试项目需要两者。这在我之前从未发生过任何其他依赖项,但是很好。
    猜你喜欢
    • 2019-08-24
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2016-07-28
    • 1970-01-01
    • 2013-12-21
    • 2012-01-11
    相关资源
    最近更新 更多