1

I have the following call to a selfhosted OWIN API:

const string baseAddress = "http://localhost:9000/";
using (WebApp.Start<ApiSelfHost>(url: baseAddress))
{
    var client = new HttpClient();

    var response = client.GetAsync(baseAddress + "api/v1/orders/test/status").Result;
}

Which is based on (I got that working):

Use OWIN to Self-Host ASP.NET Web API

The response returns the following:

StatusCode: 404, ReasonPhrase: 'Not Found'

But if I run the actual API and go to:

http://localhost:51823/api/v1/orders/test/status

I get a valid response.

That controller action has this signature:

[HttpGet]
[Route("api/v1/orders/{orderReference}/status")]
public IHttpActionResult GetOrderStatus([FromUri]string orderReference)

I tried changing that port 9000 to 51823, just to be sure, but that doesn't matter.

I have the following OWIN packages installed:

<package id="Microsoft.Owin" version="4.0.1" targetFramework="net471" />
<package id="Microsoft.Owin.Host.HttpListener" version="4.0.1" targetFramework="net471" />
<package id="Microsoft.Owin.Hosting" version="4.0.1" targetFramework="net471" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net471" />
<package id="Microsoft.Owin.Security.Jwt" version="3.0.1" targetFramework="net471" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net471" />
<package id="Microsoft.Owin.Testing" version="4.0.1" targetFramework="net471" />

Question

Any pointers to why the url cannot be found (throws 404)?

Spikee
  • 3,967
  • 7
  • 35
  • 68

1 Answers1

2

A few hours later I got it working! As was to be expected it was something deeper:

My controller gave this error:

no parameterless constructor defined for this object

Despite having set up IoC type registrations:

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

I was however missing this line:

source.RegisterApiControllers(typeof(OrderController).Assembly);

Full startup set up with injection:

public class ApiSelfHost : Startup
{
    public override 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);
    }
}

Where ConfigureAutofac is an extension on HttpConfiguration:

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

    source.ConfigureDependencyInjection(container);

    return container;
}

And one on ContainerBuilder as well:

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

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

    return source;
}

Startup base class is the actual one from the API.

Spikee
  • 3,967
  • 7
  • 35
  • 68