2014-07-17 45 views
1

我有一個託管WebAPI(HttpSelfHostServer)的控制檯應用程序。在Windows服務中託管WebAPI第一次嘗試時崩潰

在控制檯應用程序中,我有從SQL-Server查詢數據的控制器。

控制檯應用程序沒有問題(來自小提琴手和Windows_client) 現在我想在Windows服務中託管WebAPI(HttpSelfHostServer)。

因此,我創建了一個Service-Project並引用控制檯應用程序dll中的控制器,以便找到控制器。

爲了儘可能減少調試時間,我在Windows服務的事件日誌中寫入了一些信息。

我可以毫無問題地啓動和停止windows服務。 它也似乎是httpselfhost啓動和活動。

問題: - 通過第一次嘗試的URL,服務崩潰很難用:

System.NullReferenceException (eventlog-entry to .net RunTime) 

Anwendung: WS_MatrixGuide.exe 
Frameworkversion: v4.0.30319 
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet. 
Ausnahmeinformationen: **System.NullReferenceException** 
Stapel: 
    bei System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__5(System.Object) 
    bei System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object) 
    bei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    bei System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 
    bei System.Threading.ThreadPoolWorkQueue.Dispatch() 
    bei System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 

我已經嘗試了天解決問題,我也很絕望。

由於我無法調試服務,我不知道崩潰發生在哪裏。

我認爲,在Windows服務工作和主機的自主主機的啓動也是可訪問的(因爲它通過第一次訪問崩潰)。

所以..也許我做了完全錯誤的服務或我錯過了什麼重要..

由於控制檯應用程序的工作原理沒有問題,我在後從窗口服務下面只代碼: 代碼到Windows的服務:

Imports System 
Imports System.ServiceProcess 
Imports System.ServiceModel 
Imports KA_MatrixGuide.Controllers ' Verweis auf Controller in KA_MatrixGuide (Konsolenanwendung) erstellen 
Imports System.Net.Mail 
Imports System.Web.Http.SelfHost ' Self Hosting 
Imports System.Web.Http 
Imports System.IO 
Public Class WS_MatrixGuide 
    Private SH As ServiceHost 
    Public Sub New() 
     InitializeComponent() 
     Me.ServiceName = "WS_MatrixGuide" 
    End Sub 
    Protected Overrides Sub OnStart(ByVal args() As String) 
     EventLog.WriteEntry("Vor SH = new...", System.Diagnostics.EventLogEntryType.Information) 
     SH = New ServiceHost(GetType(WS_MatrixGuide)) 
     AddHandler SH.Faulted, AddressOf SH_Faulted 
     EventLog.WriteEntry("Vor Dim config...", System.Diagnostics.EventLogEntryType.Information) 
     Dim config As New HttpSelfHostConfiguration("http://127.0.0.1:21212") 
     EventLog.WriteEntry("Vor Config-Routes...", System.Diagnostics.EventLogEntryType.Information) 
     config.Routes.MapHttpRoute(_ 
      name:="DefaultApi", _ 
      routeTemplate:="api/{controller}/{id}", _ 
      defaults:=New With {.id = RouteParameter.Optional} _ 
     ) 
     'Tracing-Info's in Event-Log schreiben... 
     EventLog.WriteEntry("Vor Using Server...", System.Diagnostics.EventLogEntryType.Information) 
     Using server As New HttpSelfHostServer(config) 
      ' Objekte zu Controllern erstellen (Interface für Zugriff) 
      Dim SQL_C As Type = GetType(KA_MatrixGuide.Controllers.SQLController) 
      Dim Beauty_C As Type = GetType(KA_MatrixGuide.Controllers.BeautyController) 
      Dim Freizeit_C As Type = GetType(KA_MatrixGuide.Controllers.FreizeitController) 
      Dim Gourmet_C As Type = GetType(KA_MatrixGuide.Controllers.GourmetController) 
      Dim Konfiguration_C As Type = GetType(KA_MatrixGuide.Controllers.KonfigurationController) 
      Dim Empfehlungen_C As Type = GetType(KA_MatrixGuide.Controllers.EmpfehlungenController) 
      Try 
       EventLog.WriteEntry("Vor Server.OpenAsync...", System.Diagnostics.EventLogEntryType.Information) 
       server.OpenAsync().Wait() 
       EventLog.WriteEntry("Nach Server.OpenAsync...", System.Diagnostics.EventLogEntryType.Information) 
      Catch aggEx As AggregateException 
       EventLog.WriteEntry("Fehler beim Laden des Servers (httpSelfHostServer)...", System.Diagnostics.EventLogEntryType.Information) 
      End Try 
      'Console.WriteLine() 
      'Console.WriteLine("Enter-Taste drücken, um die Konsolen-Anwendung zu beenden.") 
      'Console.ReadLine() 
      'AddHandler server.Faulted, AddressOf Server_Faulted ' => es gibt kein Server.Faulted 
     End Using 
     'EventLog.WriteEntry("WS_MatrixGuide wurde gestartet", System.Diagnostics.EventLogEntryType.Information) 
     EventLog.WriteEntry("Vor SH.Open...", System.Diagnostics.EventLogEntryType.Information) 
     SH.Open() 
     EventLog.WriteEntry("Nach SH.Open...", System.Diagnostics.EventLogEntryType.Information) 
    End Sub 
    Protected Overrides Sub OnStop() 
     Dim Infomeldung As String = "WS_MatrixGuide wurde gestoppt" 
     EventLog.WriteEntry(Infomeldung, System.Diagnostics.EventLogEntryType.Information) 
     SH.Close() 
     SH = Nothing 
    End Sub 
    Private Sub SH_Faulted() ' Eigener Fehlerhandler 
     Dim Fehlermeldung As String = "Fehler bei Ausführung von WS_MatrixGuide. Service wurde gestoppt" 
     EventLog.WriteEntry(Fehlermeldung, System.Diagnostics.EventLogEntryType.Error) 
     Dim cMsg As String = "Fehler bei der Ausführung von WS_MatrixGuide. Der Service wurde gestoppt." 
     Dim SMTPMail As New SmtpClient 
     SMTPMail.Host = "zrhms200" 
     SMTPMail.Port = 25 ' Standard-Port 
     Dim msg As New MailMessage 
     msg.From = New MailAddress("[email protected]") 
     msg.To.Add("[email protected]") 
     msg.Subject = cMsg 
     msg.Body = cMsg 
     SMTPMail.Send(msg) 
    End Sub 
End Class 

我用VS2013,VB.NET和Win 8.1工作。

安裝.net RunTime,將安裝所有實際更新。

任何幫助將高度讚賞

非常感謝第一快速回答

弗雷迪

回答

2

接聽很晚你的答案,但還是認爲它可能對你有用。

您正在託管服務器內使用,這將是所述方法執行之後立即設置。嘗試刪除下面的語句,這將解決您的問題。

Using server As New HttpSelfHostServer(config)

+0

感謝您的(不幸的是)太晚的答案。與此同時,我已經改寫了Owin的服務(並且它的工作原理是:-)。 但是你的答案一般很有趣... 對你的答案的進一步問題: 如果我**刪除**使用服務器...語句,我如何訪問服務器(server.OpenAsync()。 或者你的意思是**更改**語句(將Dim服務器作爲新的HttpSelfHostServer(config)...或者什麼...)? 感謝您的時間 – FredyWenger

+0

您,先生,剛剛解決了我很多麻煩!我一直在摸索着我的頭來看看一段通過控制檯工作的代碼,而不是一段時間的Windows服務:=) – schizoid04

相關問題