TrackingServices 类
定义
提供一种注册、注销和获取跟踪处理程序列表的方法。Provides a way to register, unregister, and obtain a list of tracking handlers.
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 来注册和注销跟踪处理程序。The following code example shows how to use the methods of the TrackingServices class to register and unregister tracking handlers.
下面的代码示例演示如何实现跟踪处理程序。The following code example shows how to implement a tracking handler.
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());
}
}
下面的代码示例演示如何在服务器上实现此类。The following code example shows how to implement this class on a server.
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);
}
}
下面的代码示例演示如何在上一个代码示例中为服务器的客户端实现此类。The following code example shows how to implement this class on a client for the server in the preceding code example.
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.");
}
}
下面的代码示例显示了服务器和客户端使用的远程对象。The following code example shows the remote object that is used by the server and the client.
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);
}
}
注解
跟踪处理程序是实现接口的对象 ITrackingHandler ,表示当远程处理基础结构对对象或代理进行封送处理、取消封送或断开连接时,必须通知它们。Tracking handlers are objects that implement the ITrackingHandler interface, indicating that they must be notified whenever the remoting infrastructure marshals, unmarshals, or disconnects an object or proxy. 使用注册的每个对象在 TrackingServices 当前中的对象或代理 AppDomain 被封送、取消封送或断开连接时调用。Every object that is registered with TrackingServices is called by remoting when an object or proxy in the current AppDomain is marshaled, unmarshaled, or disconnected.
类中的所有方法 TrackingServices 都是静态的,并操作当前中的跟踪处理程序 AppDomain 。All methods in the TrackingServices class are static and operate on the tracking handlers in the current AppDomain.
备注
此类发出链接请求。This class makes a link demand. 如果直接调用方没有基础结构权限,则会引发 SecurityException。A SecurityException is thrown if the immediate caller does not have infrastructure permission. 有关详细信息,请参阅 链接要求 。See Link Demands for more information.
构造函数
| TrackingServices() |
创建 TrackingServices的实例。Creates an instance of TrackingServices. |
属性
| RegisteredHandlers |
获取当前 TrackingServices 中目前已注册到 AppDomain 的跟踪处理程序的数组。Gets an array of the tracking handlers that are currently registered with TrackingServices in the current AppDomain. |
方法
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| RegisterTrackingHandler(ITrackingHandler) |
将新的跟踪处理程序注册到 TrackingServices。Registers a new tracking handler with the TrackingServices. |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
| UnregisterTrackingHandler(ITrackingHandler) |
从 TrackingServices 注销指定的跟踪处理程序。Unregisters the specified tracking handler from TrackingServices. |