次の方法で共有


RemotingConfiguration.RegisterWellKnownServiceType メソッド (Type, String, WellKnownObjectMode)

既知の型として、サービス エンドでオブジェクト Type を登録し、特定のパラメータを使用して WellKnownServiceTypeEntry の新しいインスタンスを初期化します。

Overloads Public Shared Sub RegisterWellKnownServiceType( _
   ByVal type As Type, _   ByVal objectUri As String, _   ByVal mode As WellKnownObjectMode _)
[C#]
public static void RegisterWellKnownServiceType(Typetype,stringobjectUri,WellKnownObjectModemode);
[C++]
public: static void RegisterWellKnownServiceType(Type* type,String* objectUri,WellKnownObjectModemode);
[JScript]
public static function RegisterWellKnownServiceType(
   type : Type,objectUri : String,mode : WellKnownObjectMode);

パラメータ

  • type
    オブジェクト Type
  • objectUri
    オブジェクトの URI。
  • mode
    登録されている既知のオブジェクト型のアクティベーション モード。 WellKnownObjectMode のトピックを参照してください。

例外

例外の種類 条件
SecurityException コールスタックの上位にある 1 つ以上の呼び出し元に、リモート処理の型とチャネルを構成するためのアクセス許可がありません。

解説

登録された既知のオブジェクトの URI を知っているすべてのクライアントは、 ChannelServices で優先するチャネルを登録し、New または Activator.GetObject を呼び出してそのオブジェクトをアクティブにすることにより、このオブジェクト用にプロキシを取得できます。New で既知のオブジェクトをアクティブにするには、初めに RegisterWellKnownClientType メソッドを使用して、その既知のオブジェクトの型をクライアント側で登録しておく必要があります。 RegisterWellKnownClientType メソッドを呼び出すと、リモート オブジェクトの場所がリモート処理インフラストラクチャに通知され、New キーワードでオブジェクトを作成できるようになります。一方、 Activator.GetObject メソッドを使用して、既知のオブジェクトをアクティブにする場合は、そのオブジェクトの URL を引数として指定する必要があります。このため、クライアント エンドでの事前の登録は必要ありません。

呼び出しがサーバーに到達すると、.NET Framework がメッセージから URI を抽出し、リモート処理テーブルをチェックしてこの URI に一致するオブジェクトの参照を検索し、必要に応じてそのオブジェクトをインスタンス化して、このメソッドの呼び出しをオブジェクトに転送します。オブジェクトが SingleCall として登録されている場合は、このメソッドの呼び出しが完了した後で、オブジェクトが破棄されます。メソッドが呼び出されるたびに、オブジェクトの新しいインスタンスが作成されます。 Activator.GetObject と New の唯一の相違点は、前者は URL をパラメータとして指定できるのに対して、後者は URL を構成から取得する点です。

リモート オブジェクト自体は、登録処理によってインスタンス化されることはありません。これはクライアントがオブジェクト上のメソッドを呼び出そうとしたり、クライアント側からオブジェクトをアクティブにしたりする場合にだけ発生します。

既知のオブジェクトの詳細については、「 サーバー アクティベーション 」を参照してください。

使用例

[Visual Basic, C#, C++] 既知のオブジェクト型としてサーバーでオブジェクト型を登録する例を次に示します。ここで示すサーバー コードに対応するクライアント コードについては、 RegisterWellKnownClientType メソッドのトピックの例を参照してください。

 
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp


Public Class ServerClass
   
   Public Shared Sub Main()

    . . . 
      ChannelServices.RegisterChannel(New TcpChannel(8082))
      
      RemotingConfiguration.ApplicationName = "HelloServiceApplication"
      
      RemotingConfiguration.RegisterWellKnownServiceType(GetType(HelloService), "MyUri", WellKnownObjectMode.SingleCall)
    . . . 

      Console.WriteLine("Press enter to stop this process.")
      Console.ReadLine()

   End Sub 'Main

End Class 'ServerClass

[C#] 
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class ServerClass {

    public static void Main()  {

    . . . 
        ChannelServices.RegisterChannel(new TcpChannel(8082));

        RemotingConfiguration.ApplicationName = "HelloServiceApplication";

        RemotingConfiguration.RegisterWellKnownServiceType( typeof(HelloService),
                                                            "MyUri",
                                                            WellKnownObjectMode.SingleCall 
                                                          );
    . . . 

        Console.WriteLine("Press enter to stop this process.");
        Console.ReadLine();
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.runtime.remoting.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;

int main() 
{
    . . . 
    ChannelServices::RegisterChannel(new TcpChannel(8082));

    RemotingConfiguration::ApplicationName = S"HelloServiceApplication";

    RemotingConfiguration::RegisterWellKnownServiceType(__typeof(HelloService),
        S"MyUri",
        WellKnownObjectMode::SingleCall);
    . . . 

    Console::WriteLine(S"Press enter to stop this process.");
    Console::ReadLine();
    return 0;
}

[Visual Basic, C#, C++] 上のサンプル コードで登録したサービス オブジェクトを次の例に示します。

 
Imports System


Public Class HelloService
   Inherits MarshalByRefObject
   
   Private Shared n_instances As Integer
     
   Public Sub New()
      n_instances += 1
      Console.WriteLine("")
      Console.WriteLine("HelloService activated - instance # {0}.", n_instances)
   End Sub 'New
   
   
   Protected Overrides Sub Finalize()
      Console.WriteLine("HelloService instance {0} destroyed.", n_instances)
      n_instances -= 1
      MyBase.Finalize()
   End Sub 'Finalize
   
   
   Public Function HelloMethod(name As [String]) As [String]
      Console.WriteLine("HelloMethod called on HelloService instance {0}.", n_instances)
      Return "Hi there " + name + "."
   End Function 'HelloMethod

End Class 'HelloService

[C#] 
using System;

public class HelloService : MarshalByRefObject {

    static int n_instances;

    public HelloService() {
        n_instances++;
        Console.WriteLine("");
        Console.WriteLine("HelloService activated - instance # {0}.", n_instances);
    }


    ~HelloService()  {
        Console.WriteLine("HelloService instance {0} destroyed.", n_instances);
        n_instances--;
    }


    public String HelloMethod(String name)  {

        Console.WriteLine("HelloMethod called on HelloService instance {0}.", n_instances);
        return "Hi there " + name + ".";
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;

public __gc class HelloService : public MarshalByRefObject 
{
    static int n_instances;

public:
    HelloService() 
    {
        n_instances++;
        Console::WriteLine(S"");
        Console::WriteLine(S"HelloService activated - instance # {0}.", __box(n_instances));
    }

    ~HelloService() 
    {
        Console::WriteLine(S"HelloService instance {0} destroyed.", __box(n_instances));
        n_instances--;
    }

public:
    String* HelloMethod(String* name) 
    {
        Console::WriteLine(S"HelloMethod called on HelloService instance {0}.", __box(n_instances));
        return String::Format(S"Hi there {0}.", name);
    }
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

RemotingConfiguration クラス | RemotingConfiguration メンバ | System.Runtime.Remoting 名前空間 | RemotingConfiguration.RegisterWellKnownServiceType オーバーロードの一覧 | WellKnownServiceTypeEntry | WellKnownClientTypeEntry