TrackingServices 類別

定義

提供註冊、移除註冊和取得追蹤處理常式清單的方式。

public ref class TrackingServices
public class TrackingServices
[System.Runtime.InteropServices.ComVisible(true)]
public class TrackingServices
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public class TrackingServices
type TrackingServices = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type TrackingServices = class
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type TrackingServices = class
Public Class TrackingServices
繼承
TrackingServices
屬性

範例

下列程式代碼範例示範如何使用 類別的 TrackingServices 方法來註冊和取消註冊追蹤處理程式。

下列程式代碼範例示範如何實作追蹤處理程式。

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Services;

// Intercept marshal, unmarshal, and disconnect events for an object.
public class TrackingHandler : ITrackingHandler
{
    // Called when the tracked object is marshaled.

    public void MarshaledObject(Object obj, ObjRef objRef)
    {
        // Notify the user of the marshal event.
        Console.WriteLine("Tracking: An instance of {0} was marshaled.", 
            obj.ToString());

        // Print the channel information.
        if (objRef.ChannelInfo != null)
        {
            // Iterate over ChannelData.
            foreach(object data in objRef.ChannelInfo.ChannelData)
            {
                if (data is ChannelDataStore)
                {
                    // Print the URIs from the ChannelDataStore objects.
                    string[] uris = ((ChannelDataStore)data).ChannelUris;
                    foreach(string uri in uris)
                        Console.WriteLine("ChannelUri: " + uri);
                }
            }
        }

        // Print the envoy information.
        if (objRef.EnvoyInfo != null)
            Console.WriteLine("EnvoyInfo: " + objRef.EnvoyInfo.ToString());

        // Print the type information.
        if (objRef.TypeInfo != null)
        {
            Console.WriteLine("TypeInfo: " + objRef.TypeInfo.ToString());
            Console.WriteLine("TypeName: " + objRef.TypeInfo.TypeName);
        }

        // Print the URI.
        if (objRef.URI != null)
            Console.WriteLine("URI: " + objRef.URI.ToString());
    }

    // Called when the tracked object is unmarshaled.
    public void UnmarshaledObject(Object obj, ObjRef objRef)
    {
        Console.WriteLine("Tracking: An instance of {0} was unmarshaled.", 
            obj.ToString());
    }

    // Called when the tracked object is disconnected.
    public void DisconnectedObject(Object obj)
    {
        Console.WriteLine("Tracking: An instance of {0} was disconnected.", 
            obj.ToString());
    }
}

下列程式代碼範例示範如何在伺服器上實作這個類別。

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

public class Server
{
    public static void Main(string[] args)
    {
        // Register the TCP channel.
        TcpChannel channel = new TcpChannel(1234);
        ChannelServices.RegisterChannel(channel);

        // Register a tracking handler.
        ITrackingHandler handler1 = new TrackingHandler();
        TrackingServices.RegisterTrackingHandler(handler1);

        // Register a second handler.
        ITrackingHandler handler2 = new TrackingHandler();
        TrackingServices.RegisterTrackingHandler(handler2);

        // Get the number of currently registered handlers.
        Console.WriteLine("Registered tracking handlers: " + 
            TrackingServices.RegisteredHandlers.Length);

        // Remove the tracking handler from the registered handlers.
        TrackingServices.UnregisterTrackingHandler(handler2);
        Console.WriteLine("Registered tracking handlers: " + 
            TrackingServices.RegisteredHandlers.Length);

        // Create and marshal an object for remote invocation.
        RemoteService service = new RemoteService();
        ObjRef obj = RemotingServices.Marshal(service, "TcpService");

        // Wait for the user prompt.
        Console.WriteLine("\r\nPress ENTER to unmarshal the object.");
        Console.ReadLine();

        // Unmarshal the object.
        RemotingServices.Unmarshal(obj);

        // Wait for the user prompt.
        Console.WriteLine("Press ENTER to disconnect the object.");
        Console.ReadLine();

        // Disconnect the object.
        RemotingServices.Disconnect(service);
    }
}

下列程式代碼範例示範如何在上述程式代碼範例中伺服器的用戶端上實作這個類別。

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class Client
{
    public static void Main(string[] args)
    {
        // Register the TCP channel.
        ChannelServices.RegisterChannel(new TcpChannel());

        // Register the client for the remote object.
        WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(
            typeof(RemoteService),"tcp://localhost:1234/TcpService");
        RemotingConfiguration.RegisterWellKnownClientType(remoteType);

        // Create an instance of the remote object.
        RemoteService service = new RemoteService(); 

        // Invoke a method on the remote object.
        service.Hello("world");
        Console.WriteLine("Hello invoked on server.");
    }
}

下列程式代碼範例顯示伺服器和用戶端所使用的遠端物件。

using System;

// Remote object.
public class RemoteService : MarshalByRefObject
{
    private DateTime startTime;

    public RemoteService()
    {
        // Notify the user that the constructor was invoked.
        Console.WriteLine("Constructor invoked.");
        startTime = DateTime.Now;
    }

    ~RemoteService()
    {
        // Notify the user that the finalizer was invoked.
        TimeSpan elapsedTime = 
            new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
        Console.WriteLine("Finalizer invoked after " + 
            elapsedTime.ToString() + " seconds.");
    }

    public void Hello(string name)
    {
        // Print a simple message.
        Console.WriteLine("Hello, " + name);
    }
}

備註

追蹤處理程式是實作 ITrackingHandler 介面的物件,指出每當遠端基礎結構封送處理、取消封送處理或中斷物件或 Proxy 的連線時,都必須收到通知。 當目前 AppDomain 中的物件或 Proxy 封送處理、未封送處理或中斷連線時,會呼叫向 註冊TrackingServices的每個物件。

類別中的所有 TrackingServices 方法都是靜態的,並且會在目前 AppDomain的追蹤處理程式上操作。

注意

這個類別會提出連結需求。 如果立即呼叫端沒有基礎結構許可權,則會擲回 SecurityException。 如需詳細資訊 ,請參閱連結需求

建構函式

TrackingServices()

建立 TrackingServices 的執行個體。

屬性

RegisteredHandlers

取得目前在目前 TrackingServices 中註冊於 AppDomain 的追蹤處理常式的陣列。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RegisterTrackingHandler(ITrackingHandler)

TrackingServices 註冊新追蹤處理常式。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
UnregisterTrackingHandler(ITrackingHandler)

TrackingServices 將指定追蹤處理常式移除註冊。

適用於

另請參閱