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

Retrieves an object with the specified name from the CallContext.

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

Visual Basic (Declaration)
Public Shared Function GetData ( _
    name As String _
) As Object
Visual Basic (Usage)
Dim name As String
Dim returnValue As Object

returnValue = CallContext.GetData(name)
C#
public static Object GetData(
    string name
)
Visual C++
public:
static Object^ GetData(
    String^ name
)
JScript
public static function GetData(
    name : String
) : Object

Parameters

name
Type: System..::.String
The name of the item in the call context.

Return Value

Type: System..::.Object
The object in the call context associated with the specified name.
Exceptions

ExceptionCondition
SecurityException

The immediate caller does not have infrastructure permission.

Examples

The following code example demonstrates the use of the GetData 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 client class used in the sample, see the example for the CallContext class. 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.Text
Imports System.Runtime.Remoting.Messaging
Imports System.Security.Principal
Imports System.Security.Permissions

Public Class HelloServiceClass
   Inherits MarshalByRefObject

   Private Shared n_instances As Integer
   Private instanceNum As Integer  

   Public Sub New()
      n_instances += 1
      instanceNum = n_instances
      Console.WriteLine(Me.GetType().Name + " has been created.  Instance # = {0}", instanceNum)
   End Sub 'New


   Protected Overrides Sub Finalize()
      Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", instanceNum)
      MyBase.Finalize()
   End Sub 'Finalize

   <PermissionSet(SecurityAction.LinkDemand)> _
   Public Function HelloMethod(name As [String]) As [String]

      'Extract the call context data
      Dim data As LogicalCallContextData = CType(CallContext.GetData("test data"), LogicalCallContextData)
      Dim myPrincipal As IPrincipal = data.Principal

      'Check the user identity
      If myPrincipal.Identity.Name = "Bob" Then
         Console.WriteLine()
         Console.WriteLine("Hello {0}, you are identified!", myPrincipal.Identity.Name)
         Console.WriteLine(data.numOfAccesses)
      Else
         Console.WriteLine("Go away! You are not identified!")
         Return [String].Empty
      End If

      ' calculate and return result to client    
      Return "Hi there " + name + "."
   End Function 'HelloMethod

End Class 'HelloServiceClass
C#
using System;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;
using System.Security.Permissions;

public class HelloServiceClass : MarshalByRefObject {

   static int n_instances;
   int instanceNum;

   public HelloServiceClass() {
      n_instances++;
      instanceNum = n_instances;
      Console.WriteLine(this.GetType().Name + " has been created.  Instance # = {0}", instanceNum);
   }


   ~HelloServiceClass() {
      Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", instanceNum);      
   }


   [PermissionSet(SecurityAction.LinkDemand)]
   public String HelloMethod(String name) {

      //Extract the call context data
      LogicalCallContextData data = (LogicalCallContextData)CallContext.GetData("test data");      
      IPrincipal myPrincipal = data.Principal;

      //Check the user identity
      if(myPrincipal.Identity.Name == "Bob") {
         Console.WriteLine("\nHello {0}, you are identified!", myPrincipal.Identity.Name);
         Console.WriteLine(data.numOfAccesses);
      }
      else {
         Console.WriteLine("Go away! You are not identified!");
         return String.Empty;
      }

        // calculate and return result to client    
      return "Hi there " + name + ".";
   }
}
Visual C++
using namespace System;
using namespace System::Text;
using namespace System::Runtime::Remoting::Messaging;
using namespace System::Security::Principal;
using namespace System::Security::Permissions;

ref class LogicalCallContextData;

public ref class HelloServiceClass: public MarshalByRefObject
{
private:
   static int n_instances;
   int instanceNum;

public:
   HelloServiceClass()
   {
      n_instances++;
      instanceNum = n_instances;
      Console::WriteLine(  "{0} has been created.  Instance # = {1}", this->GetType()->Name, instanceNum );
   }

   ~HelloServiceClass()
   {
      Console::WriteLine( "Destroyed instance {0} of HelloServiceClass.", instanceNum );
   }

   [SecurityPermissionAttribute(SecurityAction::Demand, Flags=SecurityPermissionFlag::Infrastructure)]
   String^ HelloMethod( String^ name )
   {
      //Extract the call context data
      LogicalCallContextData^ data = dynamic_cast<LogicalCallContextData^>(CallContext::GetData( "test data" ));
      IPrincipal^ myPrincipal = data->Principal;

      //Check the user identity
      if ( myPrincipal->Identity->Name == "Bob" )
      {
         Console::WriteLine( "\nHello {0}, you are identified!", myPrincipal->Identity->Name );
         Console::WriteLine( data->numOfAccesses );
      }
      else
      {
         Console::WriteLine( "Go away! You are not identified!" );
         return String::Empty;
      }

      // calculate and return result to client    
      return String::Format( "Hi there {0}.", name );
   }
};
.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