我在Web上看到了一些描述如何在Windows服務應用程序中自主託管ASP.NET Web API的文章(請參閱here和here)。我已經寫了一些簡單的代碼在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的示例代碼,或指出我可能在代碼中做錯了什麼?
謝謝!
我正在本地系統帳戶下運行服務。 – 2012-08-08 13:57:30
@RichMiller除非你明確地授予它對URL的訪問權,否則這是行不通的。看到這個問題http://stackoverflow.com/questions/2583347/c-sharp-httplistener-without-using-netsh-to-register-a-uri – 2012-08-10 03:18:07
問題原來是我的GAC中安裝的舊程序集。我已經糾正了這個問題,並且我在一個以本地系統帳戶運行的服務中成功地自託管web api。 – 2012-08-16 18:05:43