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

MissingMethodException when referencing Microsoft.AspNetCore.TestHost in a .NET Core project that targets net461 #926

Closed
dfaivre opened this issue Feb 1, 2017 · 18 comments

Comments

@dfaivre
Copy link

dfaivre commented Feb 1, 2017

Cross posting the issue from here: dotnet/msbuild#1649

Reproduction code here: https://github.com/dfaivre/bugrepro_dotnetcore_missingmethod

It is only my best guess that this is an issue with the package. Maybe it's a NuGet issue? Or possibly an issue with the new MSBuild csproj format? Sorry if I'm wasting your time.

Summary
Using VS2016 RC, when referencing Microsoft.AspNetCore.TestHost (https://www.nuget.org/packages/Microsoft.AspNetCore.TestHost/) nuget package in a .NET Core project that targets net461, a MissingMethodException
is thrown when trying to create an HttpClient:

Error Message:
 System.MissingMethodException : 
   Method not found: 'System.Net.Http.HttpClient Microsoft.AspNetCore.TestHost.TestServer.CreateClient()'.

It is also thrown when creating a "classic" .NET Framework class library and using PackageReferences for NuGet.

The exception is not thrown when targeting netcoreapp1.0, or when creating a "classic" .NET Framework class library using packages.config for NuGet.

System Info

Microsoft Visual Studio Professional 2017 RC
Version 15.0.26127.0 D15REL
Microsoft .NET Framework
Version 4.6.01586

Installed Version: Professional

ASP.NET and Web Tools 2016   15.0.30124.0
ASP.NET and Web Tools 2016

ASP.NET Web Frameworks and Tools 2013   5.2.50105.0
For additional information, visit https://www.asp.net/

NuGet Package Manager   4.0.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.
@Tratcher
Copy link
Member

Tratcher commented Feb 1, 2017

Possibly related to aspnet/Security#1046. @anurse

Or some of the ongoing issues with the System.Net.Http packages...

@analogrelay
Copy link
Contributor

I'm seeing that this only repros when in a unit test project, not within a standalone console app. @dfaivre are you able to confirm that? It could be something related to the Test SDK in that case...

@analogrelay
Copy link
Contributor

It looks like the unit test runner is causing the binding redirects to be lost (because the actual executable is testhost.exe, so the unit test project doesn't generate binding redirects). Filed microsoft/vstest#428 to track that. That's why it seems to work in a standalone app.

@dfaivre
Copy link
Author

dfaivre commented Feb 1, 2017

Thanks for looking into it @anurse. I can confirm it doesn't throw in the stand alone app. Updated the repro project with a console example.

@dfaivre
Copy link
Author

dfaivre commented Feb 1, 2017

If anyone stumbles on this and wants a work around, I just copied all the code files from https://github.com/aspnet/Hosting/tree/dev/src/Microsoft.AspNetCore.TestHost into my unit test project.

@analogrelay
Copy link
Contributor

@dfaivre Can you try the following workaround?

  1. Remove any TestHost files you've added and go back to referencing it as a package
  2. Add the following to the <PropertyGroup> at the top of your csproj file:
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

Then try running tests again. That got it working for me when running dotnet test. I'm having an issue running tests in VS but I think that may be a different issue.

dfaivre added a commit to dfaivre/bugrepro_dotnetcore_missingmethod that referenced this issue Feb 2, 2017
@dfaivre
Copy link
Author

dfaivre commented Feb 2, 2017

@anurse Yep, that works for me too when running dotnet test. I can't even get the test to show up in Test Explorer though, but I've found it to be a little flaky so far with .NET Core xUnit projects targeting the full framework.

Updated the repro project.

@muratg
Copy link

muratg commented Feb 3, 2017

External: microsoft/vstest#428

@muratg muratg closed this as completed Feb 3, 2017
StefanOssendorf added a commit to StefanOssendorf/SecurityHeadersMiddleware that referenced this issue Mar 13, 2017
- With kind help of giorgiog in slack. Solution: aspnet/Hosting#926
@giggio
Copy link

giggio commented Mar 22, 2017

Same problem happened with xUnit. Adding GenerateBindingRedirectsOutputType and AutoGenerateBindingRedirects solved it. I needed both of them.

@fabiomaulo
Copy link

Working workaround for NUnit OutOfMemory and this issue: http://bit.ly/2q8jbch .
Thanks guys.

olivier-spinelli added a commit to Invenietis/CK-AspNet-Auth that referenced this issue Jun 14, 2017
@octocat-mona
Copy link

I've added the elements in the <PropertyGroup> like mentioned by @anurse, but my tests (including running via CLI) are now failing with error:

Message: System.Net.Http.HttpRequestException : Response status code does not indicate success: 404 (Not Found).

After a long investigation I found out that it makes a difference whether the (custom) Startup.cs is in the main .NET Core project or in the test project. The tests only succeed when the Startup class is located in the main project, it doesn't make a difference that I used .UseStartup<TestStartup>() which does get called correctly on runtime.

Is this related, any thoughts on how to fix this?

grecosoft added a commit to grecosoft/NetFusion that referenced this issue Aug 31, 2017
This is just until the version issue with the asp.net core testing assembly is addressed:
aspnet/Hosting#926
@memark
Copy link

memark commented Sep 11, 2017

@KPStolk THANK YOU! That was immensely helpful to me!

@pamanes
Copy link

pamanes commented Sep 29, 2017

I am getting the same error in VS2017 15.3.5 with ASPNETCore MVC project (4.6.2) and a unit test project that has the following packages installed:

Microsoft.AspNetCore.TestHost -Version 2.0.0
xunit -Version 2.2.0
xunit.runner.visualstudio -Version 2.3.0-beta5-build3769

I already added the property groups to the unit test project and I now get this:

Message: System.MissingMethodException : Method not found: 'Microsoft.Net.Http.Headers.MediaTypeHeaderValue Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse(System.String)'.

when it runs the following line of code:

            var server = new TestServer(new WebHostBuilder()
                .UseStartup<Startup>()
                .UseContentRoot("XYZ"));

@Tratcher
Copy link
Member

@pamanes make sure all of your dependencies are updated to 2.0. Fee free to share your project file.

@pamanes
Copy link

pamanes commented Oct 2, 2017

This is what my web project looks like:

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

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

</Project>

@Tratcher Just to be clear on what I am doing:

I have a solution that contains an ASP.NET Core Web Application where I chose .NET Framework (4.6.1) on web template and then .NET Framework and ASP.NET Core 2.0 on the next wizard. Then a full .net framework 4.6.1 unit test project. with the packages I mentioned before. and I now get the following when running client.GetAsync("/"):

Message: Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilation failures occurred:
oyknulbi.ehq(4,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(4,81): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(4,110): error CS0518: Predefined type 'System.Type' is not defined or imported
oyknulbi.ehq(4,11): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(8,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
oyknulbi.ehq(9,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
oyknulbi.ehq(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
oyknulbi.ehq(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
oyknulbi.ehq(14,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
oyknulbi.ehq(25,94): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
oyknulbi.ehq(25,94): error CS0518: Predefined type 'System.Boolean' is not defined or imported
oyknulbi.ehq(25,45): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(28,67): error CS1983: The return type of an async method must be void, Task or Task<T>
oyknulbi.ehq(28,39): error CS0400: The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?)
oyknulbi.ehq(152,49): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
oyknulbi.ehq(152,118): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(154,49): error CS0234: The type or namespace name 'IUrlHelper' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
oyknulbi.ehq(154,71): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(156,49): error CS0234: The type or namespace name 'IViewComponentHelper' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
oyknulbi.ehq(156,87): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(158,59): error CS0234: The type or namespace name 'IJsonHelper' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Rendering' (are you missing an assembly reference?)
oyknulbi.ehq(158,83): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(160,59): error CS0234: The type or namespace name 'IHtmlHelper<>' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Rendering' (are you missing an assembly reference?)
oyknulbi.ehq(160,71): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(160,71): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(160,71): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
oyknulbi.ehq(160,71): error CS0518: Predefined type 'System.Boolean' is not defined or imported
oyknulbi.ehq(160,92): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(28,67): error CS0508: '_Views_Home_Index_cshtml.ExecuteAsync()': return type must be 'Task' to match overridden member 'RazorPageBase.ExecuteAsync()'
oyknulbi.ehq(28,67): error CS0012: The type 'Task' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(151,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(151,10): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(153,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(153,10): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(155,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(155,10): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(157,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(157,10): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(159,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(159,10): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(36,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(36,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(36,26): error CS0518: Predefined type 'System.Int32' is not defined or imported
oyknulbi.ehq(36,30): error CS0518: Predefined type 'System.Int32' is not defined or imported
oyknulbi.ehq(36,36): error CS0518: Predefined type 'System.Boolean' is not defined or imported
oyknulbi.ehq(36,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(37,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(37,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(37,26): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(37,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(59,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(59,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(59,26): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(59,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(81,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(81,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(81,26): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(81,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(104,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(104,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(104,26): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(104,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(121,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(121,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(121,26): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(121,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(135,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(135,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(135,26): error CS0518: Predefined type 'System.String' is not defined or imported
oyknulbi.ehq(135,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(148,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oyknulbi.ehq(148,13): error CS0518: Predefined type 'System.Object' is not defined or imported
oyknulbi.ehq(148,13): error CS0518: Predefined type 'System.Void' is not defined or imported
oyknulbi.ehq(28,67): error CS0161: '_Views_Home_Index_cshtml.ExecuteAsync()': not all code paths return a value
oyknulbi.ehq(25,18): error CS0518: Predefined type 'System.Void' is not defined or imported
C:\Users\alepal\Documents\Visual Studio 2017\Projects\CapExRSLN\CapExR.UIWeb\Views\Home\Index.cshtml(2,5): error CS0012: The type 'ViewDataDictionary<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
C:\Users\alepal\Documents\Visual Studio 2017\Projects\CapExRSLN\CapExR.UIWeb\Views\Home\Index.cshtml(2,14): error CS0518: Predefined type 'System.String' is not defined or imported
C:\Users\alepal\Documents\Visual Studio 2017\Projects\CapExRSLN\CapExR.UIWeb\Views\Home\Index.cshtml(2,25): error CS0518: Predefined type 'System.String' is not defined or imported

@jgalentine007
Copy link

jgalentine007 commented Oct 29, 2017

I have a .NET standard 2.0 library that I was trying to test with an xUnit .Net Framework 4.7 project. I was getting a MissingMethodException error when using Task<HttpResponseMessage>. The library works fine when being run from a regular .Net Framework 4.7 project. I tried the solutions here and they did not work until I changed to a xUnit .Net Core project and now my tests run fine. Doesn't perfectly address the issue but works for me in my case, hope it helps someone else.

@seccijr
Copy link

seccijr commented Nov 5, 2017

For those receiving the error System.AggregateException : One or more errors occurred. (Method not found: 'Microsoft.Net.Http.Headers.MediaTypeHeaderValue Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse(System.String)' when running Test project please add explicitly reference to Microsoft.AspNetCore.Mvc and Microsoft.AspNetCore.StaticFiles to your dependencies:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
....
  </ItemGroup>
</Project>

Do it in your csproj file of your Test project.

@dorlandode
Copy link

@seccijr your solution worked for me. Thanks!

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

No branches or pull requests