Windows 服务应用程序中自托管 ASP.NET Web API 的问题

Ric*_*ler 5 vb.net asp.net-web-api

我在网上看到了一些文章,描述了如何在 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
Run Code Online (Sandbox Code Playgroud)

我的简单控制器如下所示:

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
Run Code Online (Sandbox Code Playgroud)

该服务编译没有问题,使用 installutil 安装,并且显然启动得很好。但是,当我点击该 URL 时,服务崩溃并在事件日志中留下以下内容:

应用程序:WebAPISelfHostPOC.exe 框架版本:v4.0.30319 描述:由于未处理的异常,进程被终止。异常信息:System.Runtime.CallbackException堆栈:在System.Runtime.Fx + AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult)在System.Net.LazyAsyncResult.Complete(IntPtr)在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:0x2b 58 错误应用程序启动时间:0x01cd74dbf3f6b8a5 错误应用程序路径:C:\Gravic\Development\WebAPISelfHostPOC\WebAPISelfHostPOC\bin\Debug\WebAPISelfHostPOC.exe 错误模块路径:C:\Windows\syswow64\KERNELBASE.dll 报告 ID:3e81d995-e0cf-11e1- b8b3-f80f41109bb9

任何人都可以向我指出一些在 Windows 服务中运行 Web API 的示例代码,或者指出我在代码中可能做错的任何事情吗?

谢谢!

Dar*_*ler 0

您运行该服务的帐户是本地管理员吗?或者您是否分配了允许访问该网址的权限?