HttpServerChannel 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为远程调用实现使用 HTTP 协议传输消息的服务器信道。
public ref class HttpServerChannel : System::Runtime::Remoting::Channels::BaseChannelWithProperties, System::Runtime::Remoting::Channels::IChannelReceiver, System::Runtime::Remoting::Channels::IChannelReceiverHook
public class HttpServerChannel : System.Runtime.Remoting.Channels.BaseChannelWithProperties, System.Runtime.Remoting.Channels.IChannelReceiver, System.Runtime.Remoting.Channels.IChannelReceiverHook
type HttpServerChannel = class
inherit BaseChannelWithProperties
interface IChannelReceiver
interface IChannel
interface IChannelReceiverHook
Public Class HttpServerChannel
Inherits BaseChannelWithProperties
Implements IChannelReceiver, IChannelReceiverHook
- 继承
- 实现
示例
下面的代码示例演示如何使用 HttpServerChannel 对象设置远程处理服务器及其客户端。 该示例包含三个部分:
服务器
客户端
服务器和客户端使用的远程对象
下面的代码示例演示了一个服务器。
#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;
int main()
{
// Create the server channel.
HttpServerChannel^ serverChannel = gcnew HttpServerChannel( 9090 );
// Register the server channel.
ChannelServices::RegisterChannel( serverChannel );
// Display the channel's scheme.
Console::WriteLine( L"The channel scheme is {0}.", serverChannel->ChannelScheme );
// Display the channel's URI.
Console::WriteLine( L"The channel URI is {0}.", serverChannel->GetChannelUri() );
// Expose an object for remote calls.
RemotingConfiguration::RegisterWellKnownServiceType(
RemoteObject::typeid, L"RemoteObject.rem", WellKnownObjectMode::Singleton );
// Get the channel's sink chain.
IServerChannelSink^ sinkChain = serverChannel->ChannelSinkChain;
Console::WriteLine( L"The type of the server channel's sink chain is {0}.", sinkChain->GetType() );
// See if the channel wants to listen.
bool wantsToListen = serverChannel->WantsToListen;
Console::WriteLine( L"The value of WantsToListen is {0}.", wantsToListen );
// Parse the channel's URI.
array<String^>^ urls = serverChannel->GetUrlsForUri( L"RemoteObject.rem" );
if ( urls->Length > 0 )
{
String^ objectUrl = urls[ 0 ];
String^ objectUri;
String^ channelUri = serverChannel->Parse( objectUrl, objectUri );
Console::WriteLine( L"The object URI is {0}.", objectUri );
Console::WriteLine( L"The channel URI is {0}.", channelUri );
Console::WriteLine( L"The object URL is {0}.", objectUrl );
}
// 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);
// Display the channel's scheme.
Console.WriteLine("The channel scheme is {0}.",
serverChannel.ChannelScheme);
// Display the channel's URI.
Console.WriteLine("The channel URI is {0}.",
serverChannel.GetChannelUri());
// Expose an object for remote calls.
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteObject), "RemoteObject.rem",
WellKnownObjectMode.Singleton);
// Get the channel's sink chain.
IServerChannelSink sinkChain = serverChannel.ChannelSinkChain;
Console.WriteLine(
"The type of the server channel's sink chain is {0}.",
sinkChain.GetType().ToString());
// See if the channel wants to listen.
bool wantsToListen = serverChannel.WantsToListen;
Console.WriteLine(
"The value of WantsToListen is {0}.",
wantsToListen);
// Parse the channel's URI.
string[] urls = serverChannel.GetUrlsForUri("RemoteObject.rem");
if (urls.Length > 0)
{
string objectUrl = urls[0];
string objectUri;
string channelUri =
serverChannel.Parse(objectUrl, out objectUri);
Console.WriteLine("The object URI is {0}.", objectUri);
Console.WriteLine("The channel URI is {0}.", channelUri);
Console.WriteLine("The object URL is {0}.", objectUrl);
}
// 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^ channel = gcnew HttpClientChannel;
// Register the channel.
ChannelServices::RegisterChannel( channel );
// Register as client for remote object.
WellKnownClientTypeEntry^ remoteType = gcnew WellKnownClientTypeEntry(
RemoteObject::typeid,L"http://localhost:9090/RemoteObject.rem" );
RemotingConfiguration::RegisterWellKnownClientType( remoteType );
// 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 channel = new HttpClientChannel();
// Register the channel.
ChannelServices.RegisterChannel(channel);
// Register as client for remote object.
WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(
typeof(RemoteObject),"http://localhost:9090/RemoteObject.rem");
RemotingConfiguration.RegisterWellKnownClientType(remoteType);
// 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 namespace System;
using namespace System::Runtime::Remoting;
// Remote object.
public ref class RemoteObject: public MarshalByRefObject
{
private:
static int callCount = 0;
public:
int GetCount()
{
callCount++;
return (callCount);
}
};
using System;
using System.Runtime.Remoting;
// Remote object.
public class RemoteObject : MarshalByRefObject
{
private int callCount = 0;
public int GetCount()
{
callCount++;
return(callCount);
}
}
注解
通道跨远程处理边界传输消息 (例如,在应用程序域) 上的计算机之间传输消息。 类 HttpServerChannel 使用 HTTP 协议传输消息。
.NET Framework远程处理基础结构使用通道来传输远程呼叫。 当客户端调用远程对象时,该调用将序列化为由客户端通道发送并由服务器通道接收的消息。 然后对其进行反序列化和处理。 任何返回的值都由服务器通道传输并由客户端通道接收。
若要在服务器端执行其他消息处理,可以指定 的 IServerChannelSinkProvider 实现,通过该实现传递处理 HttpServerChannel 的所有消息。
接受 HttpServerChannel 以二进制或 SOAP 格式序列化的消息。
HttpServerChannel对象具有关联的配置属性,可以在运行时通过调用静态RemotingConfiguration.Configure方法) (配置文件中设置这些属性,或通过将集合传递给IDictionaryHttpServerChannel构造函数) 以编程方式 (。 有关这些配置属性的列表,请参阅 的文档 HttpServerChannel。
构造函数
HttpServerChannel() |
初始化 HttpServerChannel 类的新实例。 |
HttpServerChannel(IDictionary, IServerChannelSinkProvider) |
使用指定的信道属性和接收器初始化 HttpServerChannel 类的新实例。 |
HttpServerChannel(Int32) |
初始化在指定端口上侦听的 HttpServerChannel 类的新实例。 |
HttpServerChannel(String, Int32) |
使用给定名称初始化 HttpServerChannel 类的新实例,该实例侦听指定的端口。 |
HttpServerChannel(String, Int32, IServerChannelSinkProvider) |
在指定的端口以给定的名称初始化 HttpServerChannel 类的新实例,该实例侦听指定的端口并使用指定的接收器。 |
字段
SinksWithProperties |
指示信道接收器堆栈中最上面的信道接收器。 (继承自 BaseChannelWithProperties) |
属性
ChannelData |
获取通道特定的数据。 |
ChannelName |
获取当前信道的名称。 |
ChannelPriority |
获取当前信道的优先级。 |
ChannelScheme |
获取要挂钩到的侦听器的类型(例如“http”)。 |
ChannelSinkChain |
获取当前信道正在使用的信道接收器链。 |
Count |
获取与该信道对象关联的属性的数目。 (继承自 BaseChannelObjectWithProperties) |
IsFixedSize |
获取一个值,该值指示可以输入到信道对象中的属性的数目是否是固定的。 (继承自 BaseChannelObjectWithProperties) |
IsReadOnly |
获取一个值,该值指示信道对象中的属性集合是否是只读的。 (继承自 BaseChannelObjectWithProperties) |
IsSynchronized |
获取一个值,该值指示信道对象属性的字典是否同步。 (继承自 BaseChannelObjectWithProperties) |
Item[Object] |
返回指定的信道属性。 |
Keys |
获取与信道属性关联的键的 ICollection 。 |
Properties |
获取与当前信道对象关联的信道属性的 IDictionary。 (继承自 BaseChannelWithProperties) |
SyncRoot |
获取一个对象,该对象用于同步对 BaseChannelObjectWithProperties 的访问。 (继承自 BaseChannelObjectWithProperties) |
Values |
获取与信道对象关联的属性值的 ICollection。 (继承自 BaseChannelObjectWithProperties) |
WantsToListen |
获取一个布尔值,该值指示是否需要将 IChannelReceiverHook 挂接到外部侦听器服务。 |
方法
Add(Object, Object) | (继承自 BaseChannelObjectWithProperties) |
AddHookChannelUri(String) |
添加信道挂钩必须对其进行侦听的 URI。 |
Clear() | (继承自 BaseChannelObjectWithProperties) |
Contains(Object) |
返回一个值,该值指示信道对象是否包含与指定键关联的属性。 (继承自 BaseChannelObjectWithProperties) |
CopyTo(Array, Int32) | (继承自 BaseChannelObjectWithProperties) |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetChannelUri() |
返回当前信道的 URI。 |
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。 |