HOWTO: Use the BinaryFormatter with HTTPChannel
You can use the BinaryFormatter with HttpChannel in two different ways: by using configuration files or by writing some code.
Using Configuration Files
This one is actually quite simple. Just place the following statements in the client's configuration file:
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http">
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>When using IIS on the server side this is supported by default - i.e. you don't have to change your server-side configuration file at all.
The standard way of specifying this for a server-side application would be:
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http" port="1234">
<serverProviders>
<formatter ref="binary" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration> Using code
First, you have to reference System.Runtime.Remoting.DLL and to include at least the following namespaces:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
Then you can use the following code to add a binary formatter for an HttpChannel:
IDictionary props = new Hashtable();
props["name"] = "MyHttpChannel";
props["port"] = 1234;
BinaryServerFormatterSinkProvider srv = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clnt = new BinaryClientFormatterSinkProvider();
HttpChannel channel = new HttpChannel(props,clnt,srv);
ChannelServices.RegisterChannel(channel);
If you only need either client or server side formatter, you can omit "the other" formatter and just pass null to the HttpChannel's constructor. For a "client-only" channel, you can therefore use the following code:
BinaryClientFormatterSinkProvider clnt = new BinaryClientFormatterSinkProvider();
HttpChannel channel = new HttpChannel(null,clnt,null);
ChannelServices.RegisterChannel(channel);