RealProxy Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Poskytuje základní funkce pro proxy servery.
public ref class RealProxy abstract
public abstract class RealProxy
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class RealProxy
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public abstract class RealProxy
type RealProxy = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type RealProxy = class
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type RealProxy = class
Public MustInherit Class RealProxy
- Dědičnost
-
RealProxy
- Atributy
Příklady
// Create a custom 'RealProxy'.
public ref class MyProxy: public RealProxy
{
private:
String^ myURIString;
MarshalByRefObject^ myMarshalByRefObject;
public:
MyProxy( Type^ myType )
: RealProxy( myType )
{
// RealProxy uses the Type to generate a transparent proxy.
myMarshalByRefObject = dynamic_cast<MarshalByRefObject^>(Activator::CreateInstance(myType));
// Get 'ObjRef', for transmission serialization between application domains.
ObjRef^ myObjRef = RemotingServices::Marshal( myMarshalByRefObject );
// Get the 'URI' property of 'ObjRef' and store it.
myURIString = myObjRef->URI;
Console::WriteLine( "URI :{0}", myObjRef->URI );
}
virtual IMessage^ Invoke ( IMessage^ myIMessage ) override
{
Console::WriteLine( "MyProxy.Invoke Start" );
Console::WriteLine( "" );
if ( dynamic_cast<IMethodCallMessage^>(myIMessage) )
Console::WriteLine( "IMethodCallMessage" );
if ( dynamic_cast<IMethodReturnMessage^>(myIMessage) )
Console::WriteLine( "IMethodReturnMessage" );
Type^ msgType = myIMessage->GetType();
Console::WriteLine( "Message Type: {0}", msgType );
Console::WriteLine( "Message Properties" );
IDictionary^ myIDictionary = myIMessage->Properties;
// Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
myIDictionary->default[ "__Uri" ] = myURIString;
IDictionaryEnumerator^ myIDictionaryEnumerator = dynamic_cast<IDictionaryEnumerator^>(myIDictionary->GetEnumerator());
while ( myIDictionaryEnumerator->MoveNext() )
{
Object^ myKey = myIDictionaryEnumerator->Key;
String^ myKeyName = myKey->ToString();
Object^ myValue = myIDictionaryEnumerator->Value;
Console::WriteLine( "\t{0} : {1}", myKeyName, myIDictionaryEnumerator->Value );
if ( myKeyName->Equals( "__Args" ) )
{
array<Object^>^myObjectArray = (array<Object^>^)myValue;
for ( int aIndex = 0; aIndex < myObjectArray->Length; aIndex++ )
Console::WriteLine( "\t\targ: {0} myValue: {1}", aIndex, myObjectArray[ aIndex ] );
}
if ( (myKeyName->Equals( "__MethodSignature" )) && (nullptr != myValue) )
{
array<Object^>^myObjectArray = (array<Object^>^)myValue;
for ( int aIndex = 0; aIndex < myObjectArray->Length; aIndex++ )
Console::WriteLine( "\t\targ: {0} myValue: {1}", aIndex, myObjectArray[ aIndex ] );
}
}
IMessage^ myReturnMessage;
myIDictionary->default[ "__Uri" ] = myURIString;
Console::WriteLine( "__Uri {0}", myIDictionary->default[ "__Uri" ] );
Console::WriteLine( "ChannelServices.SyncDispatchMessage" );
myReturnMessage = ChannelServices::SyncDispatchMessage( myIMessage );
// Push return value and OUT parameters back onto stack.
IMethodReturnMessage^ myMethodReturnMessage = dynamic_cast<IMethodReturnMessage^>(myReturnMessage);
Console::WriteLine( "IMethodReturnMessage.ReturnValue: {0}", myMethodReturnMessage->ReturnValue );
Console::WriteLine( "MyProxy.Invoke - Finish" );
return myReturnMessage;
}
};
// Create a custom 'RealProxy'.
public class MyProxy : RealProxy
{
String myURIString;
MarshalByRefObject myMarshalByRefObject;
public MyProxy(Type myType) : base(myType)
{
// RealProxy uses the Type to generate a transparent proxy.
myMarshalByRefObject = (MarshalByRefObject)Activator.CreateInstance((myType));
// Get 'ObjRef', for transmission serialization between application domains.
ObjRef myObjRef = RemotingServices.Marshal(myMarshalByRefObject);
// Get the 'URI' property of 'ObjRef' and store it.
myURIString = myObjRef.URI;
Console.WriteLine("URI :{0}", myObjRef.URI);
}
public override IMessage Invoke(IMessage myIMessage)
{
Console.WriteLine("MyProxy.Invoke Start");
Console.WriteLine("");
if (myIMessage is IMethodCallMessage)
Console.WriteLine("IMethodCallMessage");
if (myIMessage is IMethodReturnMessage)
Console.WriteLine("IMethodReturnMessage");
Type msgType = myIMessage.GetType();
Console.WriteLine("Message Type: {0}", msgType.ToString());
Console.WriteLine("Message Properties");
IDictionary myIDictionary = myIMessage.Properties;
// Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
myIDictionary["__Uri"] = myURIString;
IDictionaryEnumerator myIDictionaryEnumerator =
(IDictionaryEnumerator) myIDictionary.GetEnumerator();
while (myIDictionaryEnumerator.MoveNext())
{
Object myKey = myIDictionaryEnumerator.Key;
String myKeyName = myKey.ToString();
Object myValue = myIDictionaryEnumerator.Value;
Console.WriteLine("\t{0} : {1}", myKeyName,
myIDictionaryEnumerator.Value);
if (myKeyName == "__Args")
{
Object[] myObjectArray = (Object[])myValue;
for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex,
myObjectArray[aIndex]);
}
if ((myKeyName == "__MethodSignature") && (null != myValue))
{
Object[] myObjectArray = (Object[])myValue;
for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex,
myObjectArray[aIndex]);
}
}
IMessage myReturnMessage;
myIDictionary["__Uri"] = myURIString;
Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);
Console.WriteLine("ChannelServices.SyncDispatchMessage");
myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);
// Push return value and OUT parameters back onto stack.
IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)
myReturnMessage;
Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}",
myMethodReturnMessage.ReturnValue);
Console.WriteLine("MyProxy.Invoke - Finish");
return myReturnMessage;
}
}
' Create a custom 'RealProxy'.
Public Class MyProxy
Inherits RealProxy
Private myURIString As String
Private myMarshalByRefObject As MarshalByRefObject
<PermissionSet(SecurityAction.LinkDemand)> _
Public Sub New(ByVal myType As Type)
MyBase.New(myType)
' RealProxy uses the Type to generate a transparent proxy.
myMarshalByRefObject = CType(Activator.CreateInstance(myType), MarshalByRefObject)
' Get 'ObjRef', for transmission serialization between application domains.
Dim myObjRef As ObjRef = RemotingServices.Marshal(myMarshalByRefObject)
' Get the 'URI' property of 'ObjRef' and store it.
myURIString = myObjRef.URI
Console.WriteLine("URI :{0}", myObjRef.URI)
End Sub
<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.Infrastructure)> _
Public Overrides Function Invoke(ByVal myIMessage As IMessage) As IMessage
Console.WriteLine("MyProxy.Invoke Start")
Console.WriteLine("")
If TypeOf myIMessage Is IMethodCallMessage Then
Console.WriteLine("IMethodCallMessage")
End If
If TypeOf myIMessage Is IMethodReturnMessage Then
Console.WriteLine("IMethodReturnMessage")
End If
Dim msgType As Type
msgType = CObj(myIMessage).GetType
Console.WriteLine("Message Type: {0}", msgType.ToString())
Console.WriteLine("Message Properties")
Dim myIDictionary As IDictionary = myIMessage.Properties
' Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
myIDictionary("__Uri") = myURIString
Dim myIDictionaryEnumerator As IDictionaryEnumerator = CType(myIDictionary.GetEnumerator(), _
IDictionaryEnumerator)
While myIDictionaryEnumerator.MoveNext()
Dim myKey As Object = myIDictionaryEnumerator.Key
Dim myKeyName As String = myKey.ToString()
Dim myValue As Object = myIDictionaryEnumerator.Value
Console.WriteLine(ControlChars.Tab + "{0} : {1}", myKeyName, myIDictionaryEnumerator.Value)
If myKeyName = "__Args" Then
Dim myObjectArray As Object() = CType(myValue, Object())
Dim aIndex As Integer
For aIndex = 0 To myObjectArray.Length - 1
Console.WriteLine(ControlChars.Tab + ControlChars.Tab + "arg: {0} myValue: {1}", _
aIndex, myObjectArray(aIndex))
Next aIndex
End If
If myKeyName = "__MethodSignature" And Not Nothing Is myValue Then
Dim myObjectArray As Object() = CType(myValue, Object())
Dim aIndex As Integer
For aIndex = 0 To myObjectArray.Length - 1
Console.WriteLine(ControlChars.Tab + ControlChars.Tab + "arg: {0} myValue: {1}", _
aIndex, myObjectArray(aIndex))
Next aIndex
End If
End While
Dim myReturnMessage As IMessage
myIDictionary("__Uri") = myURIString
Console.WriteLine("__Uri {0}", myIDictionary("__Uri"))
Console.WriteLine("ChannelServices.SyncDispatchMessage")
myReturnMessage = ChannelServices.SyncDispatchMessage(CObj(myIMessage))
' Push return value and OUT parameters back onto stack.
Dim myMethodReturnMessage As IMethodReturnMessage = CType(myReturnMessage, IMethodReturnMessage)
Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}", myMethodReturnMessage.ReturnValue)
Console.WriteLine("MyProxy.Invoke - Finish")
Return myReturnMessage
End Function 'Invoke
End Class
Poznámky
Třída RealProxy je abstract
základní třída, ze které musí být odvozeny proxy servery.
Klient, který používá objekt napříč jakýmkoli druhem hranice vzdálené komunikace, ve skutečnosti používá transparentní proxy pro objekt. Transparentní proxy poskytuje dojem, že skutečný objekt se nachází v prostoru klienta. Dosahuje toho přesměrováním volání provedených na skutečný objekt pomocí infrastruktury vzdálené komunikace.
Transparentní proxy server je sám o sobě umístěn instancí spravované třídy modulu runtime typu RealProxy. Implementuje RealProxy část funkce, která je nutná k předávání operací z transparentního proxy serveru. Všimněte si, že objekt proxy dědí přidruženou sémantiku spravovaných objektů, jako je uvolňování paměti, podpora polí a metod, a lze ji rozšířit na formování nových tříd. Proxy má duální povahu: funguje jako objekt stejné třídy jako vzdálený objekt (transparentní proxy) a je to samotný spravovaný objekt.
Objekt proxy lze použít bez ohledu na jakékoli dílčí dělení vzdálené komunikace v rámci AppDomain.
Poznámka
Tato třída vytváří požadavek na propojení a požadavek dědičnosti na úrovni třídy. Vyvolá SecurityException se, když buď okamžitý volající, nebo odvozená třída nemá oprávnění infrastruktury. Podrobnosti o požadavcích na zabezpečení najdete v tématu Požadavky na propojení a požadavky dědičnosti.
Poznámky pro implementátory
Při dědění z RealProxy, musíte přepsat metodu Invoke(IMessage) .
Konstruktory
RealProxy() |
Inicializuje novou instanci třídy RealProxy s výchozími hodnotami. |
RealProxy(Type) |
Inicializuje novou instanci RealProxy třídy, která představuje vzdálený objekt zadaného Type. |
RealProxy(Type, IntPtr, Object) |
Inicializuje novou instanci RealProxy třídy. |
Metody
AttachServer(MarshalByRefObject) |
Připojí aktuální instanci proxy k zadané vzdálené MarshalByRefObject. |
CreateObjRef(Type) |
ObjRef Vytvoří pro zadaný typ objektu a zaregistruje ji v infrastruktuře vzdálené komunikace jako objekt aktivovaný klientem. |
DetachServer() |
Odpojte aktuální instanci proxy serveru od objektu vzdáleného serveru, který představuje. |
Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu. (Zděděno od Object) |
GetCOMIUnknown(Boolean) |
Požaduje nespravovaný odkaz na objekt reprezentovaný aktuální instancí proxy serveru. |
GetHashCode() |
Slouží jako výchozí funkce hash. (Zděděno od Object) |
GetObjectData(SerializationInfo, StreamingContext) |
Přidá transparentní proxy objekt reprezentovaný aktuální instancí RealProxy zadané SerializationInfo. |
GetProxiedType() |
Type Vrátí objekt, který představuje aktuální instanceRealProxy. |
GetStubData(RealProxy) |
Načte zástupná data uložená pro zadaný proxy server. |
GetTransparentProxy() |
Vrátí transparentní proxy server pro aktuální instanci RealProxy. |
GetType() |
Type Získá aktuální instanci. (Zděděno od Object) |
GetUnwrappedServer() |
Vrátí objekt serveru, který je reprezentován aktuální instancí proxy. |
InitializeServerObject(IConstructionCallMessage) |
Inicializuje novou instanci objektu Type vzdáleného objektu, který aktuální instance RealProxy představuje se zadaným IConstructionCallMessage. |
Invoke(IMessage) |
Při přepsání v odvozené třídě vyvolá metodu, která je zadána v poskytnutém IMessage vzdáleném objektu, který je reprezentován aktuální instancí. |
MemberwiseClone() |
Vytvoří použádnou kopii aktuálního souboru Object. (Zděděno od Object) |
SetCOMIUnknown(IntPtr) |
Uloží nespravovaný proxy objektu, který je reprezentován aktuální instancí. |
SetStubData(RealProxy, Object) |
Nastaví data zástupné procedury pro zadaný proxy server. |
SupportsInterface(Guid) |
Vyžádá si rozhraní MODELU COM se zadaným ID. |
ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |