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
- 继承
- 实现
示例
下面的代码示例演示如何使用 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 |
获取一个值,该值指示信道对象属性的字典是否同步。 (继承自 BaseChannelObjectWithProperties) |
Item[Object] |
返回指定的信道属性。 |
Keys |
获取与信道属性关联的键的 ICollection 。 |
Properties |
获取与当前信道关联的信道属性的 IDictionary 。 |
SyncRoot |
获取一个对象,该对象用于同步对 BaseChannelObjectWithProperties 的访问。 (继承自 BaseChannelObjectWithProperties) |
Values |
获取与信道对象关联的属性值的 ICollection。 (继承自 BaseChannelObjectWithProperties) |
WantsToListen |
获取一个布尔值,该值指示是否需要将当前实例挂钩到外部侦听器服务。 |
方法
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() |
返回 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) |
显式接口实现
IEnumerable.GetEnumerator() |
返回 IEnumerator,它枚举与信道对象关联的所有属性。 (继承自 BaseChannelObjectWithProperties) |
扩展方法
Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定的类型。 |
OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
AsParallel(IEnumerable) |
启用查询的并行化。 |
AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |