원격 서비스 예: 추적 서비스
TrackingServices 클래스에서는 일반적인 추적 서비스에 대해 플러그형 추적 처리기를 제공합니다. ITrackingHandler 인터페이스의 메서드는 다음 경우에 호출됩니다.
자세한 내용은 참조 설명서의 TrackingServices 및 ITrackingHandler를 참조하십시오.
경고
.NET Framework Remoting에서는 기본적으로 인증이나 암호화 작업을 수행하지 않습니다. 따라서 원격으로 클라이언트나 서버와 상호 작용하기 전에 클라이언트나 서버의 ID를 확인하는 데 필요한 모든 단계를 수행하는 것이 좋습니다. .NET Framework Remoting 응용 프로그램을 실행하려면 FullTrust 권한이 필요하므로 권한이 없는 클라이언트에게 서버에 대한 액세스 권한을 부여하면 해당 클라이언트는 완전 신뢰 상태처럼 코드를 실행할 수 있습니다. IIS(인터넷 정보 서비스)에서 원격화된 형식을 호스팅하거나 사용자 지정 채널 싱크 쌍을 만들어서 항상 끝점을 인증하고 통신 스트림을 암호화하십시오.
이 샘플을 컴파일하고 실행하려면
명령 프롬프트에서 다음 명령을 입력합니다.
csc /t:library TrackingHandler.cs
csc /r:System.Runtime.Remoting.dll /t:library /out:ServiceClass.dll serviceclass.cs
csc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll client.cs
csc /r:System.Runtime.Remoting.dll /r:TrackingHandler.dll /r:ServiceClass.dll server.cs
동일한 디렉터리를 가리키는 두 개의 명령 프롬프트를 엽니다. 한 명령 프롬프트에는 server를 입력합니다. 다른 명령 프롬프트에는 client를 입력합니다.
이 응용 프로그램은 한 컴퓨터나 네트워크에서 실행됩니다. 이 응용 프로그램을 네트워크에서 실행하려는 경우 클라이언트 구성의 **"localhost"**를 원격 컴퓨터의 이름으로 바꿔야 합니다.
TrackingHandler.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Services;
public class TrackingHandler : ITrackingHandler{
// Notifies a handler that an object has been marshaled.
public void MarshaledObject(Object obj, ObjRef or){
Console.WriteLine("Tracking: An instance of {0} was marshaled. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString());
Console.WriteLine("ObjRef dump:");
if (or.ChannelInfo != null){
Console.WriteLine(" -- ChannelInfo: ");
DumpChannelInfo(or.ChannelInfo);
}
if (or.EnvoyInfo != null)
Console.WriteLine(" -- EnvoyInfo: " + or.EnvoyInfo.ToString());
if (or.TypeInfo != null){
Console.WriteLine(" -- TypeInfo: " + or.TypeInfo.ToString());
Console.WriteLine(" -- " + or.TypeInfo.TypeName);
}
if (or.URI != null)
Console.WriteLine(" -- URI: " + or.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());
}
}
Server.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Services;
public class ServerProcess{
public static void Main(string[] Args){
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
TrackingServices.RegisterTrackingHandler(new 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);
}
}
Client.cs
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 ClientProcess{
public static void Main(string[] Args){
ChannelServices.RegisterChannel(new TcpChannel());
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());
}
}
ServiceClass.cs
using System;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
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;
}
}
참고 항목
참조
ITrackingHandler
TrackingServices