HttpChannel 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
實作一個用戶端通道用於遠端通話,使用 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
- 繼承
- 實作
範例
以下程式碼範例說明如何使用 a 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 框架遠端基礎設施用於傳輸遠端通話。 當用戶端呼叫遠端物件時,該呼叫會序列化成由用戶端通道傳送並由伺服器通道接收的訊息。 接著被解序並處理。 任何回傳的值由伺服器通道傳送,並由用戶端通道接收。
物件HttpChannel具有相關的組態屬性,可在執行時設定,無論是在設定檔中(透過呼叫靜態RemotingConfiguration.Configure方法)或程式化(將集合傳給IDictionaryHttpChannel建構器)中設定。
建構函式
| 名稱 | Description |
|---|---|
| HttpChannel() |
初始化 HttpChannel 類別的新執行個體。 |
| HttpChannel(IDictionary, IClientChannelSinkProvider, IServerChannelSinkProvider) |
初始化一個具有指定配置屬性與匯入的類別新實例 HttpChannel 。 |
| HttpChannel(Int32) |
初始化一個新的類別實例 HttpChannel ,並以伺服器通道監聽指定的埠口。 |
欄位
| 名稱 | Description |
|---|---|
| SinksWithProperties |
表示槽槽匯堆疊中的頂端通道匯。 (繼承來源 BaseChannelWithProperties) |
屬性
| 名稱 | Description |
|---|---|
| ChannelData |
取得頻道特定的資料。 |
| ChannelName |
會得到目前頻道的名稱。 |
| ChannelPriority |
獲得當前頻道的優先權。 |
| ChannelScheme |
它會取得可以勾接的聆聽者類型(例如「http」)。 |
| ChannelSinkChain |
取得目前該通道使用的通道匯流鏈條。 |
| Count |
取得與通道物件相關的屬性數量。 (繼承來源 BaseChannelObjectWithProperties) |
| IsFixedSize |
會得到一個值,表示通道物件中可輸入屬性數量是否固定。 (繼承來源 BaseChannelObjectWithProperties) |
| IsReadOnly |
會獲得一個值,表示通道物件中的屬性集合是否為唯讀。 (繼承來源 BaseChannelObjectWithProperties) |
| IsSecured |
取得或設定一個布林值,指示目前通道是否安全。 |
| IsSynchronized |
會得到一個值,表示通道物件屬性字典是否同步。 (繼承來源 BaseChannelObjectWithProperties) |
| Item[Object] |
回傳指定的通道特性。 |
| Keys |
取得 ICollection 通道屬性所關聯的一鍵。 |
| Properties |
取得 IDictionary 與當前頻道相關的頻道屬性。 |
| SyncRoot |
取得一個物件用來同步存取。BaseChannelObjectWithProperties (繼承來源 BaseChannelObjectWithProperties) |
| Values |
取得 ICollection 與通道物件相關屬性的數值。 (繼承來源 BaseChannelObjectWithProperties) |
| WantsToListen |
會取得一個布林值,表示目前實例是否想連接到外部監聽器服務。 |
方法
| 名稱 | Description |
|---|---|
| Add(Object, Object) | (繼承來源 BaseChannelObjectWithProperties) |
| AddHookChannelUri(String) |
新增一個 URI,讓通道鉤子應該會監聽。 |
| Clear() | (繼承來源 BaseChannelObjectWithProperties) |
| Contains(Object) |
回傳一個值,指示通道物件是否包含與指定鍵相關聯的屬性。 (繼承來源 BaseChannelObjectWithProperties) |
| CopyTo(Array, Int32) | (繼承來源 BaseChannelObjectWithProperties) |
| CreateMessageSink(String, Object, String) |
回傳一個通道訊息匯入器,將訊息傳送到指定的 URL 或通道資料物件。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetEnumerator() |
回傳 a IDictionaryEnumerator ,列舉所有與通道物件相關的屬性。 (繼承來源 BaseChannelObjectWithProperties) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| GetUrlsForUri(String) |
回傳一個包含指定 URI 物件的所有 URL 陣列,該物件託管於目前 HttpChannel的 。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Parse(String, String) |
從指定的 URL 中擷取通道 URI 及遠端知名物件 URI。 |
| Remove(Object) | (繼承來源 BaseChannelObjectWithProperties) |
| StartListening(Object) |
指示當前頻道開始監聽請求。 |
| StopListening(Object) |
指示當前頻道停止監聽請求。 |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| IEnumerable.GetEnumerator() |
回傳 a IEnumerator 枚舉所有與通道物件相關聯的屬性。 (繼承來源 BaseChannelObjectWithProperties) |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |