共用方式為


遠端處理範例:追蹤服務

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

TrackingServices 類別會透過可外掛式追蹤處理常式提供給一般追蹤服務使用。在下列情況下會呼叫 ITrackingHandler 介面的方法:

  • 已產生 ObjRef 物件時 (封送處理的結果)。

  • 已收到 ObjRef 時 (解封送處理的結果)。

  • 以中斷連接某個物件時。

如需詳細資訊,請參閱參考文件中的 TrackingServicesITrackingHandler

3tzky0f2.Caution(zh-tw,VS.100).gif注意:
.NET Framework 遠端處理依預設不進行驗證或加密。因此,建議您採取所有必要的步驟,以確認用戶端或伺服器的識別 (Identity),然後再與其進行遠端互動。由於 .NET Framework 遠端處理應用程式需要 FullTrust 權限才能執行,所以如果某個未經授權的用戶端被授與伺服器的存取權,該用戶端就可以執行程式碼,如同它已完全受信任。請務必驗證您的端點並加密通訊資料流,方法包括在網際網路資訊服務 (IIS) 中裝載遠端型別或建置自訂通道接收組來進行這項工作。

若要編譯和執行這個範例

  1. 將所有檔案複製到同一個目錄中。

  2. 在命令提示字元中輸入下列命令:

    vbc /t:library /r:System.Runtime.Remoting.dll TrackingHandler.vb
    vbc /t:library /r:System.Runtime.Remoting.dll RemoteType.vb
    vbc /r:RemoteType.dll /r:System.Runtime.Remoting.dll /r:TrackingHandler.dll server.vb
    vbc /r:RemoteType.dll /r:System.Runtime.Remoting.dll client.vb
    
    csc /t:library /r:System.Runtime.Remoting.dll TrackingHandler.cs
    csc /t:library /r:System.Runtime.Remoting.dll RemoteType.cs
    csc /r:RemoteType.dll /r:System.Runtime.Remoting.dll /r:TrackingHandler.dll server.cs
    csc /r:RemoteType.dll /r:System.Runtime.Remoting.dll client.cs
    
  3. 開啟兩個指向相同目錄的命令提示字元。在其中一個提示字元底下,輸入 server。在另一個命令提示字元底下,輸入 client

這個應用程式會在單一電腦或網路上執行。如果您要透過網路執行這個應用程式,您必須以遠端電腦的名稱取代用戶端組態中的 "localhost"

TrackingHandler

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Services

