HttpChannel Classe

Definizione

Implementa un canale client per chiamate remote che utilizza il protocollo HTTP per la trasmissione dei messaggi.

public ref class HttpChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::IChannelReceiverHook, System::Runtime::Remoting::Channels::IChannelSender
public ref class HttpChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::IChannelReceiverHook, System::Runtime::Remoting::Channels::IChannelSender, System::Runtime::Remoting::Channels::ISecurableChannel
public class HttpChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.IChannelReceiverHook, System.Runtime.Remoting.Channels.IChannelSender
public class HttpChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.IChannelReceiverHook, System.Runtime.Remoting.Channels.IChannelSender, System.Runtime.Remoting.Channels.ISecurableChannel
type HttpChannel = class
    inherit BaseChannelWithProperties
    interface IChannelReceiver
    interface IChannelSender
    interface IChannel
    interface IChannelReceiverHook
type HttpChannel = class
    inherit BaseChannelWithProperties
    interface IChannelReceiver
    interface IChannelSender
    interface IChannel
    interface IChannelReceiverHook
    interface ISecurableChannel
type HttpChannel = class
    inherit BaseChannelWithProperties
    interface IChannelReceiver
    interface IChannel
    interface IChannelSender
    interface IChannelReceiverHook
    interface ISecurableChannel
Public Class HttpChannel
Inherits BaseChannelWithProperties
Implements IChannelReceiver, IChannelReceiverHook, IChannelSender
Public Class HttpChannel
Inherits BaseChannelWithProperties
Implements IChannelReceiver, IChannelReceiverHook, IChannelSender, ISecurableChannel
Ereditarietà
Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato come usare un HttpClientChannel oggetto per configurare un server di comunicazione remota e il relativo client. L'esempio contiene tre parti:

  • Un server

  • Un client

  • Oggetto remoto utilizzato dal server e dal client

Nell'esempio di codice seguente viene illustrato un server.

#using <System.dll>
#using <System.Runtime.Remoting.dll>
#using "common.dll"
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;

