방법: 클라이언트 활성화 형식의 인스턴스 만들기

이 항목은 이전 버전의 기존 응용 프로그램과의 호환성을 위해 유지되고 있으나 새로운 개발에는 권장되지 않는 레거시 기술에 대해 설명합니다. 분산 응용 프로그램은 이제 WCF(Windows Communication Foundation)를 사용하여 개발됩니다.

이 기사에서는 클라이언트가 활성화한 개체를 인스턴스화하는 두 가지 방법을 설명합니다. 첫 번째 방법은 CreateInstance를 사용하고 두 번째 방법은 new 연산자를 사용합니다.

Activator.CreateInstance를 사용하여 인스턴스 만들기

  1. TcpChannel를 만들고 등록

    Dim channel As TcpChannel = New TcpChannel()
     ChannelServices.RegisterChannel(channel, False)
    
    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel, false);
    
  2. 클라이언트 활성 개체 등록

    RemotingConfiguration.RegisterActivatedClientType( _
        GetType(MyRemoteObject), _
        "tcp://localhost:1234/MyServer")
    
    RemotingConfiguration.RegisterActivatedClientType(
        typeof(MyRemoteObject),
        "tcp://localhost:1234/MyServer");
    
  3. CreateInstance 호출

    Dim url() As Object = {New UrlAttribute("tcp://localhost:1234/Server")}
    Dim obj As MyRemoteObject = CType(Activator.CreateInstance( _
        GetType(MyRemoteObject), _
        Nothing, _
        url), MyRemoteObject)
    
    object[] url = { new UrlAttribute("tcp://localhost:1234/Server") };
    MyRemoteObject obj = (MyRemoteObject)Activator.CreateInstance(
        typeof(MyRemoteObject),
        null,
        url);
    

New 연산자를 사용하여 인스턴스 만들기

  1. 채널을 만들고 등록

    Dim channel As TcpChannel = New TcpChannel()
     ChannelServices.RegisterChannel(channel, False)
    
    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel, false);
    
  2. 클라이언트 활성 개체 등록

    RemotingConfiguration.RegisterActivatedClientType( _
        GetType(MyRemoteObject), _
        "tcp://localhost:1234/MyServer")
    
    RemotingConfiguration.RegisterActivatedClientType(
        typeof(MyRemoteObject),
        "tcp://localhost:1234/MyServer");
    
  3. new 연산자 호출

    Dim obj As MyRemoteObject = New MyRemoteObject(123)
    
    MyRemoteObject obj = new MyRemoteObject(123);
    

예제

다음 코드에서는 클라이언트 활성 인스턴스를 만드는 두 방법을 보여 줍니다.

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Activation
Imports Server

Module Client

    Sub Main()
        ' Create and register a channel
        Dim channel As TcpChannel = New TcpChannel()
        ChannelServices.RegisterChannel(channel, False)

  ' Register the client activated object
        RemotingConfiguration.RegisterActivatedClientType( _
            GetType(MyRemoteObject), _
            "tcp://localhost:1234/MyServer")

  ' Call Activator.CreateInstance
    Dim obj As MyRemoteObject = CType(Activator.CreateInstance( _
       GetType(MyRemoteObject), _
       Nothing, _
           url), MyRemoteObject)
   
        ' OR call operator new
        Dim obj As MyRemoteObject = New MyRemoteObject(123)

        Console.WriteLine("Client.Main(): GetValue returned: {0}", obj.GetValue())
        Console.WriteLine("Client.Main(): Calling SetValue(10)")
        obj.SetValue(10)
        Console.WriteLine("Client.Main(): GetValue returned: {0}", obj.GetValue())
    End Sub

End Module
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Server;

namespace Client
{
    class Client
    {
        static void Main(string[] args)
        {
// Create and register channel
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);

// Register client activated object
            RemotingConfiguration.RegisterActivatedClientType(
                typeof(MyRemoteObject),
                "tcp://localhost:1234/MyServer");

// Call Activator.CreateInstance
object[] url = { new  UrlAttribute("tcp://localhost:1234/Server") };
         MyRemoteObject obj = (MyRemoteObject)Activator.CreateInstance(
            typeof(MyRemoteObject),
            null,
               url);
      // OR call operator new
            MyRemoteObject obj = new MyRemoteObject(123);

            Console.WriteLine("Client.Main(): GetValue returned: " + obj.GetValue());
            Console.WriteLine("Client.Main(): Calling SetValue(10)");
            obj.SetValue(10);
            Console.WriteLine("Client.Main(): GetValue returned: " + obj.GetValue());
        }
    }
}

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

참고 항목

개념

원격 개체 활성화
원격 응용 프로그램 구성
서버 활성화
수명 임대
클라이언트 활성화

빌드 날짜: 2010-02-13