方法 : クライアント側でアクティブ化される型のインスタンスを作成する
このトピックの対象は、既存のアプリケーションとの下位互換性のために残されているレガシ テクノロジに特定されています。新規の開発には、このトピックを適用しないでください。分散アプリケーションは、現在は Windows Communication Foundation (WCF) を使用して開発する必要があります。
ここでは、クライアント側でアクティブ化されるオブジェクトのインスタンス化を行うための 2 つの方法を示します。最初の方法では、CreateInstance を使用し、2 つ目の方法では、新しい演算子を使用します。
Activator.CreateInstance を使用したインスタンスの作成
TcpChannel を作成して登録します。
Dim channel As TcpChannel = New TcpChannel() ChannelServices.RegisterChannel(channel, False)
TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, false);
クライアント側でアクティブ化されるオブジェクトを登録します。
RemotingConfiguration.RegisterActivatedClientType( _ GetType(MyRemoteObject), _ "tcp://localhost:1234/MyServer")
RemotingConfiguration.RegisterActivatedClientType( typeof(MyRemoteObject), "tcp://localhost:1234/MyServer");
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);
新しい演算子を使用したインスタンスの作成
チャネルを作成して登録します。
Dim channel As TcpChannel = New TcpChannel() ChannelServices.RegisterChannel(channel, False)
TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, false);
クライアント側でアクティブ化されるオブジェクトを登録します。
RemotingConfiguration.RegisterActivatedClientType( _ GetType(MyRemoteObject), _ "tcp://localhost:1234/MyServer")
RemotingConfiguration.RegisterActivatedClientType( typeof(MyRemoteObject), "tcp://localhost:1234/MyServer");
新しい演算子を呼び出します。
Dim obj As MyRemoteObject = New MyRemoteObject(123)
MyRemoteObject obj = new MyRemoteObject(123);
例
次のコードは、クライアント側でアクティブ化されるインスタンスを作成するための 2 つの方法を示しています。
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());
}
}
}
コードのコンパイル方法
この例で必要な要素は次のとおりです。
- System 名前空間と System.Runtime.Remoting 名前空間への参照、および MyRemoteObject を実装する名前空間
参照
概念
リモート オブジェクトのアクティベーション
リモート アプリケーションの構成
サーバー アクティベーション
有効期間リース
クライアント アクティベーション
ビルド日:2010-02-13