共用方式為


HOW TO:建立可由遠端物件擲回的例外狀況類型

本主題專門說明一項為了在現有應用程式中提供回溯相容性而保留的舊有技術,不建議用於新的開發工作。分散式應用程式應使用 Windows Communication Foundation (WCF) 進行開發。

您可以藉由從 RemotingException 類別衍生並實作 ISerializable 介面,建立可由遠端物件擲回並由遠端呼叫端攔截的例外狀況類型。

若要建立可由遠端物件擲回並由遠端呼叫端攔截的例外狀況類型

  1. 定義一個衍生自 RemotingException 類別的類別。

    Public Class RemotableType
       Inherits MarshalByRefObject
          Implements ISerializable
    ' ...
    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 物件做為參數的還原序列化建構函式。

    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));
     }
    

範例

下列程式碼範例提供一個簡單的實作,一旦經過設定,此實作就會在遠端伺服器物件將它擲回時複製回呼叫端。

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

另請參閱

參考

ContextBoundObject

概念

可遠端處理和不可遠端處理的物件

建置日期:2010-02-13