HttpChannel Class
Implements a client channel for remote calls that uses the HTTP protocol to transmit messages.
Assembly: System.Runtime.Remoting (in System.Runtime.Remoting.dll)
Channels transport messages across remoting boundaries (for example, between computers or application domains). The HttpChannel class transports messages using the HTTP protocol.
Channels are used by the .NET Framework remoting infrastructure to transport remote calls. When a client makes a call to a remote object, the call is serialized into a message that is sent by a client channel and received by a server channel. It is then deserialized and processed. Any returned values are transmitted by the server channel and received by the client channel.
A HttpChannel object has associated configuration properties that can be set at run time either in a configuration file (by invoking the static RemotingConfiguration.Configure method) or programmatically (by passing a IDictionary collection to the HttpChannel constructor). For a list of these configuration properties, see Channel and Formatter Configuration Properties.
The following code example shows how to use a HttpClientChannel to set up a remoting server and its client. The example contains three parts:
A server
A client
A remote object used by the server and the client
The following code example shows a server.
#using <mscorlib.dll>
#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 = new HttpServerChannel(9090);
// Register the server channel.
ChannelServices::RegisterChannel(serverChannel);
// Expose an object for remote calls.
RemotingConfiguration::RegisterWellKnownServiceType(
__typeof(RemoteObject), S"RemoteObject.rem",
WellKnownObjectMode::Singleton);
// Wait for the user prompt.
Console::WriteLine(S"Press ENTER to exit the server.");
Console::ReadLine();
Console::WriteLine(S"The server is exiting.");
}
The following code example shows a client for this server.
#using <mscorlib.dll>
#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 = new HttpClientChannel();
// Register the channel.
ChannelServices::RegisterChannel(clientChannel);
// Register as client for remote object.
WellKnownClientTypeEntry* remoteType =
new WellKnownClientTypeEntry(__typeof(RemoteObject),
S"http://localhost:9090/RemoteObject.rem");
RemotingConfiguration::RegisterWellKnownClientType(remoteType);
// Create a message sink.
String* objectUri;
System::Runtime::Remoting::Messaging::IMessageSink* messageSink =
clientChannel->CreateMessageSink(
S"http://localhost:9090/RemoteObject.rem",
0, &objectUri);
Console::WriteLine(
S"The URI of the message sink is {0}.",
objectUri);
if (messageSink != 0)
{
Console::WriteLine(S"The type of the message sink is {0}.",
messageSink->GetType());
}
// Display the channel's properties using Keys and Item.
System::Collections::IEnumerator* myEnum = clientChannel->Keys->GetEnumerator();
while (myEnum->MoveNext())
{
String* key = __try_cast<String*>(myEnum->Current);
Console::WriteLine(
S"clientChannel[{0}] = <{1}>",
key, clientChannel->Item[key]);
}
// Parse the channel's URI.
String* objectUrl = S"http://localhost:9090/RemoteObject.rem";
String* channelUri = clientChannel->Parse(objectUrl, &objectUri);
Console::WriteLine(S"The object URL is {0}.", objectUrl);
Console::WriteLine(S"The object URI is {0}.", objectUri);
Console::WriteLine(S"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(S"The client is invoking the remote object.");
Console::WriteLine(S"The remote object has been called {0} times.", __box(service->GetCount()));
}
The following code example shows the remote object used by the server and the client.
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::Remoting;
// Remote object.
public __gc class RemoteObject : public MarshalByRefObject
{
private:
static int callCount = 0;
public:
int GetCount()
{
Console::WriteLine(S"GetCount was called.");
callCount++;
return(callCount);
}
};
System.Runtime.Remoting.Channels.BaseChannelObjectWithProperties
System.Runtime.Remoting.Channels.BaseChannelWithProperties
System.Runtime.Remoting.Channels.Http.HttpChannel
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.