Custom .NET Remoting – Easier Than You Think

Posted by on April 1, 2010

The idea of implementing remoting will give most programmers instant heartburn.  Granted, I’m not an expert on all the existing remoting solutions, but I haven’t found one that works intuitively.

All the different solutions I’ve seen require just about double the code.  You write a method once, then write a wrapper for the method to implement remoting. 

Furthermore, they often require special handling to make sure the contextual variables are maintained.  For example, when I was working with CSLA recently, I was appalled to see:

customer = customer.Save();

The Save() method executes remotely, so the customer object that comes back has a separate memory reference.  Surely, we can get around this. 

So I set out to write my own.  I had 4 main goals.  I wanted to be able to:

// Remote any method with no code changes to the business object
Remoting.Execute(customer.Save);

// Executes remotely, and updates the object
Debug.Assert(customer.ID > 0);

// Execute an anonymous method and update the contextual variables
int x = 0;
Remoting.Execute(() => { x = 1; });
Debug.Assert(x == 1);

// Handle anonymous types, generics
var answer = Remoting.Execute(()=>new { y = x + 1 });
Debug.Assert(answer.y == 2);

The initial library has been published to http://remoting.codeplex.com/.  The code works, but needs testing and refractoring.

Categories: Code
Tags: remoting, c#

« C# Imap and Pop3 Library
Beautiful PNG Wait Indicator with Raphaël and jQuery »