【问题标题】:Hosting WebAPI in Windows-Service crashes on first attempt首次尝试在 Windows 服务中托管 WebAPI 崩溃
【发布时间】:2014-07-17 13:57:26
【问题描述】:

我有一个带有托管 WebAPI (HttpSelfHostServer) 的控制台应用程序。

在控制台应用程序中,我有从 SQL Server 查询数据的控制器。

控制台应用程序没有问题(来自 fiddler 和 Windows_client) 现在我想在 Windows 服务中托管 WebAPI (HttpSelfHostServer)。

因此,我创建了一个 Service-Project 并在 console-app-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 服务的代码: 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 已安装,所有实际更新均已安装。

我们将不胜感激任何帮助

非常感谢您的第一次快速答复

弗雷迪

【问题讨论】:

    标签: .net vb.net asp.net-web-api windows-services self-hosting


    【解决方案1】:

    回答你的答案很晚,但仍然认为它可能对你有用。

    您在内部使用托管服务器,该服务器将在方法执行后立即处置。尝试删除以下语句,这将解决您的问题。

    Using server As New HttpSelfHostServer(config)

    【讨论】:

    • 感谢您(很遗憾)迟到的答复。与此同时,我已经将服务重写为 Owin(并且......它有效:-)。但是您的回答通常很有趣...对您的回答的进一步问题:如果我删除 Using server ...语句,我如何访问服务器(server.OpenAsync().Wait)?还是您的意思是 change 声明(将服务器调暗为 New HttpSelfHostServer(config)... 或..what...)?感谢您的时间
    • 你,先生,给我解决了很多麻烦!!!一段时间以来,我一直在为一组在控制台上工作但在 Windows 服务上工作的代码摸不着头脑:=)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多