我有一个带有托管WebAPI (HttpSelfHostServer)的控制台应用程序。
在控制台应用程序中,我有从Server查询数据的控制器。
控制台-应用程序没有问题(从小提琴和Windows_client),现在我想在Windows中托管WebAPI (HttpSelfHostServer)。
因此,我创建了一个服务项目,并引用控制台应用程序dll中的控制器,以便找到控制器。
为了尽可能少地进行调试,我从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-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,所有实际更新都已安装。
任何帮助都会非常感谢
非常感谢您的第一个快速回答
弗雷迪
发布于 2014-09-24 06:59:25
对你的答案回答得很晚,但仍然认为它对你有帮助。
您正在使用托管服务器,该服务器将在方法执行后立即被释放。尝试删除以下语句,这将解决您的问题。
Using server As New HttpSelfHostServer(config)
https://stackoverflow.com/questions/24805600
复制相似问题