원격 서비스 예제: 동적 게시
이 항목은 이전 버전의 기존 응용 프로그램과의 호환성을 위해 유지되고 있으나 새로운 개발에는 권장되지 않는 레거시 기술에 대해 설명합니다. 분산 응용 프로그램은 이제 WCF(Windows Communication Foundation)를 사용하여 개발됩니다.
.NET Remoting에서는 서버가 활성화한 원격으로 사용 가능한 형식의 기본 생성자만 지원합니다. 특정 생성자로 개체를 만든 다음 이를 게시하고 해당 인스턴스의 게시를 완전하게 제어하려면 인스턴스를 프로그래밍 방식으로 게시할 수 있습니다.
주의: |
---|
.NET Remoting은 기본적으로 인증이나 암호화를 수행하지 않습니다. 따라서 원격으로 클라이언트 또는 서버와 상호 작용하기 전에 해당 클라이언트 또는 서버의 ID를 확인하는 데 필요한 모든 단계를 수행하는 것이 좋습니다. .NET Remoting 응용 프로그램이 실행되려면 FullTrust 권한이 필요하기 때문에 인증되지 않은 클라이언트가 서버에 액세스할 수 있도록 허용된 경우에는 클라이언트가 완전히 신뢰된 것처럼 코드를 실행할 수 있습니다. 따라서 항상 끝점을 인증하고 통신 스트림을 암호화하십시오. 자세한 내용은 원격 서비스의 보안를 참조하십시오. |
이 샘플을 컴파일하고 실행하려면
명령 프롬프트에 다음 명령을 입력합니다.
vbc -t:library remote.vb vbc -r:System.Runtime.Remoting.dll -r:remote.dll server.vb vbc -r:System.Runtime.Remoting.dll -r:remote.dll client.vb
csc -t:library remote.cs csc -r:System.Runtime.Remoting.dll -r:remote.dll server.cs csc -r:System.Runtime.Remoting.dll -r:remote.dll client.cs
동일한 디렉터리를 가리키는 두 개의 명령 프롬프트를 엽니다. 한 프롬프트에 server를 입력하고, 다른 프롬프트에는 client를 입력합니다.
원격으로 사용 가능한 개체의 게시를 단계별로 중지하려면 서버 명령 프롬프트에서 Enter 키를 누르고 클라이언트를 다시 실행하여 단계별로 throw되는 서로 다른 예외를 관찰합니다. 이 응용 프로그램은 단일 컴퓨터에서 실행되거나 네트워크에서 실행됩니다. 네트워크를 통해 이 응용 프로그램을 실행하려면 클라이언트 구성의 **"localhost"**를 원격 컴퓨터의 이름으로 바꿉니다.
Remote
Imports System
Public Class ServiceClass
Inherits MarshalByRefObject
Private m_startTime As DateTime
Public Sub New()
Console.WriteLine("ServiceClass created without constructor. Instance hash is " & Me.GetHashCode().ToString())
m_startTime = DateTime.Now
End Sub
Overrides Protected Sub Finalize()
Console.WriteLine("I'm being collected after " & (New TimeSpan(DateTime.Now.Ticks - m_startTime.Ticks)).ToString() & " seconds.")
MyBase.Finalize()
End Sub
Public Function GetServerTime() As DateTime
Console.WriteLine("Time requested by a client.")
Return DateTime.Now
End Function
Public ReadOnly Property InstanceHash() As Integer
Get
Return Me.GetHashCode()
End Get
End Property
End Class
using System;
using System.Collections.Generic;
using System.Text;
namespace Remote
{
public class ServiceClass : MarshalByRefObject
{
private DateTime m_startTime;
public ServiceClass()
{
Console.WriteLine("ServiceClass created without constructor. Instance hash is " + GetHashCode().ToString());
m_startTime = DateTime.Now;
}
~ServiceClass()
{
Console.WriteLine("I'm being collected after " + (new TimeSpan(DateTime.Now.Ticks - m_startTime.Ticks)).ToString() + " seconds.");
}
public DateTime GetServerTime()
{
Console.WriteLine("Time requested by a client.");
return DateTime.Now;
}
public int InstanceHash
{
get { return GetHashCode(); }
}
}
}
Server
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Module Module1
Sub Main()
Dim channel As New HttpChannel(8080)
ChannelServices.RegisterChannel(channel, False)
Dim object1 As New ServiceClass()
' Creates the single instance of ServiceClass. All clients
' will use this instance.
Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri")
Console.WriteLine("ObjRef.URI: " & ref1.URI)
Console.WriteLine("Running. Press Enter to end publication.")
Console.ReadLine()
' This unregisters the object from publication, but leaves
' the channel listening.
RemotingServices.Disconnect(object1)
Console.WriteLine()
Console.WriteLine("Disconnected the object. Client now receives a RemotingException.")
Console.WriteLine("Press Enter to unregister the channel.")
Console.ReadLine()
' At this point, the ServerClass object still exists. The server
' could republish it.
' This unregisters the channel, but leaves the application
' domain running.
ChannelServices.UnregisterChannel(channel)
Console.WriteLine("Unregistered the channel. Client now receives a WebException.")
' The ServerClass object still exists. The server could
' reregister the channel and republish the object.
Console.WriteLine("The host application domain is still running. Press Enter to stop the process.")
Console.ReadLine()
' The ServiceClass object's Finalize method writes a message to
' the console. A single object will almost always succeed in
' running its Finalize method before the Console is finalized;
' in a larger application, you could ensure that all objects
' finalize before the application ends by calling the garbage
' collector and waiting.
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Module
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using Remote;
namespace Server
{
class Server
{
static void Main(string[] args)
{
HttpChannel channel = new HttpChannel(8080);
ChannelServices.RegisterChannel(channel, false);
ServiceClass object1 = new ServiceClass();
// Creates the single instance of ServiceClass. All clients
// will use this instance.
ObjRef ref1 = RemotingServices.Marshal(object1, "object1uri");
Console.WriteLine("ObjRef.URI: " + ref1.URI);
Console.WriteLine("Running. Press Enter to end publication.");
Console.ReadLine();
// This unregisters the object from publication, but leaves
// the channel listening.
RemotingServices.Disconnect(object1);
Console.WriteLine();
Console.WriteLine("Disconnected the object. Client now receives a RemotingException.");
Console.WriteLine("Press Enter to unregister the channel.");
Console.ReadLine();
// At this point, the ServerClass object still exists. The server
// could republish it.
// This unregisters the channel, but leaves the application
// domain running.
ChannelServices.UnregisterChannel(channel);
Console.WriteLine("Unregistered the channel. Client now receives a WebException.");
// The ServerClass object still exists. The server could
// reregister the channel and republish the object.
Console.WriteLine("The host application domain is still running. Press Enter to stop the process.");
Console.ReadLine();
// The ServiceClass object's Finalize method writes a message to
// the console. A single object will almost always succeed in
// running its Finalize method before the Console is finalized;
// in a larger application, you could ensure that all objects
// finalize before the application ends by calling the garbage
// collector and waiting.
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
Client
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Public Class ClientProcess
<MTAThread()> _
Public Shared Sub Main()
Dim channel As New HttpChannel(0)
ChannelServices.RegisterChannel(channel, False)
' Registers the remote class. (This could be done with a
' configuration file instead of a direct call.)
RemotingConfiguration.RegisterWellKnownClientType(Type.GetType("ServiceClass, remote"), "https://localhost:8080/object1uri")
' Instead of creating a new object, this obtains a reference
' to the server's single instance of the ServiceClass object.
Dim object1 As ServiceClass = New ServiceClass()
Try
Console.WriteLine("ServerTime: " & object1.GetServerTime())
Catch ex As Exception
Console.WriteLine("Exception of type: " & ex.GetType.ToString & " occurred.")
Console.WriteLine("Details: " & ex.Message)
End Try
End Sub
End Class
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using Remote;
namespace Client
{
public class ClientProcess
{
[MTAThread]
public static void Main(string[] args)
{
HttpChannel channel = new HttpChannel();
ChannelServices.RegisterChannel(channel, false);
// Registers the remote class. (This could be done with a
// configuration file instead of a direct call.)
RemotingConfiguration.RegisterWellKnownClientType(
Type.GetType("Remote.ServiceClass, remote"),
"https://localhost:8080/object1uri");
// Instead of creating a new object, this obtains a reference
// to the server's single instance of the ServiceClass object.
ServiceClass object1 = new ServiceClass();
try
{
Console.WriteLine("ServerTime: " + object1.GetServerTime());
}
catch (Exception ex)
{
Console.WriteLine("Exception of type: " + ex.ToString() + " occurred.");
Console.WriteLine("Details: " + ex.Message);
}
}
}
}
참고 항목
참조
RemotingServices.Marshal Method
RemotingServices.Disconnect Method
ChannelServices.UnregisterChannel Method