Click here to Skip to main content
14,355,397 members

.NET Remoting Message Redirection Channel Sinks

Rate this:
3.38 (20 votes)
Please Sign up or sign in to vote.
3.38 (20 votes)
20 Jun 2003
An upper logic layer transparent way to redirect .NET remoting calls, enabling exposure of .NET remoting services behind firewall/NAT, to anywhere.


.NET remoting is a beautiful framework, but its most serious deficiencies preventing it from being useful in today's Internet environment, is that it requires each node to have a global IP, so direct TCP connections can be made. Other incomplete solutions aimed at this problem exist (GenuineChannel), but they use special transport channels (instead of the standard TCP/HTTP), with large amount of unproven custom code, thus make your code heavier and unstable. These solutions also do not let 2 objects both behind firewall call each other, either.

Reachability channel sinks provide a more lightweight, elegant solution. Remoting objects hosted by machines with only local IPs behind firewalls can be made reachable from anywhere (including those machines behind firewalls themselves), using standard transport channels, by a Message Redirector. The whole process is transparent from upper, logic layers. Asynchronous calls, one-way calls are also supported.

Advantages of .NET remoting

.NET remoting has the the following advantages:

  • Separation of network-code from logic code.
  • Advantage over WebService: Clients can receive events from server. More types can be passed. Higher throughput.

Although .NET remoting is designed to accommodate the need of Enterprise intranet applications, nothing prevents it from being used over the Internet, at least not security problems - it is free from security holes - buffer overrun is impossible in managed code in which .NET remoting framework is entirely written. .NET remoting is ideal for peer-to-peer applications, because it alleviate programmers from writing complex network code that manages complex network topologies.

How to use in your project

Configuration files

  • For hosts who are behind firewall and need to expose objects:
      <add key="RedirectorURL" 
        value="tcp://Redirector's IP:Port/Redirector.rem" />
         <provider type="Reachability.ServerSinkProvider, Reachability" />
           <formatter ref="binary" />
  • For hosts who need to call objects through Redirector:
          <formatter ref="binary" />
             type="Reachability.ClientSinkProvider, Reachability" />
  • For Redirector:
    <wellknown mode="Singleton" 
            type="Reachability.Redirector, Reachability" 
        objectUri="Redirector.rem" />
               <channel ref="tcp" port="Redirector's Port" >
           <formatter ref="binary" />
          <formatter ref="binary" />

Code needed

Add reference of Reachability.dll to your project.

Hosts which need to expose objects via the Redirector must call Reachability.ServerSinkProvider.StartWaitRedirectedMsg() after RemotingConfiguration.Configure(), or after RemotingServices.Marshal(). The effect of this call is to start receiving messages from the Redirector.

How it works

There are already many articles on how .NET remoting works and on channel sinks, and I don't have time to repeat such descriptions here. The Reachability sinks have 3 components:

  1. The Redirector service
  2. The "Server" Channel Sink
  3. The "Client" Channel Sink

Basically, server sink adds reachability information (i.e. via which Redirector can we send message to this object) to (ChannelData in ) the ObjRef. When this ObjRef is passed to somewhere, there, the client sink finds out that reachability information attached to the ObjRef, and instead of directly trying to connect to the object, it pass the call to the Redirector. The Redirector will deliver the call appropriately, provided the host which created the ObjRef is listening on the Redirector properly. (This is done by StartWaitRedirectedMsg()).

Here is the heart of the code that makes hosts behind firewall receive calls (without special transport channel):

