我在网上看到了一些文章,描述了如何在 Windows 服务应用程序中自行托管 ASP.NET Web API(请参阅此处和此处)。我在 VB.NET 中编写了一些简单的代码,以便在服务启动时启动自主机,并在服务停止时停止它,如下所示:
Protected Overrides Sub OnStart(ByVal args() As String)
Try
' init a new self host configuration using the base address
_config = New HttpSelfHostConfiguration(New Uri("http://localhost:8080"))
' map the URLs into the config
MapRoutes(_config)
_server = New HttpSelfHostServer(_config)
' start the server and wait for requests
_server.OpenAsync()
Catch ex As Exception
Throw
End Try
End Sub
Protected Overrides Sub OnStop()
Try
_server.CloseAsync().Wait()
_server.Dispose()
Catch ex As Exception
Throw
End Try …Run Code Online (Sandbox Code Playgroud) 官方的MS文档说,如果我想在Linux上托管ASP.NET核心应用程序,则应该在其前面放置一个apache或nginx反向代理。但是,我找不到我应该这样做的任何理由。
我为什么要那样做?为什么不能仅在茶est上运行?为什么需要反向代理?