自分自身と他の回答にリンクされている記事を含めて、多くの公開タブの後に、この問題に遭遇します。ここで
はあなたが必要となりますErrorHandlerのクラスは、次に、これらのセクションは
<system.serviceModel>
<extensions>
<behaviorExtensions>
<!-- Add in our custom error handler -->
<add name="ErrorLogging" type="Service.ErrorHandlerBehavior, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<services>
<service name="Service.Service" behaviorConfiguration="Service.ServiceBehavior">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp"
contract="SentinelService.IService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service.ServiceBehavior">
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
<!-- Add in our custom error handler, from behaviorExtensions element -->
<ErrorLogging />
</behavior>
</serviceBehaviors>
</behaviors>
...あなたのサービスプロジェクトのApp.configファイルで更新/追加する必要があります...
Imports System.ServiceModel.Configuration
Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher
''' <summary>
''' This class defines a global ErrorHandler, that allows us to control the
''' fault message returned to the client and perform custom error processing
''' like logging.
''' </summary>
Public Class ErrorHandler
Implements IErrorHandler
''' <summary>
''' This method will be called whenever an exception occurs. Therefore,
''' we log it and then return false so the error can continue to propagate up the chain.
''' </summary>
''' <param name="ex">Exception being raised.</param>
''' <returns>False to let the error propagate up the chain, or True to stop the error here.</returns>
Public Function HandleError(ByVal ex As Exception) As Boolean Implements IErrorHandler.HandleError
'Unknown error occurred at the Service layer, log the event
logEvent("Encountered an unknown error at the Service layer.", ex, EventLogEntryType.Error)
Return False
End Function
''' <summary>
''' This method is optionally used to transform standard exceptions into custom
''' FaultException(Of TDetail) that can be passed back to the service client.
''' </summary>
''' <param name="ex">Exception being raised.</param>
''' <param name="version">SOAP version of the message.</param>
''' <param name="fault">Message object that is returned to the client.</param>
Public Sub ProvideFault(ByVal ex As Exception, ByVal version As Channels.MessageVersion, ByRef fault As Channels.Message) Implements IErrorHandler.ProvideFault
End Sub
End Class
''' <summary>
''' This class defines a ServiceBehavior, that will allow us to add our
''' custom ErrorHandler class, defined above, to each channel we have a
''' service running on.
''' </summary>
Public Class ErrorServiceBehavior
Implements IServiceBehavior
Public Sub AddBindingParameters(serviceDescription As ServiceDescription, serviceHostBase As ServiceHostBase, endpoints As ObjectModel.Collection(Of ServiceEndpoint), bindingParameters As Channels.BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters
End Sub
Public Sub ApplyDispatchBehavior(serviceDescription As ServiceDescription, serviceHostBase As ServiceHostBase) Implements IServiceBehavior.ApplyDispatchBehavior
'Enumerate all channels and add the error handler to the collection
Dim handler As New ErrorHandler()
For Each dispatcher As ChannelDispatcher In serviceHostBase.ChannelDispatchers
dispatcher.ErrorHandlers.Add(handler)
Next
End Sub
Public Sub Validate(serviceDescription As ServiceDescription, serviceHostBase As ServiceHostBase) Implements IServiceBehavior.Validate
End Sub
End Class
''' <summary>
''' This class defines a BehaviorExtensionElement, so that we can
''' use the ErrorServiceBehavior class, defined above, in our App.config.
''' </summary>
Public Class ErrorHandlerBehavior
Inherits BehaviorExtensionElement
Protected Overrides Function CreateBehavior() As Object
Return New ErrorServiceBehavior()
End Function
Public Overrides ReadOnly Property BehaviorType As Type
Get
Return GetType(ErrorServiceBehavior)
End Get
End Property
End Class
です
具体的には、behaviorExtensionを追加する必要があります。その名前をビヘイビアセクションに追加する必要があり、ビヘイビアセクションの名前を付ける必要があり、そのビヘイビアセクションはサービスのbehaviorConfigurationである必要があります。
behaviorExtensionの "type"属性には注意してください。正確でなければなりません。あなたが確信が持てない場合は、次を使用してそれを決定することができますGetType(ErrorHandlerBehavior).AssemblyQualifiedName
。
あなたが興味を持っている場合、これは私が使用LOGEVENT機能の背後にあるコードです...
''' <summary>
''' Logs a message and optional exception to the application event log.
''' </summary>
''' <param name="message">String message to log.</param>
''' <param name="ex">Exception to log.</param>
''' <param name="eventType">EventLogEntryType indicating the message severity.</param>
Public Sub logEvent(ByVal message As String, Optional ByVal ex As Exception = Nothing, _
Optional ByVal eventType As EventLogEntryType = EventLogEntryType.Information)
'Initialize report
Dim report As String = message + vbNewLine + vbNewLine
'Create eventLogger
Dim eventLogger As New EventLog()
'Register event source, add any Exception information to the report, and then log it
Try
'Register the app as an Event Source
If Not EventLog.SourceExists("MyAppName") Then
EventLog.CreateEventSource("MyAppName", "Application")
End If
If ex IsNot Nothing Then
'Force eventType to error
eventType = EventLogEntryType.Error
'Add Exception Information to report
report += "Exception Information:" + vbNewLine
Dim currentException As Exception = ex
Dim exCount As Integer = 1
While (currentException IsNot Nothing)
report += Space(5) + If(exCount = 1, "Message:", "Inner Exception:") + vbNewLine
report += Space(10) + currentException.Message + vbNewLine
report += Space(5) + "StackTrace:" + vbNewLine
report += Space(10) + currentException.StackTrace + vbNewLine
report += vbNewLine
currentException = currentException.InnerException
exCount += 1
End While
End If
Catch reportException As Exception
'This catch ensures that no matter what some error report is logged.
report += vbNewLine + vbNewLine + "PARTIAL REPORT!!!...AN ERROR WAS ENCOUNTERED GENERATING ERROR REPORT."
Finally
Try
'Log report
eventLogger.Source = "MyAppName"
eventLogger.WriteEntry(report, eventType)
Catch logEventException As Exception
'Eat it...nothing can be done at this point and we do
'not want the application to crash over an error report
'if we can prevent it
End Try
End Try
End Sub
いいリンク。もし私ができるなら、私はそのリンクを+1したいと思う。 –
@LukasCenovsky +1は本質を含んでいる。 – pjvds
マーク、それはすべての例外をキャッチしますか? CurrentDomain.UnhandledExceptionでこれが私にスローされます。 私はそれについて何をすべきですか?私のサーバーアプリケーションがクラッシュします。 (コンテキスト単一、同時実行複数を使用して) System.ServiceModel.CommunicationObjectFaultedException:通信オブジェクト、System.ServiceModel.Channels.SecurityChannelListener'1 + SecurityReplySessionChannelそのため[System.ServiceModel.Channels.IReplySessionChannel]、通信のために使用することができませんフォールト状態です。 at System.ServiceModel.Channels.CommunicationObject.ThrowIfFaulted() –