Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Dynamic insertion of a client channel sink provider

, 10 Apr 2003 CPOL
Workaround to specify a desired channel for connecting to a remote object
Remoting Helper Solution
Remoting Helper Solution.suo
using System;
using System.IO;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;

namespace RemotingHelper {
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class AbstractClientFormatterSinkProvider : IClientFormatterSinkProvider {
		public AbstractClientFormatterSinkProvider() { }
		public AbstractClientFormatterSinkProvider(IDictionary props, ICollection providerData) { }

		IClientChannelSinkProvider _nextSinkProvider;
		public IClientChannelSinkProvider Next {
			get { return _nextSinkProvider; }
			set { _nextSinkProvider = value; }

		IClientChannelSinkProvider _transportSinkProvider;
		public IClientChannelSink CreateSink(
			IChannelSender channel,
			String URL,
			Object remoteChannelData) {

			// first save the transport sink provider
			if(_transportSinkProvider == null)
				_transportSinkProvider = _nextSinkProvider;

			String format = remoteChannelData as String;
			if(format == null)
				throw new Exception("Client formatter specification required.");

			// create the appropriate formatter sink provider
			IClientChannelSinkProvider  formatterSinkProvider= null;
			if(String.Compare(format,"binary", true) == 0)
				formatterSinkProvider = new BinaryClientFormatterSinkProvider();
			if(String.Compare(format,"soap", true) == 0)
				formatterSinkProvider = new SoapClientFormatterSinkProvider();
				throw new Exception("Channel data required.");

			// insert it between this provider and the transport provider
			formatterSinkProvider.Next = _transportSinkProvider;
			_nextSinkProvider = formatterSinkProvider;

			return formatterSinkProvider.CreateSink(channel, URL, null);

	internal class AbstractClientFormatterSink : 
		IClientFormatterSink, IMessageSink, IClientChannelSink, IChannelSinkBase {

		public AbstractClientFormatterSink(IClientChannelSink nextSink) {
			this.nextSink = nextSink;

		public IMessageCtrl AsyncProcessMessage(IMessage msg,IMessageSink replySink) {
			return NextSink.AsyncProcessMessage(msg,replySink); 

		public void AsyncProcessRequest(
			IClientChannelSinkStack sinkStack,
			IMessage msg,
			ITransportHeaders headers,
			Stream stream) {
			sinkStack.Push(this, null);
			nextSink.AsyncProcessRequest(sinkStack, msg, headers, stream);

		public void AsyncProcessResponse(
			IClientResponseChannelSinkStack sinkStack,
			Object state,
			ITransportHeaders headers,
			Stream stream) {
		public Stream GetRequestStream(
			IMessage msg,
			ITransportHeaders headers) {
			return (nextSink != null) ? nextSink.GetRequestStream(msg,headers) : null;
		public void ProcessMessage(
			IMessage msg,
			ITransportHeaders requestHeaders,
			Stream requestStream,
			out ITransportHeaders responseHeaders,
			out Stream responseStream) {
			nextSink.ProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream);
		public IMessage SyncProcessMessage(IMessage msg) {
			IMessage rmsg = NextSink.SyncProcessMessage(msg);
			return rmsg;

		Hashtable prop = new Hashtable();
		public IDictionary Properties {
			get { return prop; }

		IClientChannelSink nextSink;
		public IClientChannelSink NextChannelSink {
			get { return nextSink; }
		public IMessageSink NextSink {
			get {
				for(IClientChannelSink next = nextSink; next != null; next = next.NextChannelSink)
				if(next is  IMessageSink)
					return (IMessageSink)next;
				return null;


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Wytek Szymanski
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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150128.1 | Last Updated 11 Apr 2003
Article Copyright 2003 by Wytek Szymanski
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid