Home Contact Sitemap
What is thinktecture? thinktecture is a European software development support company. We help software development and architecture teams with an influx of in-depth technical knowledge and expertise in the areas of application design, scalability, security and maintainability.

Location: Home > Resources > HTTP_BINARY

HOWTO: Use the BinaryFormatter with HTTPChannel

2002
Ingo Rammer

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);

 

 

 
© 2002-2008 by thinktecture, Ingo Rammer and Christian Weyer. All rights reserved. Contact | Impressum