2010-07-12 46 views
1

我有一個Windows服務,使用WCF在4個端口上公開相同的接口。地址是:是什麼讓WCF ServiceHost崩潰?

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

這項服務是在生產中很長一段時間,也有它打破了時間,我甚至不能遠程登錄端口的問題。我通常必須重置服務。

我真的不明白同一份合同有很多端口,但這個解決方案可能掩蓋了原來的問題。

無論如何,有什麼可以使服務器端的服務器崩潰?客戶端可能會崩潰servicehost,或者它可能與某種拒絕服務有關?

PS:這個問題在本地出現,我無法在dev中重現。在生產中使用痕跡也不切實際。

感謝

+0

您是否添加了日誌記錄來監視服務運行過程中發生了什麼?有幾個客戶同時打這個服務嗎?它是如何配置的?未處理的異常可能會導致服務停止響應,請參閱http://stackoverflow.com/questions/1136048/unhandled-exception-will-make-wcf-service-crash以獲取更多信息。 – 2010-07-12 17:05:37

+0

是的,有多個客戶端連接在同一個端口上獲取大量數據。該程序不受DOS攻擊保護,但是如果是這種情況,應該預期這種行爲嗎?也許只要它掉落就重新打開頻道比較容易... – Marcus 2010-07-12 23:33:12

回答

2

你可以問Dr. Watson尋求幫助。您可以爲您的應用程序配置WEH(前提是您可以簽署代碼)。或者您可以使用如bugcollect.comexceptioneer.comerrortc.com等工具收集崩潰信息。但最終,你不能只是簡單地問'任意進程崩潰怎麼辦'。有太多的方法。你最多可以得到一個通用的答案('它崩潰了,因爲它解除了一個受保護的地址的引用)。

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