Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是什么导致WCF ServiceHost崩溃?_C#_.net_Wcf_Servicehost - Fatal编程技术网

C# 是什么导致WCF ServiceHost崩溃?

C# 是什么导致WCF ServiceHost崩溃?,c#,.net,wcf,servicehost,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 该服务已投入生产很长时间,有时会中断,我甚至无法通过telnet将该问题发送到端口。我通常需要重新设置服务 我真的不明白为同一个合同提供多

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

net.tcp://localhost:1200/IContract
net.tcp://localhost:1201/IContract
net.tcp://localhost:1202/IContract
net.tcp://localhost:1203/IContract
该服务已投入生产很长时间,有时会中断,我甚至无法通过telnet将该问题发送到端口。我通常需要重新设置服务

我真的不明白为同一个合同提供多个端口的意义,但是这个解决方案可能掩盖了最初的问题

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

PS:这个问题偶尔出现,我无法在开发中重现。在生产中使用跟踪也不实用

谢谢你可以寻求帮助。您可以为应用程序进行配置(前提是您可以对代码进行签名)。或者,您可以使用诸如或之类的工具收集崩溃信息


但最终,您不能简单地问“任意进程如何崩溃”。方法实在太多了。您最多只能得到一个通用答案(“它崩溃了,因为它取消了对受保护地址的引用”)。

服务主机可能会失败。不管你是否解决了这个问题,下次它们只会以不同的方式失败

为此,我创建了自己的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

您是否添加了日志记录以监视服务运行时内部发生的情况?是否有多个客户端同时访问该服务?它是如何配置的?未经处理的异常可能会导致服务停止响应。有关更多信息,请参阅此-是的,有多个客户端连接在同一端口上,获取大量数据。程序不受DOS攻击的保护,但如果是这种情况,是否应该预期这种行为?也许在频道掉下时重新打开频道更容易些。。。