Dot-Net-Core

System.MissingMethodException 單元測試

  • December 22, 2021

Visual Studio 2017 Test Runner 中的項目引用似乎存在執行時問題。單元測試CSPROJ可以很好地建構TargetFramework=net47,但在執行時我們會從 MSTEST 或 XUNIT 收到以下消息。使用Microsoft.NET.Test.Sdkv15.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.Nullable 1<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 引用任何項目時出現問題。它似乎沒有正確處理依賴樹。

另一個使用KeyVaultVS Test Runner 無法正確找到擴展方法的範例…

測試執行錯誤 (x86):KeyVault 擴展

消息:System.MissingMethodException:找不到方法:‘無效 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並查看輸出後,我意識到有更新的Microsoft.NET.Test.Sdk版本15.0.0掩蓋了真正的問題。一旦我將 nuget 升級到15.3.0-preview-20170502-03,就會出現一個不同的異常。

錯誤來源:Microsoft.Rest.ClientRuntime

System.TypeLoadException:‘類型違反了繼承安全規則:‘System.Net.Http.WebRequestHandler’。派生類型必須要麼與基類型的安全可訪問性相匹配,要麼難以訪問。

現在這很有趣——它MissingMethodException掩蓋了隱藏在其中的真正問題System.Net.Http。第二個認識是這個基礎庫有一個錯誤,它會阻止類型被載入。一旦我更新System.Net.Http到 version4.3.1,問題就消失了,我的項目參考又開始工作了。

結論

更新Microsoft.NET.Test.SDK到最新預覽版System.Net.Http最新版本MissingMethodException以通過dotnet test克服怪異。您可以在此處跟踪 github 上的未解決問題

選項 #2 - 排除包參考資產

對於最新的 VS 2017CSPROJ格式 - 以下配置也修復了此問題,因為它禁止複製System.Net.Http到預設載入 GAC 版本的建構輸出4.0.0.0路徑。

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

選項 #3 - 程序集綁定重定向

dotnet核心將遵循您放置在您的任何執行時綁定重定向,因此您必須對版本的app.config任何 nuget 依賴項,您可以重定向到最新版本或恢復到上一個穩定版本。System.Net.Http``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>

引用自:https://stackoverflow.com/questions/43873978