服務主機可能會失敗。無論你是否解決這個問題都沒有關係,他們只是在下次會以不同的方式失敗。
我通過創建自己的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
您是否添加了日誌記錄來監視服務運行過程中發生了什麼?有幾個客戶同時打這個服務嗎?它是如何配置的?未處理的異常可能會導致服務停止響應,請參閱http://stackoverflow.com/questions/1136048/unhandled-exception-will-make-wcf-service-crash以獲取更多信息。 – 2010-07-12 17:05:37
是的,有多個客戶端連接在同一個端口上獲取大量數據。該程序不受DOS攻擊保護,但是如果是這種情況,應該預期這種行爲嗎?也許只要它掉落就重新打開頻道比較容易... – Marcus 2010-07-12 23:33:12