40

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

6
  • not sure but doesnt sound right.. are you sure you dont have some logic in the test which are probably doing some reflection on the assembly. Apr 27, 2017 at 23:44
  • 1
    @loneshark99, I found a workaround for now. See screenshot below for details. Again, this looks like a bug with TestServer
    – Judy007
    Apr 27, 2017 at 23:48
  • That's what I said in my comment :) Apr 27, 2017 at 23:49
  • 1
    You were close, I will give you that. :) The logic regarding finding the assemblies that have controllers (perhaps controller factory) appears to be encapsulated in the Microsoft.AspNetCore.TestHost assembly
    – Judy007
    Apr 27, 2017 at 23:57
  • By the way are u contributing to this project or just for learning Apr 28, 2017 at 0:00

4 Answers 4

52

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"))); 
4
  • 5
    Made my day. You should also mention the related issue on Github: github.com/aspnet/Mvc/issues/5992 Jun 6, 2017 at 1:26
  • 1
    You saved my life. Oct 6, 2017 at 11:30
  • Note that if you are registering your controllers in the container with AddControllersAsServices, the call to AddApplicationPart should happen first.
    – Stefaan
    Apr 6, 2018 at 12:09
  • 2
    You does not have to use Assembly.Load, the next snippet is working also. ServiceHookController is from assembly you want to test. services.AddMvc() .AddApplicationPart(typeof(ServiceHookController).Assembly);
    – Karel Kral
    Nov 14, 2018 at 19:34
34

For those of you experiencing this during a migration to netcoreapp3.0, I found the above answers work, but you can do this more cleanly by changing the reference in your .csproj file itself perhaps.

In my case I changed the first XML line

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

to

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

Credits to: https://github.com/aspnet/Mvc/issues/5992#issuecomment-395408983

4
  • 1
    Buut then I get a "Program does not contain a static 'Main' method suitable for an entry point", if I change that line in my Test project.
    – El Mac
    May 19, 2020 at 19:40
  • I think I did this on my *.Web.Tests project but I don't remember. Seems like the kind of class of issue where Clean Solution, followed by Rebuild Solution is warranted. May 20, 2020 at 23:50
  • 1
    This worked for me. New 3.1 application, not a migration. Sep 8, 2020 at 20:18
  • OMFG. I was looking for your answer for more than 2 days straight. Thank you very much! Apr 30, 2021 at 8:33
22

In addition to joey answer: There is no need to call Assembly.Load() to resolve this bug. You can use the code below. ServiceHookController is class from a separate project.


public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddApplicationPart(typeof(ServiceHookController).Assembly);
}
1
  • 2
    I like this slightly better. Thank you!
    – Sam
    Jan 11, 2019 at 1:08
15

If you follow the pre-requisites from the MS documentation the controllers from separate assemblies are loaded.

In my case adding Microsoft.AspNetCore.Mvc.Testing NuGet package fixed the issue and I don't need to call AddApplicationPart anymore.

1
  • 2
    Valid for net5. This should be an accepted answer. Jul 20, 2021 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.