Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is TestServer not able to find controllers when controller is in separate assembly for asp.net core app?

For some reason, when ASP.NET core controllers are created in separate assembly, the TestServer is not able to find controller actions when the client makes a request.(results in 404 response) Why is this? How can I work around it? Here are steps to reproduce.

  1. Create new ASP.NET Core WebAPI using .NET core
  2. Create integration tests in separate project and configure the test to use TestServer() client and get tests to work successfully.
  3. Now, separate the controller into its own shared library and refactor project created in step 1 to use this shared library instead.
  4. Re-run test which contains the TestServer() class. You'll notice now it fails.

See the follwing link for creating the integration tests. Integration testing w/ ASP.NET Core

like image 535
jbooker Avatar asked Apr 27 '17 23:04

jbooker


People also ask

What is the controller responsible for in an ASP NET MVC application?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

What is the difference between controller and ControllerBase?

ControllerBase class Web API controllers should typically derive from ControllerBase rather from Controller. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. If the same controller must support views and web APIs, derive from Controller .

How do I add a controller in Visual Studio?

Using the Add Controller Menu Option The easiest way to create a new controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the Add, Controller menu option (see Figure 1). Selecting this menu option opens the Add Controller dialog (see Figure 2).


1 Answers

Actually I found a solution for now, see diff below: enter image description here

It sounds like this may be bug of TestServer() class and how it is hosting the application during the test run.

Here is the line of code in case you cannot read above in image

.AddApplicationPart(Assembly.Load(new AssemblyName("WebApiToReproduceBug.Controllers")));  
like image 94
jbooker Avatar answered Oct 13 '22 02:10

jbooker