void main()
{
   // Create the server channel.
   HttpServerChannel^ serverChannel = gcnew HttpServerChannel( 9090 );
   
   // Register the server channel.
   ChannelServices::RegisterChannel( serverChannel );
   
   // Expose an object for remote calls.
   RemotingConfiguration::RegisterWellKnownServiceType( RemoteObject::typeid, L"RemoteObject.rem", WellKnownObjectMode::Singleton );
   
   // Wait for the user prompt.
   Console::WriteLine( L"Press ENTER to exit the server." );
   Console::ReadLine();
   Console::WriteLine( L"The server is exiting." );
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class Server
{
    public static void Main(string[] args)
    {
        // Create the server channel.
        HttpServerChannel serverChannel = new HttpServerChannel(9090);

        // Register the server channel.
        ChannelServices.RegisterChannel(serverChannel);

        // Expose an object for remote calls.
        RemotingConfiguration.RegisterWellKnownServiceType(
            typeof(RemoteObject), "RemoteObject.rem",
            WellKnownObjectMode.Singleton);

        // Wait for the user prompt.
        Console.WriteLine("Press ENTER to exit the server.");
        Console.ReadLine();
        Console.WriteLine("The server is exiting.");
    }
}

Nell'esempio di codice seguente viene illustrato un client per questo server.

#using <System.dll>
#using <System.Runtime.Remoting.dll>
#using "common.dll"

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;
void main()
{
   // Create the channel.
   HttpClientChannel^ clientChannel = gcnew HttpClientChannel;

   // Register the channel.
   ChannelServices::RegisterChannel( clientChannel );

   // Register as client for remote object.
   WellKnownClientTypeEntry^ remoteType = gcnew WellKnownClientTypeEntry( RemoteObject::typeid,L"http://localhost:9090/RemoteObject.rem" );
   RemotingConfiguration::RegisterWellKnownClientType( remoteType );

   // Create a message sink.
   String^ objectUri;
   System::Runtime::Remoting::Messaging::IMessageSink^ messageSink = clientChannel->CreateMessageSink( L"http://localhost:9090/RemoteObject.rem", nullptr,  objectUri );
   Console::WriteLine( L"The URI of the message sink is {0}.", objectUri );
   if ( messageSink != nullptr )
   {
      Console::WriteLine( L"The type of the message sink is {0}.", messageSink->GetType() );
   }

   // Display the channel's properties using Keys and Item.
   for each(String^ key in clientChannel->Keys)
   {
       Console::WriteLine("clientChannel[{0}] = <{1}>", key, clientChannel[key]);
   }

   // Parse the channel's URI.
   String^ objectUrl = L"http://localhost:9090/RemoteObject.rem";
   String^ channelUri = clientChannel->Parse( objectUrl,  objectUri );
   Console::WriteLine( L"The object URL is {0}.", objectUrl );
   Console::WriteLine( L"The object URI is {0}.", objectUri );
   Console::WriteLine( L"The channel URI is {0}.", channelUri );

   // Create an instance of the remote object.
   RemoteObject^ service = gcnew RemoteObject;
   
   // Invoke a method on the remote object.
   Console::WriteLine( L"The client is invoking the remote object." );
   Console::WriteLine( L"The remote object has been called {0} times.", service->GetCount() );
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class Client
{
    public static void Main(string[] args)
    {
        // Create the channel.
        HttpClientChannel clientChannel = new HttpClientChannel();

        // Register the channel.
        ChannelServices.RegisterChannel(clientChannel);

        // Register as client for remote object.
        WellKnownClientTypeEntry remoteType =
            new WellKnownClientTypeEntry(typeof(RemoteObject),
            "http://localhost:9090/RemoteObject.rem");
        RemotingConfiguration.RegisterWellKnownClientType(remoteType);

        // Create a message sink.
        string objectUri;
        System.Runtime.Remoting.Messaging.IMessageSink messageSink =
            clientChannel.CreateMessageSink(
            "http://localhost:9090/RemoteObject.rem",
            null, out objectUri);
        Console.WriteLine(
            "The URI of the message sink is {0}.",
            objectUri);
        if (messageSink != null)
        {
            Console.WriteLine("The type of the message sink is {0}.",
                messageSink.GetType().ToString());
        }

        // Display the channel's properties using Keys and Item.
        foreach(string key in clientChannel.Keys)
        {
            Console.WriteLine(
                "clientChannel[{0}] = <{1}>",
                key, clientChannel[key]);
        }

        // Parse the channel's URI.
        string objectUrl = "http://localhost:9090/RemoteObject.rem";
        string channelUri = clientChannel.Parse(objectUrl, out objectUri);
        Console.WriteLine("The object URL is {0}.", objectUrl);
        Console.WriteLine("The object URI is {0}.", objectUri);
        Console.WriteLine("The channel URI is {0}.", channelUri);

        // Create an instance of the remote object.
        RemoteObject service = new RemoteObject();

        // Invoke a method on the remote object.
        Console.WriteLine("The client is invoking the remote object.");
        Console.WriteLine("The remote object has been called {0} times.",
            service.GetCount());
    }
}

Nell'esempio di codice seguente viene illustrato l'oggetto remoto usato dal server e dal client.

#using <System.dll>
using namespace System;
using namespace System::Runtime::Remoting;

// Remote object.
public ref class RemoteObject: public MarshalByRefObject
{
private:
   static int callCount = 0;

public:
   int GetCount()
   {
      Console::WriteLine( L"GetCount was called." );
      callCount++;
      return (callCount);
   }

};
using System;
using System.Runtime.Remoting;

// Remote object.
public class RemoteObject : MarshalByRefObject
{
    private int callCount = 0;

    public int GetCount()
    {
        Console.WriteLine("GetCount was called.");
        callCount++;
        return(callCount);
    }
}

Commenti

Importante

La chiamata a metodi da questa classe con dati non attendibili costituisce un rischio per la sicurezza. Chiamare i metodi da questa classe solo con dati attendibili. Per altre informazioni, vedere Convalidare tutti gli input.

I canali trasportano i messaggi attraverso i limiti remoti (ad esempio, tra computer o domini applicazione). La HttpChannel classe trasporta i messaggi usando il protocollo HTTP.

I canali vengono usati dall'infrastruttura remota di .NET Framework per il trasporto di chiamate remote. Quando un client effettua una chiamata a un oggetto remoto, la chiamata viene serializzata in un messaggio inviato da un canale client e ricevuto da un canale server. Viene quindi deserializzato ed elaborato. Tutti i valori restituiti vengono trasmessi dal canale del server e ricevuti dal canale client.

Un HttpChannel oggetto ha associato proprietà di configurazione che possono essere impostate in fase di esecuzione in un file di configurazione (richiamando il metodo statico RemotingConfiguration.Configure ) o a livello di codice (passando una IDictionary raccolta al HttpChannel costruttore). Per un elenco di queste proprietà di configurazione, vedere Proprietà di configurazione canale e formattatore.

Costruttori

HttpChannel()

Inizializza una nuova istanza della classe HttpChannel.

HttpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)

Inizializza una nuova istanza della classe HttpChannel con le proprietà di configurazione e i sink specificati.

HttpChannel(Int32)

Inizializza una nuova istanza della classe HttpChannel con un canale server in attesa sulla porta specificata.

Campi

SinksWithProperties

Indica il sink di canale superiore nello stack di sink di canale.

(Ereditato da BaseChannelWithProperties)

Proprietà

ChannelData

Ottiene i dati specifici del canale.

ChannelName

Ottiene il nome del canale corrente.

ChannelPriority

Ottiene la priorità del canale corrente.

ChannelScheme

Ottiene il tipo di listener con cui eseguire l'hook, ad esempio "http".

ChannelSinkChain

Ottiene la catena di sink del canale utilizzata dal canale corrente.

Count

Ottiene il numero delle proprietà associate all'oggetto canale.

(Ereditato da BaseChannelObjectWithProperties)
IsFixedSize

Ottiene un valore che indica se il numero delle proprietà che è possibile immettere nell'oggetto canale corrente è fisso.

(Ereditato da BaseChannelObjectWithProperties)
IsReadOnly

Ottiene un valore che indica se l'insieme delle proprietà nell'oggetto canale è in sola lettura.

(Ereditato da BaseChannelObjectWithProperties)
IsSecured

Ottiene o imposta un valore booleano che indica se il canale corrente è protetto.

IsSynchronized

Ottiene un valore che indica se il dizionario delle proprietà dell'oggetto canale è sincronizzato.

(Ereditato da BaseChannelObjectWithProperties)
Item[Object]

Restituisce la proprietà del canale specificata.

Keys

Ottiene un insieme ICollection delle chiavi a cui sono associate le proprietà del canale.

Properties

Ottiene un insieme IDictionary delle proprietà del canale associate al canale corrente.

SyncRoot

Ottiene un oggetto che è possibile utilizzare per sincronizzare l'accesso a BaseChannelObjectWithProperties.

(Ereditato da BaseChannelObjectWithProperties)
Values

Ottiene un'interfaccia ICollection dei valori delle proprietà associate all'oggetto canale.

(Ereditato da BaseChannelObjectWithProperties)
WantsToListen

Ottiene un valore booleano che indica se l'istanza corrente intende eseguire l'hook al servizio listener esterno.

Metodi

Add(Object, Object)

Genera un oggetto NotSupportedException.

(Ereditato da BaseChannelObjectWithProperties)
AddHookChannelUri(String)

Aggiunge un URI sul quale l'hook del canale deve restare in attesa.

Clear()

Genera un oggetto NotSupportedException.

(Ereditato da BaseChannelObjectWithProperties)
Contains(Object)

Restituisce un valore che indica se l'oggetto canale contiene una proprietà associata alla chiave specificata.

(Ereditato da BaseChannelObjectWithProperties)
CopyTo(Array, Int32)

Genera un oggetto NotSupportedException.

(Ereditato da BaseChannelObjectWithProperties)
CreateMessageSink(String, Object, String)

Restituisce un sink dei messaggi del canale che invia messaggi all'URL o all'oggetto dati del canale specificato.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetEnumerator()

Restituisce un'interfaccia IDictionaryEnumerator che enumera tutte le proprietà associate all'oggetto canale.

(Ereditato da BaseChannelObjectWithProperties)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
GetUrlsForUri(String)

Restituisce una matrice di tutti gli URL di un oggetto con l'URI specificato, ospitato nell'oggetto HttpChannel corrente.

MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
Parse(String, String)

Estrae dall'URL specificato l'URI del canale e quello dell'oggetto remoto conosciuto.

Remove(Object)

Genera un oggetto NotSupportedException.

(Ereditato da BaseChannelObjectWithProperties)
StartListening(Object)

Indica al canale corrente di mettersi in attesa di richieste.

StopListening(Object)

Indica al canale corrente di interrompere l'attesa di richieste.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

IEnumerable.GetEnumerator()

Restituisce un'interfaccia IEnumerator che enumera tutte le proprietà associate all'oggetto canale.

(Ereditato da BaseChannelObjectWithProperties)

Metodi di estensione

Cast<TResult>(IEnumerable)

Esegue il cast degli elementi di un oggetto IEnumerable nel tipo specificato.

OfType<TResult>(IEnumerable)

Filtra gli elementi di un oggetto IEnumerable in base a un tipo specificato.

AsParallel(IEnumerable)

Consente la parallelizzazione di una query.

AsQueryable(IEnumerable)

Converte un oggetto IEnumerable in un oggetto IQueryable.

Si applica a

Vedi anche