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 是類似于方法呼叫之執行緒本機儲存體的特殊集合物件,並提供每個邏輯執行緒執行唯一的資料位置。 這些位置不會在其他邏輯執行緒上的呼叫內容之間共用。 物件可以加入至 CallContext ,因為它會向下移動並備份執行程式碼路徑,並由路徑上的各種物件檢查。

當遠端方法呼叫另一個 中的 AppDomain 物件時,類別 CallContext 會產生一個 LogicalCallContext 與遠端呼叫一起移動的實例。 只有公開 ILogicalThreadAffinative 介面且儲存在 中的 CallContext 物件會傳播到 中的 LogicalCallContext 外部 AppDomain 。 不支援此介面的物件不會在具有遠端方法呼叫的實例中 LogicalCallContext 傳輸。

注意

中的所有 CallContext 方法都是靜態的,並在目前 Thread 的呼叫內容上操作。

注意

這個類別會要求連結。 SecurityException如果立即呼叫端沒有基礎結構許可權,則會擲回 。 如需詳細資訊 ,請參閱連結需求

屬性

HostContext

取得或設定與目前執行緒相關聯的主機內容。

方法

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)

適用於

另請參閱