Share via


How to: Create an Instance of a Client-Activated Type

This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the  Windows Communication Foundation (WCF).

This article shows two ways of instantiating a client-activated object. The first method uses CreateInstance, the second uses the new operator.

Create an instance using Activator.CreateInstance

  1. Create and register a TcpChannel

    Dim channel As TcpChannel = New TcpChannel()
     ChannelServices.RegisterChannel(channel, False)
    
  2. Register the client-activated object

    RemotingConfiguration.RegisterActivatedClientType( _
        GetType(MyRemoteObject), _
        "tcp://localhost:1234/MyServer")
    
  3. Call CreateInstance

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

Create an Instance using the New Operator

  1. Create and register a channel

    Dim channel As TcpChannel = New TcpChannel()
     ChannelServices.RegisterChannel(channel, False)
    
  2. Register the client-activated object

    RemotingConfiguration.RegisterActivatedClientType( _
        GetType(MyRemoteObject), _
        "tcp://localhost:1234/MyServer")
    
  3. Call the new operator

    Dim obj As MyRemoteObject = New MyRemoteObject(123)
    

Example

The following code illustrates both methods of creating a client-activated instance:

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

Compiling the Code

This example requires:

See Also

Concepts

Activation of Remote Objects
Configuration of Remote Applications
Server Activation
Lifetime Leases
Client Activation