Click here to Skip to main content
15,896,493 members
Articles / Programming Languages / C#

.NET Remoting Customization Made Easy: Custom Sinks

Rate me:
Please Sign up or sign in to vote.
4.86/5 (77 votes)
20 May 200320 min read 212.4K   2.2K   97  
.NET Remoting customization – it doesn't have to be so hard!
//
// Written by Motti Shaked
//
// motti@wincode.net
//
// Version: 1.0.0.0
//
// DISCLAIMER:
//
// You may use this code as you please.
// You may modify and share it freely.
// You may NOT hold me liable for any damage
// caused to you, your company, your neighbors,
// or anyone else as a result of using this code,
// taking ideas from it, dreaming of it,
// or any other use you may find it suitable for.
// Whatever you do with this code is at 
// your own risk.
//
// Enjoy!
// 

using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Collections;

using CustomSinks;

namespace CredentialsSinks
{
	public class DemoCredentialsServerSink : BaseCustomSink
	{
		private Credentials [] credentialsList;

		public DemoCredentialsServerSink(SinkCreationData data)
		{
			SinkProviderData credentialsListConfig = null;

			// find the credentialsList element
			foreach (SinkProviderData d in data.ConfigurationData.Children)
			{
				if (d.Name == "credentialsList")
				{
					credentialsListConfig = d;
					break;
				}
			}

			// if the credentialsList element is not found - nothing to do here
			if (credentialsListConfig == null)
			{
				return;
			
			}

			ArrayList tempCeredentialsList = new ArrayList();

			// iterate through the credentials elements
			// and them to the hashtable
			Credentials credentials;
			foreach (SinkProviderData credConfig in credentialsListConfig.Children)
			{
				if (credConfig.Name != "credentials")
				{
					continue;
				}

				credentials = new Credentials(
					(string)credConfig.Properties["username"], 
					(string)credConfig.Properties["password"]);

				tempCeredentialsList.Add(credentials);
			}

			// switch to array and sort it to enable binary search
			this.credentialsList = (Credentials [])tempCeredentialsList.ToArray(typeof(Credentials));
			Array.Sort(this.credentialsList);
		}

		// only ProcessRequest
		// no need for ProcessResponse
		protected override void ProcessRequest(IMessage message, ITransportHeaders headers, ref Stream stream, ref object state)
		{
			Credentials credentials = message.Properties["DemoCredentials"] as Credentials;
			if (credentials != null && Array.BinarySearch(this.credentialsList, credentials) >= 0)
			{
				// okay
				Console.WriteLine(
					"DemoCredentialsServerSink: retrieved valid credential information: username: {0}, password {1}.",
					credentials.Username, credentials.Password);
				return;
			}

			// not good!
			Console.WriteLine(
				"DemoCredentialsServerSink: retrieved bad credentials or credentials are missing. Throwing an exception.");
			throw new RemotingException("Invalid credentials.");
		}
	}
}

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.

License

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


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions