Aspnetcore: Microsoft.AspNetCore.Mvc, Version=2.2.0.0 is giving an error "System.IO.FileNotFoundException"

Created on 10 Dec 2018  路  10Comments  路  Source: dotnet/aspnetcore

I have started using .Net Core 2.2 recently and I got one WebApp related test cases there, where I am using

TestServer

from

Microsoft.AspNetCore.TestHost

. While executing the test cases I am seeing below error:

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Mvc, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
   at <my code related to Startup.ConfigureServices>
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at <my test>

My Visual studio version is: Professional 2017 15.9.3. The same piece of code used to work with .Net Core 2.0. Is it kind of regression?

area-mvc question

Most helpful comment

I fixed this by following this guide and adding a missing package reference to my integration test project.

<PackageReference Include="Microsoft.AspNetCore.App" />

I had to do a clean and restart of VS 2017 before the change allowed my tests to pass once more.

All 10 comments

We're experiencing the same issue:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
File name: 'Microsoft.AspNetCore, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   at MyTestProject.Program.CreateWebHostBuilder(String[] args)

I have found the workaround, You need to install the package "Microsoft.AspNetCore.Mvc", the latest version though it's not required by your project. I think TesHostpackage is referring to old version and that's why this issue occurred.

I fixed this by following this guide and adding a missing package reference to my integration test project.

<PackageReference Include="Microsoft.AspNetCore.App" />

I had to do a clean and restart of VS 2017 before the change allowed my tests to pass once more.

Is there an ETA to fix this?

Same problem here. Visual studio 2017, updated to version 15.7.1 and externally installed .Net Core 2.2.

It seems updating and patching new version doesn't automatically resolve all dependencies. Regarding the instructions in Microsoft documents , I added all packages in project folder and changed the Sdk in project's file.

<Project Sdk="Microsoft.NET.Sdk.Web">

A complete clean and reopening the solution is required for changes to take effect.

For .NET Core 3.0 (I am using latest preview5), there is this warning when using KevM's workaround:

A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically. Otherwise, the PackageReference should be replaced with a FrameworkReference

Simply change it to

<FrameworkReference Include="Microsoft.AspNetCore.App" />

seems to be working fine.

Thanks for contacting us. Please follow the guidance provided in @KevM's response.

Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

So it looks like this problem of test assemblies not correctly resolving the dependencies of the assemblies being tested ended up never being fixed for the entire lifetime of Dotnet Core 1 and 2. Is it also going to remain unfixed for the entire lifetime of Dotnet Core 3 too or is there the prospect of this ever getting a fix?

Hi @18549 .

It looks like you are posting on a closed issue!

We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include a GitHub repository with a repro project and detailed repro steps so that we can investigate the problem.

I don't have a repro of the original project, but in ASP.NET Core 2.2 using Microsoft.AspNetCore.Mvc.Testing required the test project to reference the Web SDK and to have an explicit package reference to Microsoft.AspNetCore.App that matched the one in your project. See https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2#test-app-prerequisites

In 3.0 there is no need for any of this.

There is no such problem in .net core 1.0 either.

If you are using TestHost directly there are several things you need to take into account to make in memory tests work properly in an app. See https://devblogs.microsoft.com/aspnet/testing-asp-net-core-mvc-web-apps-in-memory/ for some of the details.

See https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests for our recommended approach to testing your application in memory.

Hi,

I have also encountered this problem (and I can make it appear and disappear).
I spent the last hour trying to recreate this in a sample project, but unfortunately I did not succeed in recreating it.

In assembly A (netcore console project) I am starting a Task which uses a nested class from assembly B (netcore web app), thus it needs to static-import that class (using static xxx;). This gives the exception below.

Could not load file or assembly 'Microsoft.Extensions.Hosting.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
   at XXX.Program.<XXX>d__1.MoveNext() in C:\git\XXX\Program.cs:line 39
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at XXX.Program.XXX()

When moving the nested class outside of its nesting, and thus using a normal using, the exception goes away.

Perhaps this might point future readers in the right directions.

Was this page helpful?
0 / 5 - 0 ratings