Share via


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 実装するオブジェクトであり、リモート処理インフラストラクチャがオブジェクトまたはプロキシをマーシャリング、アンマーシャリング、または切断するたびに通知を受け取る必要があることを示します。 に登録 TrackingServices されているすべてのオブジェクトは、現在 AppDomain のオブジェクトまたはプロキシがマーシャリング、マーシャリング解除、または切断されたときにリモート処理によって呼び出されます。

クラス内 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 から、指定した追跡ハンドラーの登録を解除します。

適用対象

こちらもご覧ください