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

HTTP 404 with ASP.NET Core Test Server when controllers are added to a seperate assembly #5992

Closed
JamieKeeling opened this issue Mar 20, 2017 · 25 comments

Comments

@JamieKeeling
Copy link

I have a basic test controller within my ASP.NET Core application:

using Microsoft.AspNetCore.Mvc;

namespace Web.Controllers
{
    [Route("api/[controller]")]
    public class TestController : Controller
    {
        [HttpGet]
        public IActionResult Test()
        {
            return Ok();
        }
    }
}

This is stored within my VS solution as follows:

image

With this configuration I receive a HTTP 200 when attempting to access TestController via localhost, and my integration test passes using the following scenario:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using System.Net.Http;
using System.Threading.Tasks;
using Web;
using Xunit;
using System.Net;

namespace IntegrationTests.Controllers
{
    public class CustomerControllerTests
    {
        private readonly TestServer _server;
        private readonly HttpClient _client;

        public CustomerControllerTests()
        {
            _server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
            _client = _server.CreateClient();
        }

        [Fact]
        public async Task HttpPost_TestController_ReturnsSuccess()
        {
            var response = await _client.GetAsync("/api/test");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
    }
}

If I move the same controller to a separate .NET Standard based library in the following structure:

image

In addition to ensuring my 'Web' project references the given library:

image

Then the same integration test referenced above fails, however the localhost test returns a HTTP 200 as expected.

I would expect the seperation of the TestController into a seperate binary to work against the TestServer too - have I missed a manual step?

For reference there's a reproducable example here - https://github.com/JamieKeeling/HelloWorldCore/tree/ControllerBinary

@JamieKeeling JamieKeeling changed the title HTTP 404 after controllers are added to a seperate assembly HTTP 404 with ASP.NET Core Test Server when controllers are added to a seperate assembly Mar 20, 2017
@jstallm
Copy link

jstallm commented Apr 27, 2017

I reproduced this. Looks like it is not specific to ASP.NET Core web app referencing .NET Standard. I was able to reproduce this when creating a separate .NET Core shared library as well for the controllers. Web application responds when it is hosted in IIS, but when we bypass IIS hosting and instead use TestServer, it appears to not be able to find controller actions

@jstallm
Copy link

jstallm commented Apr 27, 2017

found workaround for now. see http://stackoverflow.com/questions/43669633/why-does-testserver-not-able-to-find-controllers-when-controller-is-in-separate?noredirect=1#comment74386164_43669633

@marcwittke
Copy link

For the records: the app running in Kestrel is fine. It just broke my integration tests when moving some controllers to a separate assembly.

@joshmouch
Copy link

joshmouch commented Jun 23, 2017

I am seeing this same problem. The workaround of using the AddApplicationPart extension works fine, but it has me worried about what else may be different between the TestServer and Kestrel.

@rynowak
Copy link
Member

rynowak commented Jun 23, 2017

To fix this you'd have to do something like what we do here https://github.com/aspnet/Mvc/blob/dev/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs#L61

We're currently working on productizing our functional testing infrastructure so it's a reusable package #6433

@rynowak
Copy link
Member

rynowak commented Jul 17, 2017

To everyone posting here, we're releasing a preview build as part of 2.0.0 of a new testing package to add the features needed to test MVC with TestServer.

This is basically a friendlier version of what the team has been using to test MVC for the past few years.

See: https://github.com/aspnet/Mvc/tree/dev/src/Microsoft.AspNetCore.Mvc.Testing
Example Usage: https://github.com/aspnet/Mvc/blob/dev/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs

I'm closing this issue because we think that this resolves all of the issues that have been reported about difficulties of testing with TestServer. If you have a unique issue, or your problem isn't resolve by this, or you need more help, please open a new issue. Thanks!

@rynowak rynowak closed this as completed Jul 17, 2017
@johnkors
Copy link

What name does this have now, @rynowak ? The Microsoft.AspNetCore.Mvc.Testing is unlisted.

@pranavkm
Copy link
Contributor

@johnkors the package will be released as part of the 2.1.0 release. The unlisted version is there to reserve the package id on nuget.org.

@johnkors
Copy link

johnkors commented Dec 20, 2017

Gotcha. Found it on MyGet as
2.1.0:
https://dotnet.myget.org/feed/aspnetcore-dev/package/nuget/Microsoft.AspNetCore.Mvc.Testing
2.0.0:
https://dotnet.myget.org/feed/aspnetcore-release/package/nuget/Microsoft.AspNetCore.Mvc.Testing

@mathysd
Copy link

mathysd commented Jan 18, 2018

Microsoft.AspNetCore.Mvc.Testing should this package be available on nuget or is it part of a other package?

i'm only able to find this one:
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Testing/

@johnkors
Copy link

Check my comment directly above yours, @mathysd :) It's in the myget feeds.

