我在网络上看到了一些文章,描述了如何在 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
End Sub
#End Region
Private Sub MapRoutes(ByVal config As HttpSelfHostConfiguration)
' add route mappings
With config.Routes
.MapHttpRoute("API Default", "api/{controller}/{id}", New With {.id = RouteParameter.Optional})
End With
End Sub
我的简单控制器如下所示:
Public Class ClassesController
Inherits ApiController
Private _classes As List(Of [Class]) = New List(Of [Class])()
Public Sub New()
With _classes
.Add(New [Class]() With {.ID = 1, .Name = "Geometry"})
.Add(New [Class]() With {.ID = 2, .Name = "English 101"})
.Add(New [Class]() With {.ID = 3, .Name = "Psychology 101"})
.Add(New [Class]() With {.ID = 4, .Name = "Chemistry 101"})
.Add(New [Class]() With {.ID = 5, .Name = "Physical Education"})
.Add(New [Class]() With {.ID = 6, .Name = "Study Hall"})
.Add(New [Class]() With {.ID = 7, .Name = "Wood Shop"})
End With
End Sub
<HttpGet()>
Public Function GetAll() As HttpResponseMessage
Dim resp As HttpResponseMessage = Request.CreateResponse(Of List(Of [Class]))(HttpStatusCode.OK, _classes)
Return resp
End Function
<HttpGet()>
Public Function GetOne(ByVal id As Integer) As HttpResponseMessage
Dim theClass As [Class] = (From c As [Class] In _classes
Where c.ID = id
Select c).FirstOrDefault()
If theClass Is Nothing Then
Return Request.CreateResponse(Of String)(HttpStatusCode.NotFound, "The requested class could not be found.")
End If
Return Request.CreateResponse(Of [Class])(HttpStatusCode.OK, theClass)
End Function
End Class
该服务编译没有问题,使用 installutil 安装,显然启动得很好。但是,当我点击 URL 时,服务崩溃并在我的事件日志中留下以下内容:
应用程序:WebAPISelfHostPOC.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。异常信息:System.Runtime.CallbackException 堆栈:在 System.Net.LazyAsyncResult.Complete(IntPtr) 的 System.Runtime.Fx+AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult) 在 System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr ) 在 System.Net.ListenerAsyncResult.WaitCallback(UInt32, UInt32, System.Threading.NativeOverlapped*) 在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
和
错误应用程序名称:WebAPISelfHostPOC.exe,版本:1.0.0.0,时间戳:0x50217b41 错误模块名称:KERNELBASE.dll,版本:6.1.7601.17651,时间戳:0x4e211319 异常代码:0xe0434352 错误偏移:0x0000b9bc 错误进程 id:0x2b58应用程序启动时间:0x01cd74dbf3f6b8a5 错误应用程序路径:C:\Gravic\Development\WebAPISelfHostPOC\WebAPISelfHostPOC\bin\Debug\WebAPISelfHostPOC.exe 错误模块路径:C:\Windows\syswow64\KERNELBASE.dll 报告 ID:3e81d995-e0cf-11e1- b8b3-f80f41109bb9
谁能指出我在 Windows 服务中运行 Web API 的一些示例代码,或者指出我在代码中可能做错了什么?
谢谢!