Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Creating instance of TestServer throws exception after upgrading to VS 2017 #959

Closed
Chris197 opened this issue Mar 9, 2017 · 12 comments
Closed

Comments

@Chris197
Copy link

Chris197 commented Mar 9, 2017

Hi, after successfully converting a VS2015 ASP.NET Core solution to VS2017, the only thing that I can't get to work is the integration test I had set up using Microsoft.AspNetCore.TestHost.TestServer (which, when it works is awesome by the way). The code I use to run is very simple:

var builder = new WebHostBuilder().UseStartup<Startup>();
var server = new TestServer(builder);

At the point at which it tries to construct TestServer, the following exception is thrown:

System.IO.FileLoadException: 'Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

Extra info under 'FusionLog' states:

Pre-bind state information LOG: DisplayName = Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 (Fully-specified) LOG: Appbase = file:///C:/<ommitted>/bin/Debug/net451 LOG: Initial PrivatePath = NULL Calling assembly : Microsoft.ApplicationInsights.AspNetCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

LOG: This bind starts in default load context. LOG: Using application configuration file: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 LOG: Attempting download of new URL file:///C:/<ommitted>/bin/Debug/net451/Microsoft.Extensions.Configuration.Abstractions.DLL. WRN: Comparing the assembly name resulted in the mismatch: Build Number ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

I am riding the LTS train of dotnet core (v 1.0.3) and using Microsoft.AspNetCore.TestHost v 1.0.2. Looking at the other projects in my solution, any references to Microsoft.Extensions.Configuration.Abstractions are transitively included through other dependencies and are all version 1.0.2. All projects target framework dotnet451 (not appcore). This all works fine in my VS2015 solution and I haven't changed the versions going to VS2017. What I have tried:

  • Deleting all bin and obj directories, restart and recompile after everything I changed.
  • Upgrading Microsoft.AspNetCore to v 1.0.4
  • targeting dotnet452 instead of dotnet451
  • Reproducing the situation by creating a simple new VS2017 project. This error was reproducable, but was solved when I upgraded Microsoft.AspNetCore to v 1.0.4. Unfortunately this same action did not solve the problem in my main solution.
  • Adding a bindingRedirect in web.config to force the said library to use version 1.0.2. but noticed no difference, in fact, I then saw that the application config file in the bin directory already contains this redirect (and others) as generated by dot net.

The next thing I'm going to try is to upgrade everything to the 1.1.1 FTS version, although I feel that really the LTS should just work.

Any help would be much appreciated.

@Chris197 Chris197 changed the title Creating instance of TestServer throughs exception after upgrade to VS 2017 Creating instance of TestServer throws exception after upgrading to VS 2017 Mar 9, 2017
@JunTaoLuo
Copy link
Contributor

Can you post your project file so I can try to reproduce the issue?

@Chris197
Copy link
Author

Hi John,

Thanks for your reply. I have done some more digging and found that what is happening is that the test project doesn't pick up the config of the host application, and so it is missing the bindingRedirects which would normally be applied. Copying the config file of the host application into the test project, renaming it to app.config and setting it to 'copy always' fixed the problem. However, this led to a second problem where it would seem that any call to a Controller in a different assembly returns 404. Maybe I this should be registered as a separate issue, but I can't tell if this is related or not.

I have created a project in which you can reproduce both problems. I have published the code here.

The file 'README.txt' explains the exact steps to reproduce the issue.

I look forward to your reply,

Chris

@Chris197
Copy link
Author

By the way, if you find in my sample that the test doesn't show up after converting to vs2017, change the following references in the MyTest project:
"Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" ---> "Microsoft.NET.Test.Sdk" Version="15.0.0"
"xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" ---> "xunit.runner.visualstudio" Version="2.2.0"

@Tratcher
Copy link
Member

@anurse has the binding redirect workaround

@analogrelay
Copy link
Contributor

microsoft/vstest#428 describes the issue and the workaround.

@Chris197
Copy link
Author

Ok, thanks for the info. And is there any info on the second problem I reported?

@Chris197
Copy link
Author

Many thanks guys, these workarounds solved my problem and allow me to get on with enjoying VS2017. Just for anybody else stumbling on this issue, I'll summarize what I did below:

Add a new file 'xunit.runner.json' to the test project containg the following code:

{
  "shadowCopy": false
}

Then add the following code to the test project:

<!-- Solves Problem#1 (binding error) https://github.com/Microsoft/vstest/issues/428. -->
<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

<!-- Solves Problem#2 (404 when executing service calls hosted in other assemblies) -->
<!-- https://github.com/Microsoft/vstest/issues/196.-->
<ItemGroup>
    <None Update="xunit.runner.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
</ItemGroup>
<!-- https://github.com/NuGet/Home/issues/4412. -->
<Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
    <ItemGroup>
        <DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
    </ItemGroup>

    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
</Target>

@JunTaoLuo JunTaoLuo added this to the Discussions milestone Mar 14, 2017
@JunTaoLuo
Copy link
Contributor

Closing since the issue is not in Hosting and there are tracking issues and workarounds for the known problems.

@51dario51
Copy link

@Chris197 thank you so much! we spent 2 full working days fighting with the integration tests on vs2017!

@raffaeu
Copy link

raffaeu commented Apr 19, 2017

@Chris197 an extra kudo from me!! 😎

ebekker added a commit to zyborg/Zyborg-tug that referenced this issue Jun 30, 2017
* `dotnet migrate` all the projects, moving from project.json to .csproj
* Created a solution file (dotnet new sln and dotnet sln add ...)
capturing all of the projects to replace the equivalent global.json
* Adjusted *some* of the test run batch files to account for new CLI
format for invoking unit tests
* Had to make some adjustments to TestServer integration tests to
handling binding redirects properly as described by
aspnet/Hosting#959 and
microsoft/vstest#428
muhihsan added a commit to muhihsan/CreditCard-Tests that referenced this issue Jul 7, 2017
@bragma
Copy link

bragma commented Jan 5, 2018

@Chris197

Worked prefectly, thanks a lot! I owe you a beer

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants