gpt4 book ai didi

.net-core - System.MissingMethodException 单元测试

转载 作者:行者123 更新时间:2023-12-04 13:36:53 25 4
gpt4 key购买 nike

出现运行时问题项目引用 在 Visual Studio 2017 测试运行器中。单元测试 CSPROJ 使用 TargetFramework=net47 构建得很好,但在执行时,我们从 MSTEST 或 XUNIT 收到以下消息。使用 Microsoft.NET.Test.Sdk v15.0.0。
测试执行错误 (x86):Serilog Seq 扩展

System.MissingMethodException : Method not found: 'Serilog.LoggerConfiguration Serilog.SeqLoggerConfigurationExtensions.Seq(Serilog.Configuration.LoggerSinkConfiguration, System.String, Serilog.Events.LogEventLevel, Int32, System.Nullable1<System.TimeSpan>, System.String, System.String, System.Nullable1, System.Nullable1<Int64>, Serilog.Core.LoggingLevelSwitch, System.Net.Http.HttpMessageHandler, System.Nullable1, Boolean, 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 包,它可以正常工作 - 这似乎只是通过 ProjectReference 引用任何项目时出现的问题。在 .NET Core CSPROJ 中。它似乎没有正确处理依赖树。
另一个使用 KeyVault 的示例VS Test Runner 无法正确找到扩展方法的地方......
测试执行错误 (x86):KeyVault 扩展

Message: System.MissingMethodException : Method not found: '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;
}

最佳答案

发现 dotnet test 这个奇怪的问题是双重的。运行后dotnet test --diag并查看输出,这让我意识到有 newer releases of Microsoft.NET.Test.Sdk 哪个版本15.0.0掩盖了真正的问题。一旦我将 nuget 升级到 15.3.0-preview-20170502-03 ,出现了不同的异常。

错误来源:Microsoft.Rest.ClientRuntime

System.TypeLoadException: 'Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.'



现在这很有趣 - MissingMethodException掩盖了隐藏在 System.Net.Http中的真正问题.第二个认识是这个 base library has a bug which prevents the type from being loaded .曾经我 nuget updated System.Net.Http to version 4.3.1 ,问题消失了,我的项目引用又开始工作了。

结论

更新 Microsoft.NET.Test.SDK to latest preview System.Net.Http to latest version克服怪异 MissingMethodException点网测试 .您可以 track the open issue on github here .

选项 #2 - 排除包引用 Assets

最新 VS 2017 CSPROJ格式 - 以下配置也修复了此问题,因为它禁止复制 System.Net.Http构建输出 默认加载 GAC 版本的路径 4.0.0.0 .

<PackageReference Include="System.Net.Http" Version="4.3.1">
<ExcludeAssets>All</ExcludeAssets>
</PackageReference>

选项 #3 - 程序集绑定(bind)重定向
dotnet核心将遵循您在 app.config 中放置的任何运行时绑定(bind)重定向。 ,因此您必须对 System.Net.Http 的任何 nuget 依赖项到版本 4.1.* ,您可以重定向到最新版本或恢复到上一个​​稳定版本 4.0 .

<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- Another PackageReference dependency binds to 4.1.1 which is busted, we leverage .NET Core redirection and revert to CLR 4.0.0 -->
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

关于.net-core - System.MissingMethodException 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43873978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com