@zakar1ya
Copy link

To use controllers in a separate assembly, i have to use a package from myget or add application part in startup, did I understand correctly? Is there no other way?

@zakar1ya
Copy link

@wpitallo
Copy link

This issue should still be open? @johnkors tried to install it but was failing on dependencies

@johnkors
Copy link

Installing works OK, so if you're not able to install - that sounds seperate to the issue at hand here @wpitallo .

  • Check you've added the myget feed to your nuget.config.
  • If you're using VS, please restart VS after modifying nuget.config. It seems to have some caching that a restart of VS resolves.

@alexsandro-xpt
Copy link

How can I use this Microsoft.AspNetCore.Testing package?
I'm install it from myget normaly at my test project.

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="2.1.0-preview1-28285" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />

But and now?

@alexsandro-xpt
Copy link

And now to make matters worse, after some days, it begin works on my computer but in another my notebook, not!

Really stranger.......

@rducom
Copy link

rducom commented Jun 5, 2018

After migrating on 2.1 (aspnet nuget + target necore2.1), I found the same issue.
I have to .AddApplicationPart(typeof(MyTestController).Assembly);
In my case, Startup, TestServer and the TestController are in the same assembly (in the same .cs in fact ^^)

@pranavkm
Copy link
Contributor

pranavkm commented Jun 5, 2018

@rducom could you file a new issue?

@dzimchuk
Copy link

dzimchuk commented Jun 7, 2018

Same issue here after updating to 2.1. I was able to fix that by changing test projects' SDK to Microsoft.NET.Sdk.Web. Strange it used to work in 2.0 with Microsoft.NET.Sdk.

@pranavkm
Copy link
Contributor

pranavkm commented Jun 7, 2018

Same issue here after updating to 2.1. I was able to fix that by changing test projects' SDK to Microsoft.NET.Sdk.Web. Strange it used to work in 2.0 with Microsoft.NET.Sdk.

@dzimchuk the fix you made looks correct. @danroth27 did we add this requirement to the 2.1 announcement post?

@alexsandro-xpt
Copy link

alexsandro-xpt commented Jun 8, 2018

@rducom and @dzimchuk I update to 2.1, and migrate my tests to IClassFixture<WebApplicationFactory<Startup> and everything is working fine.

@vikasillumina
Copy link

vikasillumina commented Aug 8, 2018

@rynowak I am still seeing this issue with latest dotnet core 2.1. Here is the screenshot of the nuget package to show the version of TestServer library I am using
image

I tried the workaround mentioned above which was to register the Assembly of the controller in my api project
services.AddMvc().AddApplicationPart(typeof(MyController).Assembly);
and it worked. But my concern is why I need to do this?
I have 2 services done differently one has API host and controller in the same project and other one with separate projects. The service with separate project exhibit the issue of returning 404.
What I am missing?

@rynowak
Copy link
Member

rynowak commented Aug 8, 2018

Hi, it looks like you are posting on a closed issue/PR/commit!

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 detailed repro steps so that we can investigate the problem

Thanks!

@vikasillumina
Copy link

@rynowak Thanks for your reply, sorry I didn't notice the issue was already closed. I submitted this issue:
#8238

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