CallContext 類別

定義

提供一組隨執行程式碼路徑攜帶的屬性。 無法繼承這個類別。

public ref class CallContext sealed
[System.Serializable]
public sealed class CallContext
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class CallContext
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public sealed class CallContext
[<System.Serializable>]
type CallContext = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CallContext = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type CallContext = class
Public NotInheritable Class CallContext
繼承
CallContext
屬性

範例

以下程式碼範例示範如何利用類別 CallContext主物件與身份物件 傳送至遠端地點以進行識別。 若要查看本範例中使用的 LogicalCallContextData 類別程式碼,請參閱介面範例 ILogicalThreadAffinative 。 若要查看本範例中使用的 HelloServiceClass 類別程式碼,請參閱該方法的範例 GetData 。 若要查看本範例中使用的伺服器類別程式碼,請參閱該 RegisterActivatedServiceType 類別的範例。

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

End Class

備註

CallContext 是一個專門的集合物件,類似於執行緒本地儲存(Thread Local Storage)用於方法呼叫,並提供對每個執行邏輯執行緒獨一無二的資料槽。 這些時隙不會在其他邏輯執行緒的呼叫上下文間共享。 物件可以在執行程式碼路徑上上下移動時加入 CallContext ,並由路徑上的各種物件檢查。

當遠端方法呼叫到另一個 AppDomain物件時,該 CallContext 類別會 LogicalCallContext 產生一個與遠端呼叫一同傳送的實例。 只有暴露ILogicalThreadAffinative介面且儲存在 的CallContext物件才會被傳播到AppDomainLogicalCallContext外部。 不支援此介面的物件不會在 LogicalCallContext 有遠端方法呼叫的實例中傳送。

備註

所有方法在 中 CallContext 都是靜態的,並對當前 Thread的呼叫上下文操作。

備註

這個類別會產生連結需求。 若直接呼叫者沒有基礎設施權限,則會拋出 A SecurityException

屬性

名稱 Description
HostContext

取得或設定與目前執行緒相關的主機上下文。

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FreeNamedDataSlot(String)

清空一個指定名稱的資料槽。

GetData(String)

從 中取得指定名稱 CallContext的物件。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetHeaders()

回傳隨方法呼叫一同傳送的標頭。

GetType()

取得目前實例的 Type

(繼承來源 Object)
LogicalGetData(String)

從邏輯呼叫上下文中取得指定名稱的物件。

LogicalSetData(String, Object)

將特定物件儲存在邏輯呼叫上下文中,並將其與指定的名稱關聯。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
SetData(String, Object)

儲存給定物件並將其與指定名稱關聯。

SetHeaders(Header[])

設定隨方法呼叫一同傳送的標頭。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