【发布时间】:2015-02-26 03:45:09
【问题描述】:
我正在尝试为我的 WCF 客户端应用程序构建错误日志记录机制,以便它们可以将 WCF 上下文之外可能发生的任何异常报告回服务。
在这种情况下,我绝对保证所有客户端都是 .NET。
我找到了大量关于如何配置以另一种方式(例如服务到客户端)的信息、文章和示例,但没有关于客户端到服务的信息。
我希望在客户端上做这样的事情:
Dim i As Integer
Try
i = String.Empty
Catch ex As Exception
Client.Create.Call(Sub(S As IService)
S.Log(LogLevels.Error, ex)
End Sub)
End Try
FaultException(Of T) 不起作用;我试过这个:
Dim i As Integer
Try
i = String.Empty
Catch ex As Exception
Try
Throw New FaultException(Of Exception)(ex)
Catch fex As FaultException
Client.Create.Call(Sub(S As IService)
S.Log(LogLevels.Error, fex.Message, fex)
End Sub)
End Try
End Try
它自己返回一条极其晦涩难懂的错误消息:
“不应使用数据协定名称‘InvalidCastException:http://schemas.datacontract.org/2004/07/System’键入‘System.InvalidCastException’。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。”
异常的二进制序列化也不起作用;我在反序列化时遇到错误:
“输入流不是有效的二进制格式。”
我的代码:
Class Test
Public Sub Test
Dim i As Integer
Dim aEx As Byte()
Dim oEx As Exception
Try
i = String.Empty
Catch ex As Exception
aEx = ex.ToBytes
oEx = aEx.ToObject(Of Exception)()
End Try
End Sub
End Class
Public Module Generic
<Extension>
Public Function ToBytes(Of T)([Object] As T) As Byte()
Using oStream As New MemoryStream
With New BinaryFormatter
.Serialize(oStream, [Object])
End With
Return oStream.ToArray
End Using
End Function
<Extension>
Public Function ToObject(Of T)(Data As Byte()) As T
Using oStream As New MemoryStream
oStream.Write(Data, 0, Data.Length)
oStream.Seek(0, SeekOrigin.Begin)
With New BinaryFormatter
Return .Deserialize(oStream)
End With
End Using
End Function
End Module
如何通过网络发送此非 WCF 异常?
编辑
这是我的服务界面,为简洁而编辑:
<ServiceContract>
Public Interface IService
<OperationContract> Sub Log(Level As LogLevels, Message As String, Exception As Exception)
End Interface
【问题讨论】:
标签: wcf exception serialization exception-handling