小编Rob*_*ing的帖子

TeamCity 7上的AutoFixture和Moq与NUnit的程序集版本冲突

我以前对我的解决方案的所有单元测试都包含在一个库中,并且最近被拆分了.当位于单个程序集中时,所有测试都在本地和TeamCity上传递,但是在分离时会出现版本冲突.

配置:

  • Team City 7.1.5(build 24400)
  • AutoFixture 3.20.2
  • AutoFixture.AutoMoq 3.20.2
  • Moq 4.2.1402.2112
  • NUnit 2.6.3

我有几个单元测试程序集,它们都引用了一个基本测试库.所有测试程序集都使用上面列出的NuGet包.

在开发机器上运行测试(VS 2015)时,所有测试都成功通过.

运行团队城市构建时,会引发以下错误:

System.IO.FileLoadException:无法加载文件或程序集'Moq,Version = 4.1.1308.2120,Culture = neutral,PublicKeyToken = 69f491c39445e920'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(来自HRESULT的异常:0x80131040)在Ploeh.AutoFixture.AutoMoq.MockPostprocessor.Create(对象请求,ISpecimenContext上下文)

在我的解决方案的任何地方都没有提到Moq 4.1.1308.2120,所以我知道它必须是AutoFixture的参考.

将AutoFixture更新为3.31.3没有任何区别.

我在所有测试程序集的app.config文件中都有以下Binding Redirect:

<dependentAssembly>
  <assemblyIdentity name="Moq" publicKeyToken="69f491c39445e920" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.2.1402.2112" newVersion="4.2.1402.2112" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

我无法将我的Moq版本降级到4.1.1308.2120,因为我在测试中使用了4.2的功能.

在我看来,Team City忽略了重定向.我不知道为什么,并且尝试了这些程序集的每个版本组合,我无法让Team City成功运行测试.

teamcity nunit moq autofixture teamcity-7.0

6
推荐指数
1
解决办法
1077
查看次数

向所有传出的 Refit 请求添加动态标头

我在使用 IHttpClientFactory 的 .NET Core 3.1 应用程序中使用 Refit (5.1.67) 作为我的 HttpClient 包装器。

我调用的 API 使用客户端凭据令牌进行保护。

我正在用这个注册客户:

services.AddRefitClient<ISomeApiClient>().ConfigureHttpClient(c =>
            c.BaseAddress = new Uri(Configuration["BaseUrlFromConfig"]));
Run Code Online (Sandbox Code Playgroud)

客户端的方法如下所示:

public interface ISomeApiClient
{
    [Get("/api/somewhere")]
    Task<IEnumerable<MyResponseObject>> GetItems([Header("X-User-Id")] string userId, [Header("Authorization")] string accessToken);

    [Get("/api/somewhere-else")]
    Task<MyResponseObject> GetItem([Header("X-User-Id")] string userId, [Header("Authorization")] string accessToken, int id);
}
Run Code Online (Sandbox Code Playgroud)

我想避免的是每次调用端点时都必须显式传递 accessToken 和 userId (如上)。理想情况下,我希望我的客户看起来像这样:

public interface ISomeApiClient
{
    [Get("/api/somewhere")]
    Task<IEnumerable<MyResponseObject>> GetItems();

    [Get("/api/somewhere")]
    Task<IEnumerable<MyResponseObject>> GetItems(int id);
}
Run Code Online (Sandbox Code Playgroud)

感觉我需要某种用于传出请求的请求中间件,我可以在其中添加这两个标头。如果它们是静态的,我只会装饰整个界面,但因为这些是行不通的运行时值。

我在文档中找不到有关此问题的任何帮助,并且希望得到任何指点。

c# middleware httpclient refit .net-core

2
推荐指数
1
解决办法
6225
查看次数