다음을 통해 공유


방법: 원격 개체에 의해 throw될 수 있는 예외 형식 만들기

RemotingException 클래스에서 파생되고 ISerializable 인터페이스를 구현하는 사용자 고유의 예외 형식을 만들어 원격 개체가 throw하고 원격 호출자가 catch할 수 있습니다.

원격 개체에서 throw하고 원격 호출자에서 catch할 수 있는 예외 형식을 만들려면

  1. 1. RemotingException 클래스에서 파생되는 클래스를 정의합니다.

    Public Class RemotableType
       Inherits MarshalByRefObject
    End Class 'RemotableType
    
    public class RemotableType : MarshalByRefObject{
    }
    
  2. 클래스에 SerializableAttribute 특성을 지정합니다.

    <Serializable()> Public Class CustomRemotableException
        Inherits RemotingException
        …
    End Class
    
    [Serializable]
    public class CustomRemotableException : RemotingException, ISerializable {
        …
    }
    
  3. SerializationInfo 개체 및 StreamingContext 개체를 매개 변수로 사용하는 deserialization 생성자를 구현합니다.

예제

다음의 코드 예에서는 구성되었을 경우 원격 서버 개체가 throw하면 호출자에게 다시 복사되는 간단한 예외를 구현합니다.

<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 
      + "\"";
      }
   }
}

참고 항목

참조

ContextBoundObject

개념

원격화할 수 있는 개체와 원격화할 수 없는 개체

기타 리소스

원격화할 수 없는 개체