The I/O operation has been aborted because of either a thread exit or an application request when running OWIN Selfhost test

0

Background

I have a unit test which configures an OWIN SelfHost for my REST API:

[Fact]

public void My_Test()
{
    const string baseAddress = "http://localhost:9000/";
    using (WebApp.Start<ApiSelfHost>(url: baseAddress))
    {
        var client = new HttpClient();
        var response = client.GetAsync(baseAddress + "api/test").Result;

        _output.WriteLine($"Status code: {response.StatusCode}");

        response.IsSuccessStatusCode.ShouldBeTrue();
    }
}

ApiSelfHost is set up like so:

public class ApiSelfHost
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);

        var httpConfiguration = new HttpConfiguration();

        var container = httpConfiguration.ConfigureAutofac();

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(httpConfiguration);

        app.UseWebApi(httpConfiguration);
    }
}

Autofac is set up like this:

internal static IContainer ConfigureAutofac(this HttpConfiguration source)
{
    var container = new ContainerBuilder()
        .ConfigureAutofac()
        .Build();

    source.ConfigureDependencyInjection(container);

    return container;
}

And

internal static ContainerBuilder ConfigureAutofac(this ContainerBuilder source)
{
    source.RegisterApiControllers(typeof(MyController).Assembly);

    source.Register(c => MyContextMock.Create())
        .AsImplementedInterfaces()
        .InstancePerRequest();

    return source;
}

The controller action is very simple, just a simple test to see if the controller is hit:

[HttpGet]
[Route("api/test")]
public IHttpActionResult RunTest()
{
    return Ok();
}

Sadly a breakpoint in that method is NOT hit.

Symptoms

If I run the test in Debug, I get this error exception:

System.Net.HttpListenerException HResult=0x80004005 Message=The I/O operation has been aborted because of either a thread exit or an application request Source=System StackTrace: at System.Net.HttpListener.EndGetContext(IAsyncResult asyncResult)

In a black page titled System.pdb not loaded.

The callstack is deep within mscorlib.ddl and I think not very meaningful to figuring out where the issue happens:

Callstack

If I run the test directly I get this weird result:

Test passes but fails?

Question

What is happening and what can I do to fix this?

UPDATE

I have some progress, I've tried to recreate the issue in an isolated project, that worked. I also found a way to fix that isolated project:

If I initialise my controller directly, like so:

var ctrl = new MyController();

It works, because the controller's assembly is actively brought up. However, I fail to make that work for the actual solution this question is about. I keep looking ...

c#
owin
autofac
xunit
self-host-webapi
asked on Stack Overflow May 23, 2019 by Spikee • edited May 23, 2019 by Spikee

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0