【问题标题】:Handle exceptions in .net remoting objects处理 .net 远程处理对象中的异常
【发布时间】:2011-08-20 13:07:30
【问题描述】:

我正在开发一个 .Net Remoting 项目。如果远程对象有任何异常,我想将该异常详细发送给客户端。我正在使用以下代码来实现这一点-

'This is on a shared .dll
Public Interface ICreateNewMouza
Function CreateNewMouza(ByVal MouzaToCreate As Mouza) As Integer
End Interface

Imports System
Imports System.Runtime.Serialization

<serializable()> _
Public Class CustomException
Inherits System.ApplicationException

Public Sub New(ByVal message As String)
    MyBase.New(message)
End Sub

Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
    MyBase.New(info, context)
End Sub

Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
    MyBase.GetObjectData(info, context)
End Sub
End Class

'This is remote object which a client will invoke-

Imports System.Runtime.Remoting
Imports ClassInterfaces

Public Class CreateNewMouza
Inherits MarshalByRefObject
Implements ClassInterfaces.ICreateNewMouza

Public Function CreateNewMouza(ByVal MouzaToCreate As ClassInterfaces.Mouza) As Integer Implements ClassInterfaces.ICreateNewMouza.CreateNewMouza
    Try
       ' some code
    Catch ex As Exception
      ## what should be here?
    End Try

End Function
End Class

try..catch 块中应该包含什么?我错过了什么吗? 请帮帮我。

提前致谢 SK保罗

【问题讨论】:

    标签: .net remoting


    【解决方案1】:

    您已经找到了 - 您将捕获的异常是 RemotingException。与远程处理相比,我一直更喜欢 WCF,但您的示例中似乎已正确设置。

    您是否遇到了特定问题,或者它无法正常工作,或者您只是在设置设置时出于好奇而询问?

    【讨论】:

    • 亲爱的 rwmnau,第一件事是,我无法将异常发送给客户端。但我能够在该远程对象中收集异常。现在,如何发送给客户?也许,我不知道正确的方法。
    • @SKPaul。只需在 Try/Catch 块中抛出您的自定义异常。您的客户端将收到 CustomException 类型的异常。要向您的异常添加其他信息,请向您的类添加一个属性并在相关函数/构造函数中对其进行序列化/反序列化。
    • Hansjoerg,我不想抛出任何异常。但是如果发生任何异常,我想抓住它并将其传达给客户。希望,你已经实现了我的意图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多