Share via


HttpChannel 類別

定義

針對遠端呼叫實作使用 HTTP 通訊協定傳輸訊息的用戶端通道。

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
繼承
實作

範例

下列程式代碼範例示範如何使用 HttpClientChannel 來設定遠端伺服器及其用戶端。 此範例包含三個部分:

  • 伺服器

  • 用戶端

  • 伺服器和用戶端所使用的遠端物件

下列程式代碼範例顯示伺服器。

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

下列程式代碼範例顯示此伺服器的用戶端。

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

下列程式代碼範例顯示伺服器和用戶端所使用的遠端物件。

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

備註

重要

使用不信任的資料呼叫此類別的方法,會造成安全性上的風險。 呼叫此類別的方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

通道會跨遠端界限傳輸訊息,例如電腦或應用程式域之間的 () 。 類別 HttpChannel 會使用 HTTP 通訊協定傳輸訊息。

.NET Framework 遠端基礎結構會使用通道來傳輸遠端呼叫。 當用戶端呼叫遠端物件時,呼叫會串行化為用戶端通道所傳送並由伺服器通道接收的訊息。 然後,它會還原串行化並處理。 任何傳回的值會由伺服器通道傳輸,並由用戶端通道接收。

HttpChannel物件具有相關聯的組態屬性,可藉由叫用靜態RemotingConfiguration.Configure方法) ,或透過程式設計 (方式將集合傳遞IDictionaryHttpChannel建構函式) ,在運行時間設定組態檔 (。 如需這些組態屬性的清單,請參閱 通道和格式器組態屬性

建構函式

HttpChannel()

初始化 HttpChannel 類別的新執行個體。

HttpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider)

使用指定的組態屬性和接收,初始化 HttpChannel 類別的新執行個體。

HttpChannel(Int32)

使用接聽指定之連接埠的伺服器通道,初始化 HttpChannel 類別的新執行個體。

欄位

SinksWithProperties

指示通道接收堆疊中的頂端通道接收。

(繼承來源 BaseChannelWithProperties)

屬性

ChannelData

取得通道特定資料。

ChannelName

取得目前通道的名稱。

ChannelPriority

取得目前通道的優先權。

ChannelScheme

取得要連結的接聽程式類型 (例如,"http")。

ChannelSinkChain

取得目前通道正在使用的通道接收鏈結。

Count

取得與通道物件相關聯的屬性數目。

(繼承來源 BaseChannelObjectWithProperties)
IsFixedSize

取得值,指出可輸入到通道物件的屬性數目是否為固定的。

(繼承來源 BaseChannelObjectWithProperties)
IsReadOnly

取得值,指出通道物件中的屬性集合是否為唯讀。

(繼承來源 BaseChannelObjectWithProperties)
IsSecured

取得或設定指出目前通道是否安全的布林值。

IsSynchronized

取得值,指出通道物件屬性的字典是否已經同步 (Synchronize)。

(繼承來源 BaseChannelObjectWithProperties)
Item[Object]

傳回指定的通道屬性。

Keys

取得與通道屬性相關聯之索引鍵的 ICollection

Properties

取得與目前通道相關聯之通道屬性的 IDictionary

SyncRoot

取得可用來對 BaseChannelObjectWithProperties 同步存取的物件。

(繼承來源 BaseChannelObjectWithProperties)
Values

取得與通道物件相關聯之屬性值的 ICollection

(繼承來源 BaseChannelObjectWithProperties)
WantsToListen

取得布林 (Boolean) 值,指出目前執行個體是否要連接到外部接聽服務。

方法

Add(Object, Object)

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
AddHookChannelUri(String)

加入 URI,通道攔截程序 (Hook) 應該在其上接聽。

Clear()

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
Contains(Object)

傳回值,指出通道物件是否包含與指定之索引鍵相關聯的屬性。

(繼承來源 BaseChannelObjectWithProperties)
CopyTo(Array, Int32)

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
CreateMessageSink(String, Object, String)

傳回通道訊息接收,其傳遞訊息給指定 URL 或通道資料物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回 IDictionaryEnumerator,其列舉與通道物件相關聯的所有屬性。

(繼承來源 BaseChannelObjectWithProperties)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetUrlsForUri(String)

傳回具有指定 URI 之物件的所有 URL 的陣列,這 URI 裝載 (Host) 於目前 HttpChannel 上。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Parse(String, String)

從指定的 URL 擷取通道 URI 和遠端已知物件 URI。

Remove(Object)

擲回 NotSupportedException

(繼承來源 BaseChannelObjectWithProperties)
StartListening(Object)

指示目前通道開始接聽要求。

StopListening(Object)

指示目前通道停止接聽要求。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IEnumerable.GetEnumerator()

傳回 IEnumerator,其列舉與通道物件相關聯的所有屬性。

(繼承來源 BaseChannelObjectWithProperties)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