HttpServerChannel 类

定义

为使用 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框架远程处理基础结构使用通道来传输远程呼叫。 当客户端调用远程对象时,调用将序列化为客户端通道发送并由服务器通道接收的消息。 然后对其进行反序列化和处理。 任何返回的值都由服务器通道传输,并由客户端通道接收。

若要在服务器端执行其他消息处理,可以指定传递所有消息的HttpServerChannel实现IServerChannelSinkProvider

接受 HttpServerChannel 以二进制或 SOAP 格式序列化的消息。

对象HttpServerChannel具有可在配置文件(通过调用静态RemotingConfiguration.Configure方法)或以编程方式(将集合传递给IDictionaryHttpServerChannel构造函数)中设置的关联配置属性。 有关这些配置属性的列表,请参阅相关 HttpServerChannel文档。

构造函数

名称 说明
HttpServerChannel()

初始化 HttpServerChannel 类的新实例。

HttpServerChannel(IDictionary, IServerChannelSinkProvider)

使用指定的通道属性和接收器初始化类的新实例 HttpServerChannel

HttpServerChannel(Int32)

初始化在指定端口上侦听的类的新实例 HttpServerChannel

HttpServerChannel(String, Int32, IServerChannelSinkProvider)

使用给定名称在指定端口处初始化类的新实例 HttpServerChannel ,该名称侦听指定端口,并使用指定的接收器。

HttpServerChannel(String, Int32)

使用给定的名称初始化类的新实例 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)

引发 NotSupportedException

(继承自 BaseChannelObjectWithProperties)
AddHookChannelUri(String)

添加通道挂钩必须侦听的 URI。

Clear()

引发 NotSupportedException

(继承自 BaseChannelObjectWithProperties)
Contains(Object)

返回一个值,该值指示通道对象是否包含与指定键关联的属性。

(继承自 BaseChannelObjectWithProperties)
CopyTo(Array, Int32)

引发 NotSupportedException

(继承自 BaseChannelObjectWithProperties)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetChannelUri()

返回当前通道的 URI。

GetEnumerator()

返回一个 IDictionaryEnumerator 枚举与通道对象关联的所有属性。

(继承自 BaseChannelObjectWithProperties)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
GetUrlsForUri(String)

返回具有指定 URI 的对象的所有 URL 的数组,这些 URL 托管在当前 HttpChannel对象上。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Parse(String, String)

从指定的 URL 中提取通道 URI 和远程已知对象 URI。

Remove(Object)

引发 NotSupportedException

(继承自 BaseChannelObjectWithProperties)
StartListening(Object)

指示当前通道开始侦听请求。

StopListening(Object)

指示当前通道停止侦听请求。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
IEnumerable.GetEnumerator()

返回一个 IEnumerator 枚举与通道对象关联的所有属性。

(继承自 BaseChannelObjectWithProperties)

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于