HttpClientChannel Classe

Definizione

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

public ref class HttpClientChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelSender
public ref class HttpClientChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelSender, System::Runtime::Remoting::Channels::ISecurableChannel
public class HttpClientChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelSender
public class HttpClientChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelSender, System.Runtime.Remoting.Channels.ISecurableChannel
type HttpClientChannel = class
    inherit BaseChannelWithProperties
    interface IChannelSender
    interface IChannel
type HttpClientChannel = class
    inherit BaseChannelWithProperties
    interface IChannelSender
    interface IChannel
    interface ISecurableChannel
Public Class HttpClientChannel
Inherits BaseChannelWithProperties
Implements IChannelSender
Public Class HttpClientChannel
Inherits BaseChannelWithProperties
Implements 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 HttpClientChannel 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.

Per eseguire un'ulteriore elaborazione dei messaggi sul lato client, è possibile specificare un'implementazione di IClientChannelSinkProvider tramite la quale vengono passati tutti i messaggi elaborati da HttpClientChannel .

Per impostazione predefinita, HttpServerChannel usa un formattatore SOAP per serializzare tutti i messaggi.

Un HttpClientChannel 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 HttpClientChannel costruttore). Per un elenco di queste proprietà di configurazione, vedere Proprietà di configurazione canale e formattatore.

Costruttori

HttpClientChannel()

Inizializza una nuova istanza della classe HttpClientChannel.

HttpClientChannel(IDictionary, IClientChannelSinkProvider)

Inizializza una nuova istanza della classe HttpClientChannel con le proprietà di configurazione e il sink specificato.

HttpClientChannel(String, IClientChannelSinkProvider)

Inizializza una nuova istanza della classe HttpClientChannel con il nome e il sink specificato.

Campi

SinksWithProperties

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

(Ereditato da BaseChannelWithProperties)

Proprietà

ChannelName

Ottiene il nome del canale corrente.

ChannelPriority

Ottiene la priorità del 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 che indica se il canale del client è sicuro.

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'interfaccia IDictionary delle proprietà del canale associate all'oggetto canale corrente.

(Ereditato da BaseChannelWithProperties)
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)

Metodi

Add(Object, Object)

Genera un oggetto NotSupportedException.

(Ereditato da BaseChannelObjectWithProperties)
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)
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)
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