RemotingConfiguration.RegisterWellKnownServiceType Metodo

Definizione

Registra un oggetto Type sul lato servizio come tipo noto (a chiamata singola o Singleton).

Overload

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

Registra un oggetto Type registrato nell'oggetto WellKnownServiceTypeEntry fornito sul lato servizio come tipo noto.

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

Registra un oggetto Type sul lato servizio come tipo noto utilizzando i parametri specificati per inizializzare una nuova istanza della classe WellKnownServiceTypeEntry.

RegisterWellKnownServiceType(WellKnownServiceTypeEntry)

Registra un oggetto Type registrato nell'oggetto WellKnownServiceTypeEntry fornito sul lato servizio come tipo noto.

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)

Parametri

entry
WellKnownServiceTypeEntry

Impostazioni di configurazione per il tipo noto.

Eccezioni

Almeno uno dei chiamanti in posizione più elevata nello stack di chiamate non dispone delle autorizzazioni alla configurazione di canali e tipi remoti.

Esempio

#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

Commenti

Qualsiasi client che conosce l'URI di un oggetto noto registrato può ottenere un proxy per l'oggetto registrando il canale che preferisce con ChannelServicese attivando l'oggetto chiamando new o il Activator.GetObject metodo . Per attivare un oggetto noto con new, è innanzitutto necessario registrare il tipo di oggetto noto nel client usando il RegisterWellKnownClientType metodo . La chiamata al metodo fornisce all'infrastruttura RegisterWellKnownClientType remota la posizione dell'oggetto remoto, che consente alla new parola chiave di crearla. Se invece si usa il Activator.GetObject metodo per attivare l'oggetto noto, è necessario specificarlo con l'URL dell'oggetto come argomento, quindi non è necessaria alcuna registrazione precedente sul lato client.

Quando la chiamata arriva al server, il .NET Framework estrae l'URI dal messaggio, esamina le tabelle di comunicazione remota per individuare il riferimento per l'oggetto che corrisponde all'URI e quindi crea un'istanza dell'oggetto, se necessario, inoltrando la chiamata al metodo all'oggetto . Se l'oggetto viene registrato come SingleCall, viene eliminato definitivamente dopo il completamento della chiamata al metodo. Viene creata una nuova istanza dell'oggetto per ogni metodo chiamato. L'unica differenza tra Activator.GetObject e new è che il primo consente di specificare un URL come parametro e quest'ultimo ottiene l'URL dalla configurazione.

L'oggetto remoto stesso non viene creata un'istanza dal processo di registrazione. Ciò si verifica solo quando un client tenta di chiamare un metodo sull'oggetto o attiva l'oggetto dal lato client.

Per una descrizione dettagliata degli oggetti noti, vedere Attivazione del server.

Vedi anche

Si applica a

RegisterWellKnownServiceType(Type, String, WellKnownObjectMode)

Registra un oggetto Type sul lato servizio come tipo noto utilizzando i parametri specificati per inizializzare una nuova istanza della classe 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)

Parametri

type
Type

Oggetto Type.

objectUri
String

URI dell'oggetto.

mode
WellKnownObjectMode

Modalità di attivazione del tipo di oggetto noto di cui è in corso la registrazione. Per informazioni, vedere WellKnownObjectMode.

Eccezioni

Almeno uno dei chiamanti in posizione più elevata nello stack di chiamate non dispone delle autorizzazioni alla configurazione di canali e tipi remoti.

Esempio

Nell'esempio di codice seguente viene illustrata la registrazione di un tipo di oggetto nel server come tipo di oggetto noto. Per il codice client che corrisponde al codice del server presentato, vedere l'esempio per il RegisterWellKnownClientType metodo .

#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

Nell'esempio di codice seguente viene illustrato l'oggetto servizio registrato nel codice di esempio precedente.

#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

Commenti

Qualsiasi client che conosce l'URI di un oggetto noto registrato può ottenere un proxy per l'oggetto registrando il canale che preferisce con ChannelServicese attivando l'oggetto chiamando new o il Activator.GetObject metodo . Per attivare un oggetto noto con new, è innanzitutto necessario registrare il tipo di oggetto noto nel client usando il RegisterWellKnownClientType metodo . La chiamata al metodo fornisce all'infrastruttura RegisterWellKnownClientType remota la posizione dell'oggetto remoto, che consente alla new parola chiave di crearla. Se invece si usa il Activator.GetObject metodo per attivare l'oggetto noto, è necessario specificarlo con l'URL dell'oggetto come argomento, quindi non è necessaria alcuna registrazione precedente sul lato client.

Quando la chiamata arriva al server, il .NET Framework estrae l'URI dal messaggio, esamina le tabelle di comunicazione remota per individuare il riferimento per l'oggetto che corrisponde all'URI e quindi crea un'istanza dell'oggetto, se necessario, inoltrando la chiamata al metodo all'oggetto . Se l'oggetto viene registrato come SingleCall, viene eliminato definitivamente dopo il completamento della chiamata al metodo. Viene creata una nuova istanza dell'oggetto per ogni metodo chiamato. L'unica differenza tra Activator.GetObject e new è che il primo consente di specificare un URL come parametro e quest'ultimo ottiene l'URL dalla configurazione.

L'oggetto remoto stesso non viene creata un'istanza dal processo di registrazione. Ciò si verifica solo quando un client tenta di chiamare un metodo sull'oggetto o attiva l'oggetto dal lato client.

Per una descrizione dettagliata degli oggetti noti, vedere Attivazione del server.

Vedi anche

Si applica a