Udostępnij za pośrednictwem


HttpClientChannel Klasa

Definicja

Implementuje kanał klienta dla wywołań zdalnych, które używają protokołu HTTP do przesyłania komunikatów.

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
Dziedziczenie
Implementuje

Przykłady

W poniższym przykładzie kodu pokazano, jak skonfigurować serwer komunikacji zdalnie i jego klienta za pomocą elementu a HttpClientChannel . Przykład zawiera trzy części:

  • Serwer

  • Klient

  • Obiekt zdalny używany przez serwer i klienta

Poniższy przykład kodu przedstawia serwer.

#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.");
    }
}

Poniższy przykład kodu przedstawia klienta dla tego serwera.

#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());
    }
}

Poniższy przykład kodu przedstawia obiekt zdalny używany przez serwer i klienta.

#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);
    }
}

Uwagi

Ważna

Wywoływanie metod z tej klasy z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Wywołaj metody z tej klasy tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs.

Kanały transportują komunikaty przez granice komunikacji przychodzącej (na przykład między komputerami lub domenami aplikacji). Klasa HttpClientChannel transportuje komunikaty przy użyciu protokołu HTTP.

Kanały są używane przez infrastrukturę komunikacji zdalnej platformy .NET Framework do transportu połączeń zdalnych. Gdy klient wykonuje wywołanie obiektu zdalnego, wywołanie jest serializowane do komunikatu wysyłanego przez kanał klienta i odbieranego przez kanał serwera. Następnie jest deserializowany i przetwarzany. Wszystkie zwrócone wartości są przesyłane przez kanał serwera i odbierane przez kanał klienta.

Aby wykonać dodatkowe przetwarzanie komunikatów po stronie klienta, można określić implementację IClientChannelSinkProvider , za pomocą której przekazywane są wszystkie komunikaty przetwarzane przez HttpClientChannel element .

Domyślnie HttpServerChannel program używa formatującego protokołu SOAP do serializacji wszystkich komunikatów.

Obiekt HttpClientChannel ma skojarzone właściwości konfiguracji, które można ustawić w czasie wykonywania w pliku konfiguracji (wywołując metodę statyczną RemotingConfiguration.Configure ) lub programowo (przekazując IDictionary kolekcję do konstruktora HttpClientChannel ).

Konstruktory

Nazwa Opis
HttpClientChannel()

Inicjuje nowe wystąpienie klasy HttpClientChannel.

HttpClientChannel(IDictionary, IClientChannelSinkProvider)

Inicjuje nowe wystąpienie HttpClientChannel klasy z określonymi właściwościami konfiguracji i ujściem.

HttpClientChannel(String, IClientChannelSinkProvider)

Inicjuje HttpClientChannel nowe wystąpienie klasy o określonej nazwie i ujściu.

Pola

Nazwa Opis
SinksWithProperties

Wskazuje ujście kanału górnego w stosie ujścia kanału.

(Odziedziczone po BaseChannelWithProperties)

Właściwości

Nazwa Opis
ChannelName

Pobiera nazwę bieżącego kanału.

ChannelPriority

Pobiera priorytet bieżącego kanału.

Count

Pobiera liczbę właściwości skojarzonych z obiektem kanału.

(Odziedziczone po BaseChannelObjectWithProperties)
IsFixedSize

Pobiera wartość wskazującą, czy liczba właściwości, które można wprowadzić do obiektu kanału, jest stała.

(Odziedziczone po BaseChannelObjectWithProperties)
IsReadOnly

Pobiera wartość wskazującą, czy kolekcja właściwości w obiekcie kanału jest tylko do odczytu.

(Odziedziczone po BaseChannelObjectWithProperties)
IsSecured

Pobiera lub ustawia, czy kanał klienta jest zabezpieczony.

IsSynchronized

Pobiera wartość wskazującą, czy słownik właściwości obiektu kanału jest synchronizowany.

(Odziedziczone po BaseChannelObjectWithProperties)
Item[Object]

Zwraca określoną właściwość kanału.

Keys

Pobiera klucze ICollection , z którymi są skojarzone właściwości kanału.

Properties

Pobiera właściwości IDictionary kanału skojarzone z bieżącym obiektem kanału.

(Odziedziczone po BaseChannelWithProperties)
SyncRoot

Pobiera obiekt używany do synchronizowania dostępu do obiektu BaseChannelObjectWithProperties.

(Odziedziczone po BaseChannelObjectWithProperties)
Values

Pobiera wartości ICollection właściwości skojarzonych z obiektem kanału.

(Odziedziczone po BaseChannelObjectWithProperties)

Metody

Nazwa Opis
Add(Object, Object)

Zgłasza element NotSupportedException.

(Odziedziczone po BaseChannelObjectWithProperties)
Clear()

Zgłasza element NotSupportedException.

(Odziedziczone po BaseChannelObjectWithProperties)
Contains(Object)

Zwraca wartość wskazującą, czy obiekt kanału zawiera właściwość skojarzona z określonym kluczem.

(Odziedziczone po BaseChannelObjectWithProperties)
CopyTo(Array, Int32)

Zgłasza element NotSupportedException.

(Odziedziczone po BaseChannelObjectWithProperties)
CreateMessageSink(String, Object, String)

Zwraca ujście komunikatu kanału, który dostarcza komunikaty do określonego adresu URL lub obiektu danych kanału.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetEnumerator()

Zwraca wartość IDictionaryEnumerator , która wylicza wszystkie właściwości skojarzone z obiektem kanału.

(Odziedziczone po BaseChannelObjectWithProperties)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Pobiera Type bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Object.

(Odziedziczone po Object)
Parse(String, String)

Wyodrębnia identyfikator URI kanału i zdalnego dobrze znanego identyfikatora URI obiektu z określonego adresu URL.

Remove(Object)

Zgłasza element NotSupportedException.

(Odziedziczone po BaseChannelObjectWithProperties)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

Nazwa Opis
IEnumerable.GetEnumerator()

Zwraca wartość IEnumerator , która wylicza wszystkie właściwości skojarzone z obiektem kanału.

(Odziedziczone po BaseChannelObjectWithProperties)

Metody rozszerzania

Nazwa Opis
AsParallel(IEnumerable)

Umożliwia równoległość zapytania.

AsQueryable(IEnumerable)

Konwertuje IEnumerable na IQueryable.

Cast<TResult>(IEnumerable)

Rzutuje elementy IEnumerable do określonego typu.

OfType<TResult>(IEnumerable)

Filtruje elementy IEnumerable na podstawie określonego typu.

Dotyczy