【问题标题】:Problems Self-Hosting ASP.NET Web API in a Windows Service Application在 Windows 服务应用程序中自承载 ASP.NET Web API 的问题
【发布时间】:2012-08-04 22:56:31
【问题描述】:

我在网络上看到一些文章描述了如何在 Windows 服务应用程序中自托管 ASP.NET Web API(请参阅here 和here)。我在 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.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: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 的示例代码,或者指出我在代码中可能做错了什么?

谢谢!

【问题讨论】:

    标签: vb.net asp.net-web-api


    【解决方案1】:

    问题原来是安装在 GAC 中的一组旧程序集,这些程序集是 Web API 的测试版安装遗留下来的。我能够让我的项目引用新的程序集,并且问题得到了解决。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我在本地系统账户下运行服务。
      • @RichMiller 除非您明确授予它访问 URL 的权限,否则这是行不通的。看到这个问题stackoverflow.com/questions/2583347/…
      • 问题原来是安装在我的 GAC 中的旧程序集。我已经纠正了这个问题,并且我成功地在本地系统帐户下运行的服务中自托管了 web api。
      猜你喜欢
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多