aspnetcore: Assigning IHostBuilder.Build().GetTestServer() to TestServer throws System.InvalidCastException

If you believe you have an issue that affects the security of the platform please do NOT create an issue and instead email your issue details to secure@microsoft.com. Your report may be eligible for our bug bounty but ONLY if it is reported through email.

Describe the bug

A clear and concise description of what the bug is. Since WebHostBuilder is going to be deprecated and there is no document to describe it’s replacement for building TestServer, I am trying the following to build TestServer for my integration tests:

var hostbuilder = Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<MyStartup>().ConfigureServices(ConfigureServices));

//_server = new TestServer(hostbuilder); ASP.Net Core 2.2 code!
_server = hostbuilder.Build().GetTestServer(); // XXX: Hack for ASP.Net Core 3.0

and it throws runtime exception:

   System.InvalidCastException : Unable to cast object of type 'Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer' to type 'Microsoft.AspNetCore.TestHost.TestServer'.

https://github.com/aspnet/AspNetCore.Docs/issues/14962

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core ‘…’ 3.0
  2. Run this code ‘…’
  3. With these arguments ‘…’
  4. See error

Expected behavior

A clear and concise description of what you expected to happen. Expect IHostBuilder.Build().GetTestServer() to return TestServer object for integration tests.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here. Include the output of dotnet --info

$ dn --info
.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  19.04
 OS Platform: Linux
 RID:         ubuntu.19.04-x64
 Base Path:   /usr/share/dotnet-3.0.100/sdk/3.0.100/

Host (useful for support):
  Version: 3.0.0
  Commit:  7d57652f33

.NET Core SDKs installed:
  3.0.100 [/usr/share/dotnet-3.0.100/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.0.0 [/usr/share/dotnet-3.0.100/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/share/dotnet-3.0.100/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

When using HostBuilder you need to add the TestServer a different way: https://github.com/aspnet/AspNetCore/blob/cf16b4b53847fc4aa54c0484cac53de07afd4c8d/src/Hosting/TestHost/test/TestServerTests.cs#L53-L62

Or use WebApplicationFactory.

If you want to use HostBuilder, you can also extend WebApplicationFactory and create a IHostBuilder that suits your needs.

public class YourApplicationFactory : WebApplicationFactory<YourApplicationFactory>
{
    protected override IHostBuilder CreateHostBuilder()
    {
        return Host.CreateDefaultBuilder()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                // use whatever config you want here
                webBuilder.UseStartup<Startup>();
            });
    }
}
var factory = new YourApplicationFactory ();
TestServer testServer = factory.Server;