【发布时间】:2017-10-08 00:14:56
【问题描述】:
在 Visual Studio 2017 测试运行程序中,项目引用似乎存在运行时问题。单元测试 CSPROJ 使用 TargetFramework=net47 构建得很好,但在执行时我们从 MSTEST 或 XUNIT 收到以下消息。使用Microsoft.NET.Test.Sdk v15.0.0。
测试执行错误 (x86):Serilog Seq 扩展
System.MissingMethodException:找不到方法:'Serilog.LoggerConfiguration Serilog.SeqLoggerConfigurationExtensions.Seq(Serilog.Configuration.LoggerSinkConfiguration, System.String, Serilog.Events.LogEventLevel, Int32, System.Nullable
1<System.TimeSpan>, System.String, System.String, System.Nullable1, System.Nullable1<Int64>, Serilog.Core.LoggingLevelSwitch, System.Net.Http.HttpMessageHandler, System.Nullable1,布尔值,Int32)'。
单元测试示例 - Serilog
[Fact]
public void TestMethod1()
{
LoggerConfiguration loggerConfig = new LoggerConfiguration();
loggerConfig.MinimumLevel.Debug();
loggerConfig.WriteTo.LiterateConsole();
loggerConfig.WriteTo.Seq("http://localhost:65454");
}
如果我们引用 net462 项目,我们会得到相同的结果,因此我们认为它与 VS 2017 相关,而不是 .NET Framework 版本。我们从未在 VS 2015 中看到此错误。似乎在加载带有可选参数/匹配签名等的 DLL 扩展时存在问题。该方法显然存在或无法编译 - 为什么在运行时会崩溃?
如果我只使用本地 nuget 包,它可以正常工作 - 这似乎只是在 .NET Core CSPROJ 中通过 ProjectReference 引用任何项目时出现问题。它似乎没有正确处理依赖树。
另一个使用 KeyVault 的示例,其中 VS Test Runner 无法正确找到扩展方法...
测试执行错误 (x86):KeyVault 扩展
消息:System.MissingMethodException:找不到方法:'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler[])'。
单元测试示例 - KeyVault
[Fact]
public void TestMethod1()
{
KeyVaultClient _kvClient = new KeyVaultClient(new AuthenticationCallback(getKeyVaultToken));
}
private static async Task<string> getKeyVaultToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
ClientCredential clientCred = new ClientCredential("test", "account");
AuthenticationResult result = authContext.AcquireTokenAsync(resource, clientCred).Result;
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token");
return result.AccessToken;
}
【问题讨论】:
标签: .net-core mstest visual-studio-2017 xunit .net-4.7