CallContext クラス

実行コード パスと共に渡されるプロパティのセットを提供します。このクラスは継承できません。

この型のすべてのメンバの一覧については、CallContext メンバ を参照してください。

System.Object
   System.Runtime.Remoting.Messaging.CallContext

<Serializable>
NotInheritable Public Class CallContext
[C#]
[Serializable]
public sealed class CallContext
[C++]
[Serializable]
public __gc __sealed class CallContext
[JScript]
public
   Serializable
class CallContext

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

CallContext は、メソッド呼び出しのスレッド ローカル ストレージに似た専用のコレクション オブジェクトで、実行の各論理スレッドに対して一意のデータ スロットを提供します。そのスロットは、他の論理スレッドの呼び出しコンテキスト間で共有されません。実行コード パスの走査およびバックアップ時に、オブジェクトが CallContext に追加され、パスに沿ってさまざまなオブジェクトでチェックされることがあります。

別の AppDomain 内のオブジェクトに対してリモート メソッド呼び出しが実行された場合、 CallContext クラスは、そのリモート呼び出しと共に転送される LogicalCallContext インスタンスを生成します。 ILogicalThreadAffinative インターフェイスを公開し、 CallContext に格納されるオブジェクトだけが、 LogicalCallContextAppDomain の外部に反映されます。このインターフェイスをサポートしていないオブジェクトは、リモート メソッド呼び出しと共に LogicalCallContext インスタンスで送信されることはありません。

メモ    CallContext のすべてのメソッドは静的で、現在の Thread の呼び出しコンテキストで動作します。

メモ   このクラスはリンク確認要求を行います。直前の呼び出し元にインフラストラクチャ アクセス許可がない場合、SecurityException がスローされます。詳細については、「 リンク確認要求 」を参照してください。

使用例

[Visual Basic, C#, C++] CallContext クラスを使用して、識別のためにリモートの場所に プリンシパル オブジェクトと ID オブジェクト を送信する例を次のコードに示します。サンプルで使用する LogicalCallContextData クラスのコードを表示するには、 ILogicalThreadAffinative インターフェイスのトピックの例を参照してください。サンプルで使用する HelloServiceClass クラスのコードを表示するには、 GetData メソッドのトピックの例を参照してください。サンプルで使用するサーバー クラスのコードを表示するには、 RegisterActivatedServiceType クラスのトピックの例を参照してください。

 
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Messaging
Imports System.Security.Principal


Public Class ClientClass
      
   Public Shared Sub Main()
      
      Dim ident As New GenericIdentity("Bob")
      Dim prpal As New GenericPrincipal(ident, New String() {"Level1"})
      Dim data As New LogicalCallContextData(prpal)
      
      'Enter data into the CallContext
      CallContext.SetData("test data", data)
      
      
      Console.WriteLine(data.numOfAccesses)
      
      ChannelServices.RegisterChannel(New TcpChannel())
      
      RemotingConfiguration.RegisterActivatedClientType(GetType(HelloServiceClass), "tcp://localhost:8082")
      
      Dim service As New HelloServiceClass()
      
      If service Is Nothing Then
         Console.WriteLine("Could not locate server.")
         Return
      End If
      
      
      ' call remote method
      Console.WriteLine()
      Console.WriteLine("Calling remote object")
      Console.WriteLine(service.HelloMethod("Caveman"))
      Console.WriteLine(service.HelloMethod("Spaceman"))
      Console.WriteLine(service.HelloMethod("Bob"))
      Console.WriteLine("Finished remote object call")
      Console.WriteLine()
      
      'Extract the returned data from the call context
      Dim returnedData As LogicalCallContextData = CType(CallContext.GetData("test data"), LogicalCallContextData)
      
      Console.WriteLine(data.numOfAccesses)
      Console.WriteLine(returnedData.numOfAccesses)

   End Sub 'Main

End Class 'ClientClass

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

public class ClientClass {

   public static void Main() {
      
      GenericIdentity ident = new GenericIdentity("Bob");
      GenericPrincipal prpal = new GenericPrincipal(ident, 
                                                    new string[] {"Level1"});
      LogicalCallContextData data = new LogicalCallContextData(prpal);      
      
      //Enter data into the CallContext
      CallContext.SetData("test data", data);

      
      Console.WriteLine(data.numOfAccesses);

      ChannelServices.RegisterChannel(new TcpChannel());

      RemotingConfiguration.RegisterActivatedClientType(typeof(HelloServiceClass),
                                                        "tcp://localhost:8082");

      HelloServiceClass service = new HelloServiceClass();

      if(service == null) {
          Console.WriteLine("Could not locate server.");
          return;
      }


      // call remote method
      Console.WriteLine();
      Console.WriteLine("Calling remote object");
      Console.WriteLine(service.HelloMethod("Caveman"));
      Console.WriteLine(service.HelloMethod("Spaceman"));
      Console.WriteLine(service.HelloMethod("Bob"));
      Console.WriteLine("Finished remote object call");
      Console.WriteLine();

      //Extract the returned data from the call context
      LogicalCallContextData returnedData = 
         (LogicalCallContextData)CallContext.GetData("test data");

      Console.WriteLine(data.numOfAccesses);
      Console.WriteLine(returnedData.numOfAccesses);
   }
}

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

#using <service.dll>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Tcp;
using namespace System::Runtime::Remoting::Messaging;
using namespace System::Security::Principal;

int main() 
{
    GenericIdentity* ident = new GenericIdentity(S"Bob");
    String* id __gc[] = new String* __gc[1];
    id[0] = S"Level1";

    GenericPrincipal* prpal = new GenericPrincipal(ident, id );
    LogicalCallContextData* data = new LogicalCallContextData(prpal);      

    //Enter data into the CallContext
    CallContext::SetData(S"test data", data);

    Console::WriteLine(data->numOfAccesses);

    ChannelServices::RegisterChannel(new TcpChannel());

    RemotingConfiguration::RegisterActivatedClientType(__typeof(HelloServiceClass),
        S"tcp://localhost:8082");

    HelloServiceClass* service = new HelloServiceClass();

    if (service == 0) 
    {
        Console::WriteLine(S"Could not locate server.");
        return 0;
    }

    // call remote method
    Console::WriteLine();
    Console::WriteLine(S"Calling remote Object*");
    Console::WriteLine(service->HelloMethod(S"Caveman"));
    Console::WriteLine(service->HelloMethod(S"Spaceman"));
    Console::WriteLine(service->HelloMethod(S"Bob"));
    Console::WriteLine(S"Finished remote Object* call");
    Console::WriteLine();

    //Extract the returned data from the call context
    LogicalCallContextData* returnedData = 
        static_cast<LogicalCallContextData*>(CallContext::GetData(S"test data"));

    Console::WriteLine(data->numOfAccesses);
    Console::WriteLine(returnedData->numOfAccesses);

    return 0;
}

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

必要条件

名前空間: System.Runtime.Remoting.Messaging

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

アセンブリ: Mscorlib (Mscorlib.dll 内)

.NET Framework セキュリティ:

参照

CallContext メンバ | System.Runtime.Remoting.Messaging 名前空間 | Header