CallContext.SetData メソッド

特定のオブジェクトを格納し、指定した名前に関連付けます。

Public Shared Sub SetData( _
   ByVal name As String, _   ByVal data As Object _)
[C#]
public static void SetData(stringname,objectdata);
[C++]
public: static void SetData(String* name,Object* data);
[JScript]
public static function SetData(
   name : String,data : Object);

パラメータ

  • name
    呼び出しコンテキストで新しい項目に関連付ける名前。
  • data
    呼び出しコンテキストに格納するオブジェクト。

例外

例外の種類 条件
SecurityException 直前の呼び出し元に、インフラストラクチャ アクセス許可がありません。

使用例

[Visual Basic, C#, C++] SetData メソッドを使用して、識別のためにリモートの場所に プリンシパル オブジェクトと 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

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

.NET Framework セキュリティ:

参照

CallContext クラス | CallContext メンバ | System.Runtime.Remoting.Messaging 名前空間