Procedura: creare un tipo di eccezione generabile da oggetti remoti
Per creare un tipo di eccezione personalizzato che puo` essere generato da un oggetto remoto e intercettato da un chiamante remoto, e` possibile derivare la classe RemotingException e implementare l'interfaccia ISerializable.
Per creare un tipo di eccezione generabile da oggetti remoti e intercettabile da chiamanti remoti
1.Definire una classe che deriva dalla classe RemotingException.
Public Class RemotableType Inherits MarshalByRefObject End Class 'RemotableType
public class RemotableType : MarshalByRefObject{ }
Impostare l'attributo SerializableAttribute per la classe.
<Serializable()> Public Class CustomRemotableException Inherits RemotingException … End Class
[Serializable] public class CustomRemotableException : RemotingException, ISerializable { … }
Implementare un costruttore di deserializzazione che accetta un oggetto SerializationInfo e un oggetto StreamingContext come parametri.
Esempio
In questo esempio di codice viene fornita una semplice implementazione che, se configurata, viene copiata sul chiamante quando e` generata dall'oggetto server remoto.
<Serializable()> Public Class CustomRemotableException
Inherits RemotingException
Private _internalMessage As String
Public Sub CustomRemotableException()
_internalMessage = String.Empty
End Sub
Public Sub CustomRemotableException(ByVal message As String)
_internalMessage = message
End Sub
Public Sub CustomRemotableException(ByVal info As _
SerializationInfo, ByVal context As StreamingContext)
_internalMessage = info.GetValue("_internalMessage", _
_internalMessage.GetType())
End Sub
Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
info.AddValue("_internalMessage", _internalMessage)
End Sub
Public Shadows 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
+ "\"";
}
}
}
Vedere anche
Riferimenti
Concetti
Oggetti remotizzabili e non remotizzabili