我有一个托管WebAPI(HttpSelfHostServer)的控制台应用程序。
在console-app中,我有控制器从SQL-Server查询数据。
控制台应用程序没有问题(来自fiddler和Windows_client) 现在我想在Windows服务中托管WebAPI(HttpSelfHostServer)。
因此我创建了一个Service-Project并在console-app-dll中引用了控制器,以便找到控制器。
为了获得最小的调试机会,我在Windows服务的事件日志中写了一些信息。
我可以毫无问题地启动和停止Windows服务。 似乎httpselfhost已启动并处于活动状态。
问题: - 首次尝试使用网址时,服务严重崩溃:
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-service中的自托管启动工作正常,主机也可以访问(因为第一次访问时崩溃)。
所以..也许我在服务中做了一些完全错误的事情,或者我错过了重要的事情......
由于控制台应用程序没有问题,我在下面只发布了windows-service中的代码: 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("Service@matso.ch")
msg.To.Add("fredy.wenger@matso.ch")
msg.Subject = cMsg
msg.Body = cMsg
SMTPMail.Send(msg)
End Sub
End Class
我正在使用VS2013,VB.NET和Win 8.1。
安装.net RunTime,安装所有实际更新。
任何帮助都将受到高度赞赏
非常感谢第一个快速回答
的Fredy
答案 0 :(得分:2)
回答你的回答很晚,但仍然认为它对你有用。
您正在使用托管服务器,在方法执行后会立即处置。尝试删除以下声明,这将解决您的问题。
Using server As New HttpSelfHostServer(config)