다음을 통해 공유


ITrackingHandler 인터페이스

구현 개체가 원격 인프라에 의해 마샬링, 역마샬링 및 개체와 프록시의 연결 끊기에 대한 알림을 받아야 한다는 것을 나타냅니다.

네임스페이스: System.Runtime.Remoting.Services
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
Public Interface ITrackingHandler
‘사용 방법
Dim instance As ITrackingHandler
[ComVisibleAttribute(true)] 
public interface ITrackingHandler
[ComVisibleAttribute(true)] 
public interface class ITrackingHandler
/** @attribute ComVisibleAttribute(true) */ 
public interface ITrackingHandler
ComVisibleAttribute(true) 
public interface ITrackingHandler

설명

현재 AppDomain의 개체나 프록시가 마샬링되거나, 역마샬링되거나, 연결이 끊어질 때 TrackingServices에 등록된 모든 개체는 원격으로 호출됩니다.

개체만 연결이 끊어질 수 있습니다. 프록시에서 연결이 끊어지면 예외가 throw됩니다.

참고

이 인터페이스는 링크 요청을 만듭니다. 직접 실행 호출자가 참조에서 인터페이스까지 호출하며 인프라 권한을 가지지 않는 경우 SecurityException이 throw됩니다. 자세한 내용은 링크 요청을 참조하십시오.

예제

다음 코드 예제에서는 원격 인프라에서 알림을 받도록 ITrackingHandler 인터페이스의 메서드를 구현하는 방법을 보여 줍니다.

다음 코드 예제에서는 이 인터페이스를 구현하는 방법을 보여 줍니다.

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.

    [System.Security.Permissions.SecurityPermissionAttribute(
     System.Security.Permissions.SecurityAction.LinkDemand,
     Flags=System.Security.Permissions.SecurityPermissionFlag.Infrastructure)]
    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.
    [System.Security.Permissions.SecurityPermissionAttribute(
     System.Security.Permissions.SecurityAction.LinkDemand,
     Flags=System.Security.Permissions.SecurityPermissionFlag.Infrastructure)]
    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.
    [System.Security.Permissions.SecurityPermissionAttribute(
     System.Security.Permissions.SecurityAction.LinkDemand,
     Flags=System.Security.Permissions.SecurityPermissionFlag.Infrastructure)]
    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;
using System.Security.Permissions;

public class Server
{
[SecurityPermission(SecurityAction.Demand)]
    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.Diagnostics;
using System.Reflection;
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;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;

// 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 destructor was invoked.
        TimeSpan elapsedTime = 
            new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
        Console.WriteLine("Destructor invoked after " + 
            elapsedTime.ToString() + " seconds.");
    }

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

.NET Framework 보안

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

ITrackingHandler 멤버
System.Runtime.Remoting.Services 네임스페이스