Compartilhar via


Como: Criar um tipo de exceção que pode ser Descartado por objetos remoto

You can Criar Your own tipo exceção that can be thrown by a remoto objeto and caught by a remoto chamador by Deriving from the classe RemotingException and Implementing the interface ISerializable.

To Criar an tipo exceção that can be thrown by remoto objects and caught by remoto callers

  • 1. Definir a classe that derives from the RemotingException classe.

    Public Class RemotableType
       Inherits MarshalByRefObject
    
      Implements ISerializable
' ...
End Class 'RemotableType
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        _internalMessage = info.GetValue("_internalMessage", GetType(String))
    End Sub
   public CustomRemotableException(SerializationInfo info, StreamingContext context)
   {
      _internalMessage = (string)info.GetValue("_internalMessage", typeof(string));
 }

Exemplo

The seguinte Provides exemplo de código a Simples implementação that, IF configured, is COPIED voltar to the chamador When thrown by the objeto servidor remoto.

<Serializable()> Public Class CustomRemotableException
    Inherits RemotingException
    Implements ISerializable

    Private _internalMessage As String

    Public Sub New()
        _internalMessage = String.Empty
    End Sub

    Public Sub New(ByVal message As String)
        _internalMessage = message
    End Sub

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        _internalMessage = info.GetValue("_internalMessage", GetType(String))
    End Sub

    Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        info.AddValue("_internalMessage", _internalMessage)
    End Sub

    Public Overrides ReadOnly Property Message() As String
        Get
            Return "This is your custom remotable exception returning : """ + _internalMessage + """"
        End Get
    End Property
End Class
    [Serializable]
    public class CustomRemotableException : RemotingException, ISerializable
    {
        private string _internalMessage;

        public CustomRemotableException()
        {
            _internalMessage = String.Empty;
        }

        public CustomRemotableException(string message)
        {
            _internalMessage = message;
        }

        public CustomRemotableException(SerializationInfo info, StreamingContext context)
        {
            _internalMessage = (string)info.GetValue("_internalMessage", typeof(string));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("_internalMessage", _internalMessage);
        }

        // Returns the exception information. 
        public override string Message
        {
            get
            {
                return "This is your custom remotable exception returning: \""
          + _internalMessage
          + "\"";
            }
        }
    }

Consulte também

Conceitos

Remotable e Nonremotable objetos

Referência

ContextBoundObject

Outros recursos

Nonremotable Objects