2012-08-07 140 views
3

我在Web上看到了一些描述如何在Windows服务应用程序中自主托管ASP.NET Web API的文章(请参阅herehere)。我已经写了一些简单的代码在VB.NET服务启动时,开始自拍主机并在服务停止停止它,就像这样:在Windows服务应用程序中自行托管ASP.NET Web API的问题

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,显然开始就很好。然而,当我打网址的服务崩溃并离开在我的事件日志如下:

应用:WebAPISelfHostPOC.exe Framework版本:v4.0.30319 说明:该过程由于未处理的异常终止。 异常信息:System.Runtime.CallbackException堆栈:在 System.Runtime.Fx + AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult) 在System.Net.LazyAsyncResult.Complete(IntPtr的)在 System.Net.LazyAsyncResult.ProtectedInvokeCallback(系统.Object, IntPtr的)在System.Net.ListenerAsyncResult.WaitCallback(UInt32的, UInt32的,System.Threading.NativeOverlapped *)在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32的, UInt32的,System.Threading.NativeOverlapped *)

and

错误的应用程序名:WebAPISelfHostPOC.exe,版本:1.0.0.0, 时间戳:0x50217b41错误模块名称:KERNELBASE.dll,版本: 6.1.7601.17651,时间戳:0x4e211319异常代码:0xe0434352故障偏移:0x0000b9bc出错进程ID:0x2b58断层 应用程序启动时间:0x01cd74dbf3f6b8a5错误的应用程序路径: C:\ Gravic \开发\ WebAPISelfHostPOC \ WebAPISelfHostPOC \ BIN \调试\ WebAPISelfHostPOC.exe 错误模块路径:C:\ WINDOWS \ SysWow64资料\ KERNELBASE.dll Report Id: 3e81d995-e0cf-11e1-b8b3-f80f41109bb9

任何人都可以指向我在Windows服务中运行Web API的示例代码,或指出我可能在代码中做错了什么?

谢谢!

回答

0

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

+0

我正在本地系统帐户下运行服务。 – 2012-08-08 13:57:30

+0

@RichMiller除非你明确地授予它对URL的访问权,否则这是行不通的。看到这个问题http://stackoverflow.com/questions/2583347/c-sharp-httplistener-without-using-netsh-to-register-a-uri – 2012-08-10 03:18:07

+0

问题原来是我的GAC中安装的旧程序集。我已经纠正了这个问题,并且我在一个以本地系统帐户运行的服务中成功地自托管web api。 – 2012-08-16 18:05:43

2

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

相关问题