What makes WCF ServiceHost crash?
I have a Windows Service that exposes the same interface on 4 ports using WCF. The addresses are:
net.tcp://localhost:1200/IContract
net.tcp://localhost:1201/IContract
net.tcp://localhost:1202/IContract
net.tcp://localhost:1203/IContract
This service is in production开发者_Python百科 for a long time and there are times it breaks and I cannot even telnet the port with the problem. I usually have to reset the service.
I really don't get the point of having lots of ports for the same contract, but this solution probably masked the original problem.
Anyway, what could be making the servicehost to crash on the server side? Could a client crash the servicehost, or it may be related to some kind of denial of service?
PS: this problem occurs esporadically and I'm not able to reproduce in dev. Using a trace in production is not practical too.
Thanks
You can ask Dr. Watson for help. You can configure WEH for your application (provided you can sign your code). Or you can collect crash information using tools like bugcollect.com, exceptioneer.com or errortc.com.
But ultimately, you can't just simply ask 'how can a arbitrary process crash'. There are simply too many ways. You can get at best a generic answer ('It crashed because it dereferenced a protected address').
Service hosts can fail. It doesn't matter if you fix this or not, they are just goign to fail in a different fashion next time.
I account for this by creating my own subtype of ServiceHost that includes logging and automatic restarting capabilities.
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
精彩评论