2

TL;DR: How do I configure OWIN Web API 1 via a StartUp class or alternatively use the HttpSelfHostServer with the static files extension?

I have a working self-hosted OWIN Web API 2 project, which I'd like to downgrade to .Net 4.0 - I'd prefer not to install the .Net 4.5+ framework replacement, because of unknown side-effects on a production machine.

I can't use Microsoft.AspNet.WebApi.OwinSelfHost because it's not available for .Net 4.0 and therefore I can't call the IAppBuilder.useWebApi() extension method for registering my configuration.

I'm bootstraping the server now like this: WebApp.Start(Of StartUp)(url:="...")
and the StartUp class looks like this, but I can't set the config:

Public Class StartUp
Public Sub Configuration(app As IAppBuilder)
    ' the host should be obsolete, as it's already configured in the app context
    Dim config As New HttpSelfHostConfiguration("http://localhost:9000")

    ' config.MapHttpAttributeRoutes()
    config.Routes.MapHttpRoute(
        name:="DefaultApi",
        routeTemplate:="api/{controller}/{id}",
        defaults:=New With {.id = RouteParameter.Optional}
    )

    ' configure formatters, converters, filters ...

    ' app.UseWebApi(config)

    Dim physicalFileSystem As New PhysicalFileSystem("...\static")
    app.UseDefaultFiles(New DefaultFilesOptions With {.FileSystem = physicalFileSystem})
    app.UseStaticFiles(New StaticFileOptions With {.FileSystem = physicalFileSystem, .ServeUnknownFileTypes = True})
End Sub
End Class

If I'd use the documented Web API 1 approach of bootstrapping the server (see below), I don't know how to configure the static files extension:

var config = new HttpSelfHostConfiguration("http://localhost:8080");

config.Routes.MapHttpRoute(
    "API Default", "api/{controller}/{id}", 
    new { id = RouteParameter.Optional });

using (HttpSelfHostServer server = new HttpSelfHostServer(config)) {
    server.OpenAsync().Wait();
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
}

Although I've checked the AspNetWebStack sources, I think the message handling has changed between V2.1.0 and V5.0.0+ and therefore I can't apply that approach.

I'm using the following nuget packages - which I might reduce after its working:

<packages>
  <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.SelfHost" version="4.0.30506.0" targetFramework="net40" />
  <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.Owin" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.Diagnostics" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.FileSystems" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.Host.HttpListener" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.Hosting" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.SelfHost" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Owin.StaticFiles" version="2.1.0" targetFramework="net40" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="4.5.6" targetFramework="net40" />
  <package id="Owin" version="1.0" targetFramework="net40" />
</packages>

Btw. although I've used VB.Net in the example, feel free to use your preferred language in your answer.

kiwiwings
  • 3,386
  • 1
  • 21
  • 57
  • ... in the meantime, I was informed that actually .net 4.6 is installed on the machine, so this renders my approach obsolete ... :S – kiwiwings Jun 25 '19 at 07:09

0 Answers0