RemotingConfiguration.RegisterWellKnownServiceType Método

Definición

Registra un Type de objeto en el servicio como objeto de tipo conocido (llamada única o singleton).

Sobrecargas

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

Registra un Type de objeto guardado en el WellKnownServiceTypeEntry proporcionado en el servicio como un tipo conocido.

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

Registra un Type de objeto en el servicio como un tipo conocido mediante parámetros especificados para inicializar una nueva instancia de WellKnownServiceTypeEntry.

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

Registra un Type de objeto guardado en el WellKnownServiceTypeEntry proporcionado en el servicio como un tipo conocido.

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)

Parámetros

entry
WellKnownServiceTypeEntry

Valores de configuración del tipo conocido.

Excepciones

Al menos uno de los llamadores situados en la parte superior de la pila de llamadas no tiene permiso para configurar los tipos y canales de comunicación remota.

Ejemplos

#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

Comentarios

Cualquier cliente que conozca el URI de un objeto conocido registrado puede obtener un proxy para el objeto registrando el canal que prefiere con ChannelServicesy activando el objeto llamando al new método o Activator.GetObject . Para activar un objeto conocido con new, primero debe registrar el tipo de objeto conocido en el cliente mediante el RegisterWellKnownClientType método . Llamar al método proporciona a la RegisterWellKnownClientType infraestructura de comunicación remota la ubicación del objeto remoto, lo que permite que la new palabra clave la cree. Por otro lado, si usa el Activator.GetObject método para activar el objeto conocido, debe proporcionarlo con la dirección URL del objeto como argumento, por lo que no es necesario registrar previamente en el extremo del cliente.

Cuando llega la llamada al servidor, .NET Framework extrae el URI del mensaje, examina las tablas de comunicación remota para buscar la referencia del objeto que coincide con el URI y, a continuación, crea una instancia del objeto si es necesario, reenviando la llamada al método al objeto. Si el objeto se registra como SingleCall, se destruye una vez completada la llamada al método. Se crea una nueva instancia del objeto para cada método llamado. La única diferencia entre Activator.GetObject y new es que la primera permite especificar una dirección URL como parámetro y la última obtiene la dirección URL de la configuración.

El proceso de registro no crea instancias del propio objeto remoto. Esto solo sucede cuando un cliente intenta llamar a un método en el objeto o activa el objeto desde el lado cliente.

Para obtener una descripción detallada de los objetos conocidos, consulte Activación del servidor.

Consulte también

Se aplica a

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

Registra un Type de objeto en el servicio como un tipo conocido mediante parámetros especificados para inicializar una nueva instancia 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)

Parámetros

type
Type

Type de objeto.

objectUri
String

URI del objeto.

mode
WellKnownObjectMode

El modo de activación del tipo de objeto conocido que se va a registrar. (Consulte WellKnownObjectMode).

Excepciones

Al menos uno de los llamadores situados en la parte superior de la pila de llamadas no tiene permiso para configurar los tipos y canales de comunicación remota.

Ejemplos

En el ejemplo de código siguiente se muestra el registro de un tipo de objeto en el servidor como un tipo de objeto conocido. Para el código de cliente que corresponde al código de servidor presentado, vea el ejemplo del RegisterWellKnownClientType método .

#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

En el ejemplo de código siguiente se muestra el objeto de servicio registrado en el código de ejemplo anterior.

#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

Comentarios

Cualquier cliente que conozca el URI de un objeto conocido registrado puede obtener un proxy para el objeto registrando el canal que prefiere con ChannelServicesy activando el objeto llamando al new método o Activator.GetObject . Para activar un objeto conocido con new, primero debe registrar el tipo de objeto conocido en el cliente mediante el RegisterWellKnownClientType método . Llamar al método proporciona a la RegisterWellKnownClientType infraestructura de comunicación remota la ubicación del objeto remoto, lo que permite que la new palabra clave la cree. Por otro lado, si usa el Activator.GetObject método para activar el objeto conocido, debe proporcionarlo con la dirección URL del objeto como argumento, por lo que no es necesario registrar previamente en el extremo del cliente.

Cuando llega la llamada al servidor, .NET Framework extrae el URI del mensaje, examina las tablas de comunicación remota para buscar la referencia del objeto que coincide con el URI y, a continuación, crea una instancia del objeto si es necesario, reenviando la llamada al método al objeto. Si el objeto se registra como SingleCall, se destruye una vez completada la llamada al método. Se crea una nueva instancia del objeto para cada método llamado. La única diferencia entre Activator.GetObject y new es que la primera permite especificar una dirección URL como parámetro y la última obtiene la dirección URL de la configuración.

El proceso de registro no crea instancias del propio objeto remoto. Esto solo sucede cuando un cliente intenta llamar a un método en el objeto o activa el objeto desde el lado cliente.

Para obtener una descripción detallada de los objetos conocidos, consulte Activación del servidor.

Consulte también

Se aplica a