Public Class TrackingHandler
    Implements ITrackingHandler

    ' Notifies a handler that an object has been marshaled.
    Public Sub MarshaledObject(ByVal obj As Object, ByVal objref As System.Runtime.Remoting.ObjRef) Implements System.Runtime.Remoting.Services.ITrackingHandler.MarshaledObject
        Console.WriteLine("Tracking: An instance of {0} was marshaled. The instance HashCode is: {1}", _
            obj.ToString(), obj.GetHashCode().ToString())
        Console.WriteLine("ObjRef dump:")
        If (objref.ChannelInfo IsNot Nothing) Then
            Console.WriteLine("  -- ChannelInfo: ")
            DumpChannelInfo(objref.ChannelInfo)
        End If

        If (objref.EnvoyInfo IsNot Nothing) Then
            Console.WriteLine("  -- EnvoyInfo: " + CType(objref.EnvoyInfo, Object).ToString())
        End If
        If (objref.TypeInfo IsNot Nothing) Then
            Console.WriteLine("  -- TypeInfo: " + CType(objref.TypeInfo, Object).ToString())
            Console.WriteLine("      -- " + objref.TypeInfo.TypeName)
        End If
        If (objref.URI IsNot Nothing) Then
            Console.WriteLine("  -- URI: " + objref.URI.ToString())
        End If
    End Sub

    Private Sub DumpChannelInfo(ByVal info As IChannelInfo)
        Dim obj As Object
        For Each obj In info.ChannelData
            If (obj Is GetType(ChannelDataStore)) Then
                Dim uri As String
                For Each uri In CType(obj, ChannelDataStore).ChannelUris
                    Console.WriteLine("      -- ChannelUris:" + uri)
                Next
            End If
        Next

    End Sub

    ' Notifies a handler that an object has been unmarshaled.
    Public Sub UnmarshaledObject(ByVal obj As Object, ByVal [or] As System.Runtime.Remoting.ObjRef) Implements System.Runtime.Remoting.Services.ITrackingHandler.UnmarshaledObject
        Console.WriteLine("Tracking: An instance of {0} was unmarshaled. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString())
    End Sub

    ' Notifies a handler that an object has been disconnected.
    Public Sub DisconnectedObject(ByVal obj As Object) Implements System.Runtime.Remoting.Services.ITrackingHandler.DisconnectedObject
        Console.WriteLine("Tracking: An instance of {0} was disconnected. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString())
    End Sub
End Class
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Services;

namespace TrackingHandler
{
    public class TrackingHandler : ITrackingHandler
    {
        // Notifies a handler that an object has been marshaled.
        public void MarshaledObject(Object obj, ObjRef objref)
        {
            Console.WriteLine("Tracking: An instance of {0} was marshaled. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString());
            Console.WriteLine("ObjRef dump:");
            if (objref.ChannelInfo != null)
            {
                Console.WriteLine("  -- ChannelInfo: ");
                DumpChannelInfo(objref.ChannelInfo);
            }
            if (objref.EnvoyInfo != null)
                Console.WriteLine("  -- EnvoyInfo: " + objref.EnvoyInfo.ToString());
            if (objref.TypeInfo != null)
            {
                Console.WriteLine("  -- TypeInfo: " + objref.TypeInfo.ToString());
                Console.WriteLine("      -- " + objref.TypeInfo.TypeName);
            }
            if (objref.URI != null)
                Console.WriteLine("  -- URI: " + objref.URI.ToString());
        }

        private void DumpChannelInfo(IChannelInfo info)
        {

            foreach (object obj in info.ChannelData)
            {
                if (obj is ChannelDataStore)
                {
                    foreach (string uri in ((ChannelDataStore)obj).ChannelUris)
                        Console.WriteLine("      -- ChannelUris:" + uri);
                }
            }
        }

        // Notifies a handler that an object has been unmarshaled.
        public void UnmarshaledObject(Object obj, ObjRef or)
        {
            Console.WriteLine("Tracking: An instance of {0} was unmarshaled. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString());
        }

        // Notifies a handler that an object has been disconnected.
        public void DisconnectedObject(Object obj)
        {
            Console.WriteLine("Tracking: An instance of {0} was disconnected. The instance HashCode is: {1}", 
                obj.ToString(), obj.GetHashCode().ToString());
        }
    }
}

RemoteType

Imports System

Public Class ServiceClass
    Inherits MarshalByRefObject

    Private starttime As DateTime

    Public Sub New()
        Console.WriteLine("A ServiceClass has been created.")
        starttime = DateTime.Now
    End Sub

    Protected Overrides Sub Finalize()
        Console.WriteLine("ServiceClass being collected after " & (New TimeSpan(DateTime.Now.Ticks - starttime.Ticks)).ToString() & " seconds.")
    End Sub

    Public Function GetServerTime() As DateTime
        Console.WriteLine("Time requested by client")
        Return DateTime.Now
    End Function
End Class
using System;

namespace RemoteType
{
    public class ServiceClass : MarshalByRefObject
    {
        private DateTime starttime;

        public ServiceClass()
        {
            Console.WriteLine("A ServiceClass has been created.");
            starttime = DateTime.Now;
        }

        ~ServiceClass()
        {
            Console.WriteLine("ServiceClass being collected after " + (new TimeSpan(DateTime.Now.Ticks - starttime.Ticks)).ToString() + " seconds.");
        }

        public DateTime GetServerTime()
        {
            Console.WriteLine("Time requested by client.");
            return DateTime.Now;
        }
    }
}

伺服器

[Visual Basic]

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Services
Imports TrackingHandler

Public Class Server
    Public Shared Sub Main()
        Dim channel As TcpChannel = New TcpChannel(8080)
        ChannelServices.RegisterChannel(channel, False)

        TrackingServices.RegisterTrackingHandler(New TrackingHandler())

        Dim service As ServiceClass = New ServiceClass()
        Dim obj As ObjRef = RemotingServices.Marshal(service, "TcpService")

        Console.WriteLine("Press Enter to unmarshal the object.")
        Console.ReadLine()

        RemotingServices.Unmarshal(obj)

        Console.WriteLine("Press Enter to disconnect the object.")
        Console.ReadLine()

        RemotingServices.Disconnect(service)
    End Sub
End Class

[C#]

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Services;
using TrackingHandler;
using RemoteType;

namespace Server
{
    public class Server
    {
        public static void Main(string[] Args)
        {
            TcpChannel channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel, false);

            TrackingServices.RegisterTrackingHandler(new TrackingHandler.TrackingHandler());

            ServiceClass service = new ServiceClass();
            ObjRef obj = RemotingServices.Marshal(service, "TcpService");

            Console.WriteLine("\r\nPress Enter to unmarshal the object.");
            Console.ReadLine();

            RemotingServices.Unmarshal(obj);

            Console.WriteLine("Press Enter to disconnect the object.");
            Console.ReadLine();

            RemotingServices.Disconnect(service);
        }
    }
}

用戶端

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Public Class Client

    Public Shared Sub Main()
        ChannelServices.RegisterChannel(New TcpChannel(), False)

        Dim remotetype As WellKnownClientTypeEntry = New WellKnownClientTypeEntry( _
            GetType(ServiceClass), _
            "tcp://localhost:8080/TcpService")
        RemotingConfiguration.RegisterWellKnownClientType(remotetype)

        Dim service As ServiceClass = New ServiceClass()
        Console.WriteLine("Server time is: " & service.GetServerTime().ToLongTimeString())
    End Sub

End Class
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteType;

namespace Client
{
    public class ClientProcess
    {
        public static void Main(string[] Args)
        {
            ChannelServices.RegisterChannel(new TcpChannel(), false);

            WellKnownClientTypeEntry remotetype = new WellKnownClientTypeEntry(
                typeof(ServiceClass), 
                "tcp://localhost:8080/TcpService");
            RemotingConfiguration.RegisterWellKnownClientType(remotetype);

            ServiceClass service = new ServiceClass();
            Console.WriteLine("Server time is: " + service.GetServerTime().ToLongTimeString());
        }
    }
}

另請參閱

參考

ITrackingHandler
TrackingServices

其他資源

遠端處理範例