如何通过IAppBuilder配置OWIN Web API 1

时间:2019-06-21 09:02:27

标签: owin katana

TL; DR:如何通过StartUp类配置OWIN Web API 1,或者使用带有静态文件扩展名的HttpSelfHostServer?

我有一个正在运行的自托管OWIN Web API 2项目,我想将其降级到.Net 4.0-我不希望安装.Net 4.5+框架替代品,因为unknown side-effects on a production machine

我无法使用Microsoft.AspNet.WebApi.OwinSelfHost,因为它不适用于.Net 4.0,因此无法调用IAppBuilder.useWebApi()扩展方法来注册我的配置。

我现在正在像这样引导服务器:WebApp.Start(Of StartUp)(url:="...")
并且StartUp类如下所示,但我无法设置配置

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

如果我使用记录在案的Web API 1 approach来引导服务器(请参见下文),则不知道如何配置静态文件扩展名:

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();
}

尽管我已经检查过AspNetWebStack sources,但我认为消息处理在V2.1.0和V5.0.0 +之间已经发生了变化,因此我不能采用这种方法。

我正在使用以下nuget软件包-在工作后可能会减少它们:

<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>

顺便说一句。尽管我在示例中使用了VB.Net,但请随时在您的答案中使用您喜欢的语言。

0 个答案:

没有答案