是什么让WCF ServiceHost崩溃?

时间:2010-07-12 17:00:54

标签: c# .net wcf servicehost

我有一个Windows服务,使用WCF在4个端口上公开相同的接口。地址是:

net.tcp://localhost:1200/IContract
net.tcp://localhost:1201/IContract
net.tcp://localhost:1202/IContract
net.tcp://localhost:1203/IContract

这项服务已经投入生产很长时间了,有时它会中断,我甚至无法通过问题远程登陆该端口。我通常不得不重置服务。

我真的不明白为同一个合同拥有大量的端口,但这个解决方案可能掩盖了原来的问题。

无论如何,什么可能使servicehost在服务器端崩溃?客户端可能会崩溃servicehost,还是可能与某种拒绝服务有关?

PS:这个问题很容易发生,我无法在开发中重现。在生产中使用痕迹也是不切实际的。

由于

2 个答案:

答案 0 :(得分:2)

您可以向Dr. Watson寻求帮助。您可以为您的应用程序配置WEH(前提是您可以签署代码)。或者,您可以使用bugcollect.comexceptioneer.comerrortc.com等工具收集崩溃信息。

但最终,你不能简单地问'任意过程如何崩溃'。有太多方法。你最多可以得到一个通用的答案('它崩溃了,因为它取消了一个受保护的地址')。

答案 1 :(得分:1)

服务主机可能会失败。如果你解决这个问题并不重要,下次他们就会以不同的方式失败。

我通过创建自己的ServiceHost子类型来解决这个问题,其中包括日志记录和自动重启功能。

Public Class RestartableServiceHost
    Inherits ServiceHost

    Private m_Log As FileLogger
    Private ReadOnly m_FaultResponse As ServiceHostFaultResponse
    Private ReadOnly m_Name As String

    Public Sub New(ByVal serviceType As Type, ByVal faultResponse As ServiceHostFaultResponse, ByVal log As FileLogger)
        MyBase.New(serviceType)
        If serviceType Is Nothing Then Throw New ArgumentNullException("serviceType", "serviceType is nothing.")
        If log Is Nothing Then Throw New ArgumentNullException("log", "log is nothing.")


        m_Log = log
        m_FaultResponse = faultResponse
        m_Name = serviceType.Name & " service host"
    End Sub

    Public Sub New(ByVal singletonInstance As Object, ByVal faultResponse As ServiceHostFaultResponse, ByVal log As FileLogger)
        MyBase.New(singletonInstance)

        If singletonInstance Is Nothing Then Throw New ArgumentNullException("singletonInstance", "singletonInstance is nothing.")
        If log Is Nothing Then Throw New ArgumentNullException("log", "log is nothing.")

        m_Log = log
        m_FaultResponse = faultResponse
        m_Name = singletonInstance.GetType.Name & " service host"
    End Sub

    Private Sub AamServiceHost_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Closed
        m_Log.Write(m_Name & " has closed")
    End Sub

    Private Sub AamServiceHost_Closing(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Closing
        m_Log.Write(m_Name & " is closing")
    End Sub

    Private Sub AamServiceHost_Faulted(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Faulted
        m_Log.Write(m_Name & " has faulted.")

        Select Case m_FaultResponse
            Case ServiceHostFaultResponse.None
                'NOP
            Case ServiceHostFaultResponse.AbortReopenThrow
                Try
                    Abort()
                Catch ex As Exception
                    m_Log.Write("Unable to abort SecurityMasterChangeListener Service Host", ex, Severity.Warning)
                End Try
                Threading.Thread.Sleep(TimeSpan.FromSeconds(30))
                Try
                    Open()
                Catch ex As Exception
                    m_Log.Write("Unable to reopen SecurityMasterChangeListener Service Host.", ex, Severity.ErrorServiceFailed)
                    Throw
                End Try
        End Select

    End Sub

    Private Sub AamServiceHost_Opened(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Opened
        m_Log.Write(m_Name & " has opened")
    End Sub

    Private Sub AamServiceHost_Opening(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Opening
        m_Log.Write(m_Name & " is opening")
    End Sub

    Private Sub AamServiceHost_UnknownMessageReceived(ByVal sender As Object, ByVal e As UnknownMessageReceivedEventArgs) Handles Me.UnknownMessageReceived
        m_Log.Write("SecurityMasterChangeListener Service Host has received an unknown message. The message will be ignored.", Severity.ErrorTaskFailed)
    End Sub

End Class