Bagikan melalui


CallContext.GetData(String) Metode

Definisi

Mengambil objek dengan nama yang ditentukan dari CallContext.

public:
 static System::Object ^ GetData(System::String ^ name);
public static object GetData (string name);
[System.Security.SecurityCritical]
public static object GetData (string name);
static member GetData : string -> obj
[<System.Security.SecurityCritical>]
static member GetData : string -> obj
Public Shared Function GetData (name As String) As Object

Parameter

name
String

Nama item dalam konteks panggilan.

Mengembalikan

Objek dalam konteks panggilan yang terkait dengan nama yang ditentukan.

Atribut

Pengecualian

Penelepon langsung tidak memiliki izin infrastruktur.

Contoh

Contoh kode berikut menunjukkan penggunaan GetData metode untuk mengirimkan Objek Utama dan Identitas ke lokasi jarak jauh untuk identifikasi. Untuk melihat kode untuk kelas yang LogicalCallContextData digunakan dalam sampel ini, lihat contoh antarmuka ILogicalThreadAffinative . Untuk melihat kode untuk kelas klien yang digunakan dalam sampel, lihat contoh untuk CallContext kelas . Untuk melihat kode untuk kelas server yang digunakan dalam sampel ini, lihat contoh untuk RegisterActivatedServiceType kelas .

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 );
   }
};
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);
   }

   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 + ".";
   }
}
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

   
   Protected Overrides Sub Finalize()
      Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", instanceNum)
      MyBase.Finalize()
   End Sub
  
   <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

Berlaku untuk