Having what appears to be runtime issues with Project References in Visual Studio 2017 Test Runner. The Unit Test CSPROJ builds just fine with TargetFramework=net47, but at execution time we get the following message from MSTEST or XUNIT. Using Microsoft.NET.Test.Sdk v15.0.0.
Test Execution Error (x86): Serilog Seq Extension
System.MissingMethodException : Method not found: '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, Boolean, Int32)'.
Unit Test Example - Serilog
[Fact]
public void TestMethod1()
{
LoggerConfiguration loggerConfig = new LoggerConfiguration();
loggerConfig.MinimumLevel.Debug();
loggerConfig.WriteTo.LiterateConsole();
loggerConfig.WriteTo.Seq("http://localhost:65454");
}
If we reference net462 projects, we get the same result so we believe it is related to VS 2017, not .NET Framework version. We have never seen this error with VS 2015. Seems like there is an issue loading DLL extensions with optional parameters / matching signatures, etc. The method clearly exists or it wouldn't compile - why at runtime is this crashing out?
If I just use local nuget packages it works fine - this only seems to be a problem when referencing any Projects via ProjectReference in .NET Core CSPROJ. It doesn't seem to handle the dependency tree properly.
Another example using KeyVault where VS Test Runner cannot find the extension methods properly...
Test Execution Error (x86): KeyVault Extension
Message: System.MissingMethodException : Method not found: 'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler[])'.
Unit Test Example - 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;
}
Discovered this odd issue with
dotnet testis two-fold. After runningdotnet test --diagand reviewing the output, it led me to realize there are newer releases ofMicrosoft.NET.Test.Sdkwhich version15.0.0was masking the real problem. Once I upgraded the nuget to15.3.0-preview-20170502-03, a different exception appeared.Error Source: Microsoft.Rest.ClientRuntime
Now this is interesting - the
MissingMethodExceptionwas masking the real problem which was buried inSystem.Net.Http. The second realization is that this base library has a bug which prevents the type from being loaded. Once I nuget updatedSystem.Net.Httpto version4.3.1, the issue went away and my Project References started working again.Conclusion
Update
Microsoft.NET.Test.SDKto latest preview andSystem.Net.Httpto latest version to get past the weirdMissingMethodExceptionwith dotnet test. You can track the open issue on github here.Option #2 - Excluding Package Reference Assets
For latest VS 2017
CSPROJformats - the following config also fixes this as it supresses copyingSystem.Net.Httpto the Build Output path which by default loads the GAC'd version4.0.0.0.Option #3 - Assembly Binding Redirect
dotnetcore will follow any runtime binding redirects you place in yourapp.config, so any nuget dependencies you have toSystem.Net.Httpto version4.1.*, you can redirect to the latest version or revert to the last stable version4.0.