RemotingConfiguration.RegisterWellKnownServiceType Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Inscrit un objet Type à la fin du service en tant que type connu (appel unique ou singleton).
Surcharges
| Nom | Description |
|---|---|
| RegisterWellKnownServiceType(WellKnownServiceTypeEntry) |
Inscrit un objet enregistré dans l’objet Type fourni WellKnownServiceTypeEntry à la fin du service en tant que type connu. |
| RegisterWellKnownServiceType(Type, String, WellKnownObjectMode) |
Inscrit un objet Type sur la fin du service en tant que type connu, à l’aide des paramètres donnés pour initialiser une nouvelle instance de WellKnownServiceTypeEntry. |
RegisterWellKnownServiceType(WellKnownServiceTypeEntry)
Inscrit un objet enregistré dans l’objet Type fourni WellKnownServiceTypeEntry à la fin du service en tant que type connu.
public:
static void RegisterWellKnownServiceType(System::Runtime::Remoting::WellKnownServiceTypeEntry ^ entry);
public static void RegisterWellKnownServiceType(System.Runtime.Remoting.WellKnownServiceTypeEntry entry);
static member RegisterWellKnownServiceType : System.Runtime.Remoting.WellKnownServiceTypeEntry -> unit
Public Shared Sub RegisterWellKnownServiceType (entry As WellKnownServiceTypeEntry)
Paramètres
Paramètres de configuration pour le type connu.
Exceptions
Au moins un des appelants plus haut dans la pile d’appels n’est pas autorisé à configurer les types et canaux de communication à distance.
Exemples
#using <system.dll>
#using <system.runtime.remoting.dll>
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;
using namespace System::Runtime::Remoting::Messaging;
using namespace System::Runtime::Serialization;
public ref class RemoteObject: public MarshalByRefObject
{
public:
void Method1( LocalObject^ param )
{
Console::WriteLine( "Invoked: Method1( {0})", param );
}
};
int main()
{
ChannelServices::RegisterChannel( gcnew HttpChannel( 8090 ) );
WellKnownServiceTypeEntry^ wkste = gcnew WellKnownServiceTypeEntry( RemoteObject::typeid,"RemoteObject",WellKnownObjectMode::Singleton );
RemotingConfiguration::RegisterWellKnownServiceType( wkste );
RemoteObject^ RObj = dynamic_cast<RemoteObject^>(Activator::GetObject( RemoteObject::typeid, "http://localhost:8090/RemoteObject" ));
LocalObject^ LObj = gcnew LocalObject;
RObj->Method1( LObj );
Console::WriteLine( "Press Return to exit..." );
Console::ReadLine();
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization;
using System.Security.Permissions;
public class ObjRefExample {
public static void Main() {
ChannelServices.RegisterChannel(new HttpChannel(8090));
WellKnownServiceTypeEntry wkste =
new WellKnownServiceTypeEntry(typeof(RemoteObject),
"RemoteObject",
WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType( wkste );
RemoteObject RObj =
(RemoteObject)Activator.GetObject(typeof(RemoteObject),
"http://localhost:8090/RemoteObject");
LocalObject LObj = new LocalObject();
RObj.Method1( LObj );
Console.WriteLine("Press Return to exit...");
Console.ReadLine();
}
}
public class RemoteObject : MarshalByRefObject {
public void Method1(LocalObject param) {
Console.WriteLine("Invoked: Method1({0})", param);
}
}
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Serialization
Imports System.Security.Permissions
' code that drives the example
Public Class ObjRefExample
<PermissionSet(SecurityAction.LinkDemand)> _
Public Overloads Shared Sub Main()
ChannelServices.RegisterChannel(New HttpChannel(8090))
RemotingConfiguration.RegisterWellKnownServiceType(New WellKnownServiceTypeEntry(GetType(RemoteObject), "RemoteObject", WellKnownObjectMode.Singleton))
Dim RObj As RemoteObject = CType(Activator.GetObject(GetType(RemoteObject), "http://localhost:8090/RemoteObject"), RemoteObject)
Dim LObj As New LocalObject()
RObj.Method1(LObj)
Console.WriteLine("Press Return to exit...")
Console.ReadLine()
End Sub
End Class
' a simple remote object
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class RemoteObject
Inherits MarshalByRefObject
Public Sub Method1(ByVal param As LocalObject)
Console.WriteLine("Invoked: Method1({0})", param)
End Sub
End Class
Remarques
Tout client qui connaît l’URI d’un objet bien connu inscrit peut obtenir un proxy pour l’objet en inscrivant le canal avec lequel il préfère ChannelServiceset en activant l’objet en appelant new ou la Activator.GetObject méthode. Pour activer un objet connu avec new, vous devez d’abord inscrire le type d’objet connu sur le client à l’aide de la RegisterWellKnownClientType méthode. L’appel de la RegisterWellKnownClientType méthode donne à l’infrastructure de communication à distance l’emplacement de l’objet distant, ce qui permet au new mot clé de le créer. Si, d’autre part, vous utilisez la Activator.GetObject méthode pour activer l’objet bien connu, vous devez le fournir avec l’URL de l’objet en tant qu’argument, donc aucune inscription préalable à la fin du client n’est nécessaire.
Lorsque l’appel arrive sur le serveur, le .NET Framework extrait l’URI du message, examine les tables de communication à distance pour rechercher la référence de l’objet qui correspond à l’URI, puis instancie l’objet si nécessaire, en transférant l’appel de méthode à l’objet. Si l’objet est inscrit en tant que SingleCall, il est détruit une fois l’appel de méthode terminé. Une nouvelle instance de l’objet est créée pour chaque méthode appelée. La seule différence entre Activator.GetObject et new c’est que l’ancien vous permet de spécifier une URL en tant que paramètre, et ce dernier obtient l’URL de la configuration.
L’objet distant lui-même n’est pas instancié par le processus d’inscription. Cela se produit uniquement lorsqu’un client tente d’appeler une méthode sur l’objet ou active l’objet du côté client.
Voir aussi
S’applique à
RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)
Inscrit un objet Type sur la fin du service en tant que type connu, à l’aide des paramètres donnés pour initialiser une nouvelle instance de WellKnownServiceTypeEntry.
public:
static void RegisterWellKnownServiceType(Type ^ type, System::String ^ objectUri, System::Runtime::Remoting::WellKnownObjectMode mode);
public static void RegisterWellKnownServiceType(Type type, string objectUri, System.Runtime.Remoting.WellKnownObjectMode mode);
static member RegisterWellKnownServiceType : Type * string * System.Runtime.Remoting.WellKnownObjectMode -> unit
Public Shared Sub RegisterWellKnownServiceType (type As Type, objectUri As String, mode As WellKnownObjectMode)
Paramètres
- objectUri
- String
URI de l’objet.
- mode
- WellKnownObjectMode
Mode d’activation du type d’objet connu inscrit. (Voir WellKnownObjectMode.)
Exceptions
Au moins un des appelants plus haut dans la pile d’appels n’est pas autorisé à configurer les types et canaux de communication à distance.
Exemples
L’exemple de code suivant illustre l’inscription d’un type d’objet sur le serveur comme type d’objet connu. Pour le code client qui correspond au code du serveur présenté, consultez l’exemple de la RegisterWellKnownClientType méthode.
#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;
int main()
{
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
public class ServerClass {
public static void Main() {
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class ServerClass
Public Shared Sub Main()
ChannelServices::RegisterChannel( gcnew TcpChannel( 8082 ) );
RemotingConfiguration::ApplicationName = "HelloServiceApplication";
RemotingConfiguration::RegisterWellKnownServiceType( HelloService::typeid,
"MyUri",
WellKnownObjectMode::SingleCall );
ChannelServices.RegisterChannel(new TcpChannel(8082));
RemotingConfiguration.ApplicationName = "HelloServiceApplication";
RemotingConfiguration.RegisterWellKnownServiceType( typeof(HelloService),
"MyUri",
WellKnownObjectMode.SingleCall
);
ChannelServices.RegisterChannel(New TcpChannel(8082))
RemotingConfiguration.ApplicationName = "HelloServiceApplication"
RemotingConfiguration.RegisterWellKnownServiceType(GetType(HelloService), "MyUri", WellKnownObjectMode.SingleCall)
Console::WriteLine( "Press enter to stop this process." );
Console::ReadLine();
return 0;
}
Console.WriteLine("Press enter to stop this process.");
Console.ReadLine();
}
}
Console.WriteLine("Press enter to stop this process.")
Console.ReadLine()
End Sub
End Class
L’exemple de code suivant montre l’objet de service inscrit dans l’exemple de code ci-dessus.
#using <system.dll>
using namespace System;
public ref class HelloService: public MarshalByRefObject
{
private:
static int n_instances;
public:
HelloService()
{
n_instances++;
Console::WriteLine( "" );
Console::WriteLine( "HelloService activated - instance # {0}.", n_instances );
}
~HelloService()
{
Console::WriteLine( "HelloService instance {0} destroyed.", n_instances );
n_instances--;
}
String^ HelloMethod( String^ name )
{
Console::WriteLine( "HelloMethod called on HelloService instance {0}.", n_instances );
return String::Format( "Hi there {0}.", name );
}
};
using System;
public class HelloService : MarshalByRefObject {
static int n_instances;
public HelloService() {
n_instances++;
Console.WriteLine("");
Console.WriteLine("HelloService activated - instance # {0}.", n_instances);
}
~HelloService() {
Console.WriteLine("HelloService instance {0} destroyed.", n_instances);
n_instances--;
}
public String HelloMethod(String name) {
Console.WriteLine("HelloMethod called on HelloService instance {0}.", n_instances);
return "Hi there " + name + ".";
}
}
Public Class HelloService
Inherits MarshalByRefObject
Private Shared n_instances As Integer
Public Sub New()
n_instances += 1
Console.WriteLine("")
Console.WriteLine("HelloService activated - instance # {0}.", n_instances)
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("HelloService instance {0} destroyed.", n_instances)
n_instances -= 1
MyBase.Finalize()
End Sub
Public Function HelloMethod(name As [String]) As [String]
Console.WriteLine("HelloMethod called on HelloService instance {0}.", n_instances)
Return "Hi there " + name + "."
End Function 'HelloMethod
End Class
Remarques
Tout client qui connaît l’URI d’un objet bien connu inscrit peut obtenir un proxy pour l’objet en inscrivant le canal avec lequel il préfère ChannelServiceset en activant l’objet en appelant new ou la Activator.GetObject méthode. Pour activer un objet connu avec new, vous devez d’abord inscrire le type d’objet connu sur le client à l’aide de la RegisterWellKnownClientType méthode. L’appel de la RegisterWellKnownClientType méthode donne à l’infrastructure de communication à distance l’emplacement de l’objet distant, ce qui permet au new mot clé de le créer. Si, d’autre part, vous utilisez la Activator.GetObject méthode pour activer l’objet bien connu, vous devez le fournir avec l’URL de l’objet en tant qu’argument, donc aucune inscription préalable à la fin du client n’est nécessaire.
Lorsque l’appel arrive sur le serveur, le .NET Framework extrait l’URI du message, examine les tables de communication à distance pour rechercher la référence de l’objet qui correspond à l’URI, puis instancie l’objet si nécessaire, en transférant l’appel de méthode à l’objet. Si l’objet est inscrit en tant que SingleCall, il est détruit une fois l’appel de méthode terminé. Une nouvelle instance de l’objet est créée pour chaque méthode appelée. La seule différence entre Activator.GetObject et new c’est que l’ancien vous permet de spécifier une URL en tant que paramètre, et ce dernier obtient l’URL de la configuration.
L’objet distant lui-même n’est pas instancié par le processus d’inscription. Cela se produit uniquement lorsqu’un client tente d’appeler une méthode sur l’objet ou active l’objet du côté client.