public void RedirectRequest(Guid slot, bool oneway, 
     ITransportHeaders requestHeaders, byte[] requestStream, 
     out ITransportHeaders responseHeaders, out byte[] responseStream)
   SyncQueue q = reqs[slot] as SyncQueue;  
   if(q==null)reqs.Add(slot, q=new SyncQueue());
   q.Enqueue(new Message(requestHeaders, 
       requestStream, Thread.CurrentPrincipal, oneway));
      q = resps[slot] as SyncQueue;   
      if(q==null)resps.Add(slot, q=new SyncQueue()); 

      Response r = q.Dequeue() as Response;
      responseHeaders = r.responseHeaders;
      responseStream = r.responseStream;
      responseHeaders = null;
      responseStream = null;
   static void WaitForRequest(object o)
      Redirector r = Activator.GetObject(typeof(Reachability.Redirector), 
                     RedirectorUrl) as Reachability.Redirector;
      Redirector.Message m;
            r.GetNextRequest(rdata.Slot, out m);
          // must place this sink after formatter before
          // server transport, so message goes into stream.
            IMessage respMsg;    

                ITransportHeaders respHeader;
                Stream respStream;
            ServerChannelSinkStack stack = 
                  new ServerChannelSinkStack();     
                stack.Push(new ServerSink(theSink),r);
                 new MemoryStream(m.requestStream),
               out respMsg, out respHeader, out respStream);
              new Redirector.Response(respHeader, respStream));
     catch(Exception e){

The node behind firewall will not listen for incoming calls, instead it calls GetNextRequest() and blocks till an incoming call arrives (like the Windows API GetMessage()). The Redirector is blocked until ReturnResponse() is called.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
My name is Zhiheng Cao. I major in Eletronics Engineering, in University of Tokyo. I do part time jobs of computer programming and teaching high school students English and Mathematics.

CodeProject provided me with valuable information for my programming job many times in the past. I hope I can do my part by providing goodies I have.

Comments and Discussions

GeneralMy vote of 1 Pin
Lavrekho21-Aug-11 3:20
memberLavrekho21-Aug-11 3:20 
GeneralTimes out Pin
tnvcode30-Sep-09 16:52
membertnvcode30-Sep-09 16:52 
GeneralRe: Times out Pin
Alexsandro_xpt16-Apr-12 9:27
memberAlexsandro_xpt16-Apr-12 9:27 
QuestionCompression Sink there way? Pin
Alexsandro_xpt9-Aug-09 12:56
memberAlexsandro_xpt9-Aug-09 12:56 
GeneralBecause of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed. Pin
Alexsandro_xpt30-Jun-09 16:12
memberAlexsandro_xpt30-Jun-09 16:12 
GeneralRe: Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed. Pin
Alexsandro_xpt6-Aug-09 14:40
memberAlexsandro_xpt6-Aug-09 14:40 
JokeIt Works! Pin
Spungin8-Mar-09 20:11
memberSpungin8-Mar-09 20:11 
GeneralZHI, please reply if you are still around... Pin
kdubious9-Aug-08 18:10
memberkdubious9-Aug-08 18:10 
QuestionIs their a way to get the client to not open a listening socket Pin
piba6-May-08 11:41
memberpiba6-May-08 11:41 
QuestionStarting client events from server Pin
Damz19-Sep-07 8:13
memberDamz19-Sep-07 8:13 
Generalserver not processing any request when client is not reachable Pin
manish barai23-Jun-07 2:40
membermanish barai23-Jun-07 2:40 
GeneralMore sample code requested Pin
boboruba28-Feb-07 1:12
memberboboruba28-Feb-07 1:12 
GeneralRe: More sample code requested Pin
piba6-May-08 11:26
memberpiba6-May-08 11:26 
GeneralTrying to make it work. Pin
MihaiMarin20-Apr-06 2:59
memberMihaiMarin20-Apr-06 2:59 
GeneralMore details please Pin
Lang Deng17-Oct-05 20:32
memberLang Deng17-Oct-05 20:32 
GeneralI found better stuff : Pin
ajn999931-Jan-05 19:09
memberajn999931-Jan-05 19:09 
GeneralRe: I found better stuff : Pin
Anonymous7-May-05 15:09
memberAnonymous7-May-05 15:09 
GeneralRe: I found better stuff : Pin
off6522-Oct-05 19:26
memberoff6522-Oct-05 19:26 
Generala small question Pin
Member 5992917-May-04 0:03
memberMember 5992917-May-04 0:03 
Generalmistake Pin
Member 5992917-May-04 0:05
memberMember 5992917-May-04 0:05 
GeneralSecurity Exception Pin
Gery Dorazio20-Aug-03 17:29
memberGery Dorazio20-Aug-03 17:29 
GeneralRe: Security Exception Pin
MadShad20047-Apr-04 22:22
memberMadShad20047-Apr-04 22:22 
QuestionIs this solution really useful? Pin
_Bill2002_26-Jun-03 20:05
member_Bill2002_26-Jun-03 20:05 
AnswerRe: Is this solution really useful? Pin
zhi28-Jun-03 6:02
memberzhi28-Jun-03 6:02 
GeneralRe: Is this solution really useful? Pin
_Bill2002_28-Jun-03 9:07
member_Bill2002_28-Jun-03 9:07 

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.

Posted 20 Jun 2003


27 bookmarked