.NET Framework Class Library
CallContext..::.SetData Method

Stores a given object and associates it with the specified name.

Namespace:  System.Runtime.Remoting.Messaging
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Shared Sub SetData ( _
    name As String, _
    data As Object _
)
Visual Basic (Usage)
Dim name As String
Dim data As Object

CallContext.SetData(name, data)
C#
public static void SetData(
    string name,
    Object data
)
Visual C++
public:
static void SetData(
    String^ name, 
    Object^ data
)
JScript
public static function SetData(
    name : String, 
    data : Object
)

Parameters

name
Type: System..::.String
The name with which to associate the new item in the call context.
data
Type: System..::.Object
The object to store in the call context.
Exceptions

ExceptionCondition
SecurityException

The immediate caller does not have infrastructure permission.

Examples

The following code example demonstrates the use of the SetData method to transmit Principal and Identity Objects to a remote location for identification. To view the code for the LogicalCallContextData class used in this sample, see the example for the ILogicalThreadAffinative interface. To view the code for the HelloServiceClass class used in this sample, see the example for the GetData method. To view the code for the server class used in this sample, see the example for the RegisterActivatedServiceType class.

Visual Basic
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
Imports System.Security.Permissions


Public Class ClientClass
   <PermissionSet(SecurityAction.LinkDemand)> _
   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;
using System.Security.Permissions;

public class ClientClass {
   [PermissionSet(SecurityAction.LinkDemand)]
   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);
   }
}
Visual C++
#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 = gcnew GenericIdentity( "Bob" );
   array<String^>^id = gcnew array<String^>(1);
   id[ 0 ] = "Level1";
   GenericPrincipal^ prpal = gcnew GenericPrincipal( ident,id );
   LogicalCallContextData ^ data = gcnew LogicalCallContextData( prpal );

   //Enter data into the CallContext
   CallContext::SetData( "test data", data );
   Console::WriteLine( data->numOfAccesses );
   ChannelServices::RegisterChannel( gcnew TcpChannel );
   RemotingConfiguration::RegisterActivatedClientType( HelloServiceClass::typeid, "tcp://localhost:8082" );
   HelloServiceClass ^ service = gcnew HelloServiceClass;
   if ( service == nullptr )
   {
      Console::WriteLine( "Could not locate server." );
      return 0;
   }

   // 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 = static_cast<LogicalCallContextData ^>(CallContext::GetData( "test data" ));
   Console::WriteLine( data->numOfAccesses );
   Console::WriteLine( returnedData->numOfAccesses );
   return 0;
}
CPP_OLD
#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;
}
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker