Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#
Article

Dynamic insertion of a client channel sink provider

Rate me:
Please Sign up or sign in to vote.
4.42/5 (9 votes)
10 Apr 2003CPOL2 min read 55.8K   592   26   4
Workaround to specify a desired channel for connecting to a remote object

Introduction

The .NET remoting architecture is world-famous for its flexibility and versatility with
limitations rarely found.

Here is one limitation that I want to point out. Then I will show a workaround, exploiting
the fabulous flexibility of the remoting architecture, by dynamically inserting a client channel sink provider.

The problem: I have two remote services named Knossos and Zakros with channel configuration entries

for Knossos: 

XML
  <channel ref="tcp" port="9000" name = "Two way binary tcp channel"><BR>   <clientProviders><BR>    <formatter ref="binary"/><BR>   </clientProviders><BR>   <serverProviders><BR>    <formatter ref="binary"/><BR>   </serverProviders><BR>  </channel>

and for Zacros:

XML
  <channel ref="tcp" port="9000" name = "Two way soap tcp channel"><BR>   <clientProviders><BR>    <formatter ref="soap"/><BR>   </clientProviders><BR>   <serverProviders><BR>    <formatter ref="soap"/><BR>   </serverProviders><BR>  </channel>

Note that the transport is TCP for either service but the formatting is 'binary' for Knossos and 'soap' for Zakros.

Next comes a client app that can communicate with both Zakros and Knossos. Here is
the channel configuration for the client app,

for Knossos:

XML
  <channel ref="tcp" port="8000" name="Two way binary client tcp channel"><BR>   <clientProviders><BR>    <formatter ref="binary"/><BR>   </clientProviders><BR>   <serverProviders><BR>    <formatter ref="binary"/><BR>   </serverProviders><BR>  </channel>

and for Zakros:

XML
  <channel ref="tcp" port="8080" name="Two way soap client tcp channel"><BR>   <clientProviders><BR>    <formatter ref="soap"/><BR>   </clientProviders><BR>   <serverProviders><BR>    <formatter ref="soap"/><BR>   </serverProviders><BR>  </channel>

Note again that the transport is TCP for either but formatting is binary and SOAP.

There is a single client app that must communicate with Knossos using the TCP-binary channel and with Zakros using the TCP-soap channel. That is the problem. The comunication with Knossos over the TCP-binary channel will work just fine. However, the communication with Zakros over the TCP-soap channel will not work. The reason for this is that the remoting system will pick the first TCP channel from the list of registered channels without
regard to the formatter sink. So, the connection to Zakros does not work because the first TCP channel uses a binary formatter whereas the connection to Zakros requires a soap formatter.

Everything would work just fine if the connection to Zakros was configured as an HTTP-soap channel. But that is not the desired configuartion.

One possible workaround is to dynamically insert the appropriate formatters. We can do this with the help of a custom client sink provider. Here is the class definition.

C#
public class AbstractClientFormatterSinkProvider <BR> : IClientFormatterSinkProvider {<BR> public AbstractClientFormatterSinkProvider() { }<BR> public AbstractClientFormatterSinkProvider(IDictionary props, <BR>                                            ICollection providerData) { }<BR><BR> IClientChannelSinkProvider _nextSinkProvider;<BR> public IClientChannelSinkProvider Next {<BR>  get { return _nextSinkProvider; }<BR>  set { _nextSinkProvider = value; }<BR> }<BR><BR> IClientChannelSinkProvider _transportSinkProvider;<BR> public IClientChannelSink CreateSink(<BR>  IChannelSender channel,<BR>  String URL,<BR>  Object remoteChannelData) {<BR><BR>  // first save the transport sink provider<BR>  if(_transportSinkProvider == null)<BR>   _transportSinkProvider = _nextSinkProvider;<BR><BR>  String format = remoteChannelData as String;<BR>  if(format == null)<BR>   throw new Exception("Client formatter specification required.");<BR><BR>  // create the appropriate formatter sink provider<BR>  IClientChannelSinkProvider  formatterSinkProvider= null;<BR>  if(String.Compare(format,"binary", true) == 0)<BR>   formatterSinkProvider = new BinaryClientFormatterSinkProvider();<BR>  else<BR>  if(String.Compare(format,"soap", true) == 0)<BR>   formatterSinkProvider = new SoapClientFormatterSinkProvider();<BR>  else<BR>   throw new Exception("Channel data required.");<BR><BR>  // insert it between this provider and the transport provider<BR>  formatterSinkProvider.Next = _transportSinkProvider;<BR>  _nextSinkProvider = formatterSinkProvider;<BR><BR>  return formatterSinkProvider.CreateSink(channel, URL, null);<BR> }<BR>}

For the remoting system to accept this client channel sink provider the client's config file must be changed like so:

XML
<clientProviders><BR> <formatter type="RemotingHelper.AbstractClientFormatterSinkProvider, <BR>            RemotingHelper"/><BR></clientProviders>

This reflects that the assembly 'RemotingHelper.dll' contains the class 'RemotingHelper.AbstractClientFormatterSinkProvider'.

And inside the client app the code connecting to the servers Knossos and Zakros is as follows:

C#
String urlKnossos = "tcp://knossos:9000/KnossosObject.rem";<BR>KnossosObject objKnossos = (KnossosObject)RemotingServices.Connect(<BR> typeof(KnossosObject), urlKnossos, "binary");<BR> <BR>String urlZakros = "tcp://zakros:9000/ZakrosObject.soap";<BR>ZakrosObject objZakros = (ZakrosObject)RemotingServices.Connect(<BR> typeof(ZakrosObject), urlZakros, "soap"); 

The third parameters 'binary' and 'soap' are passed to the provider's CreateSink method as remoteChannelData, providing the necessary information to dynamically insert the appropriate formatter sink provider.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I am a consultant, trainer, software archtect/engineer, since the early 1980s, working in the greater area of Boston, MA, USA.

My work comprises the entire spectrum of software, shrink-wrapped applications, IT client-server, systems and protocol related work, compilers and operating systems, and more ....

I am currently focused on platform development for distributed computing in service oriented data centers.

Comments and Discussions

 
QuestionHow can one contact Wytek Szymanski, the author of this arcticle? Pin
YossiMimon1-May-08 23:55
YossiMimon1-May-08 23:55 
Questionseveral questions about the introduced mechanism for clerification [modified] Pin
YossiMimon1-May-08 4:02
YossiMimon1-May-08 4:02 
GeneralPerfect Pin
~Sheldon7-Aug-06 10:28
~Sheldon7-Aug-06 10:28 
GeneralReally helpful to understand custom .NET Remoting channels Pin
Stefano Franco1-Dec-05 21:24
Stefano Franco1-Dec-05 21:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.