Condividi tramite


Procedura: creare un'istanza di un tipo attivato dal client

Questo argomento è specifico di una tecnologia legacy mantenuta per una questione di compatibilità con le applicazioni esistenti di versioni precedenti e non è consigliato per il nuovo sviluppo. Le applicazioni distribuite devono ora essere sviluppate utilizzando  Windows Communication Foundation (WCF).

In questo articolo sono illustrate due modalità di creazione di un'istanza di un oggetto attivato dal client. Nel primo metodo viene utilizzato CreateInstance, nel secondo, il nuovo operatore.

Creare un'istanza utilizzando Activator.CreateInstance

  1. Creare e registrare un TcpChannel

    Dim channel As TcpChannel = New TcpChannel()
     ChannelServices.RegisterChannel(channel, False)
    
    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel, false);
    
  2. Registrare l'oggetto attivato dal client

    RemotingConfiguration.RegisterActivatedClientType( _
        GetType(MyRemoteObject), _
        "tcp://localhost:1234/MyServer")
    
    RemotingConfiguration.RegisterActivatedClientType(
        typeof(MyRemoteObject),
        "tcp://localhost:1234/MyServer");
    
  3. Chiamare il metodo 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);
    

Creare un'istanza utilizzando l'operatore new.

  1. Creare e registrare un canale

    Dim channel As TcpChannel = New TcpChannel()
     ChannelServices.RegisterChannel(channel, False)
    
    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel, false);
    
  2. Registrare l'oggetto attivato dal client

    RemotingConfiguration.RegisterActivatedClientType( _
        GetType(MyRemoteObject), _
        "tcp://localhost:1234/MyServer")
    
    RemotingConfiguration.RegisterActivatedClientType(
        typeof(MyRemoteObject),
        "tcp://localhost:1234/MyServer");
    
  3. Chiamare l'operatore new

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

Esempio

Nel codice seguente sono illustrati entrambi i metodi di creazione di un'istanza attivata dal client:

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());
        }
    }
}

Compilazione del codice

L'esempio presenta i requisiti seguenti:

Vedere anche

Concetti

Attivazione di oggetti remoti
Configurazione di applicazioni remote
Attivazione del server
Lease di durata
Attivazione da client

Data di compilazione: 2010-02-13