Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel, false);
    
  2. Register the client-activated object

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

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

Create an Instance using the New Operator

  1. Create and register a channel

    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel, false);
    
  2. Register the client-activated object

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

    MyRemoteObject obj = new MyRemoteObject(123);
    

Example

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

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

Compiling the Code

This example requires:

See Also

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.