开发者

How-to proxy-remote a .NET IEnumerable with fixed-size batches over the network?

.NET remoting can generate transparent proxy remotes for an interface with serializable members, such as:

public interface INetworkInterface {
  bool login(string username, string password);
  bool ExecuteSomething(string command);
}

I'd like to supply a custom serializer and active proxy-remoter for any IEnumerator<T> return result. My custom proxy-remoter would handle batching a specific number of elements at a time over the network. For example, given the following interface:

public interface INetworkInterface2 {
    IEnumerable<string> ExecuteSomething(string command);
}

I would like to supply a custom serializer AND proxy which is automatically used anytime an IEnumerator<T> where T:ISerializable appears in a remoted interface, and would serialize IEnumerator<string> into a client-side EnumeratorBatchHandlerClient<string> which has a .NET remote-proxy to a server-side EnumeratorBatchHandlerServer<string>.

I'm thinking the client-batch-handler would look something like this:

class EnumeratorBatchHandlerClient<T> : IEnumerable<T> {
    List<T> batched_results = new List<T>();
    EnumerableBatchHandlerServer server_proxy;    
    T cur = null;
    bool MoveNext() {
      if (batched_results.Count == 0) {
          batched_results = server_proxy.getNextBatch().ToList();
          if (batched_results.Count == 0) {
            return false;  // we are really out of results
          }
      } 
      cur = batched_results[0]; batched_results.RemoveAt(0);
      return true;
    }
    public T Current {  get { return cur; } }

}

Is there a way to cause .NET Remoting to transparently produce and link my custom client/server "ienumerable batch proxy" when IEnumerable appears in a proxied interface?

Update: One crafty idea I had for doing this was to build a RealProxy that would do the work of inserting the stubs, and stick this in front of my remoted class. I figured if I made the client-handler serializable and the server-handler marshalbyref it would send the client-handler to the开发者_如何学编程 cilent-side and let it talk remotely to the server-handler. I built such a RealProxy, and it does faithfully instantiate my EnumeratorClientBatchHandler<> and EnumeratorServerBatchHandler<> anytime it sees an IEnumerator. However, I can't "remote" the RealProxy with .NET remoting, because it can't be a subclass of MarshalByRefObject and RealProxy at the same time.

The only path forward I see is to build my own remoting system which doesn't require interface implementations to be subclasses of MarshalByRefObject in order to proxy remote them. I don't see any reason this isn't possible, since it looks like RealProxy can proxy any interface type. (only .NET remoting is checking that the underlying type is MarshalByRefObject). As a side-benefit, I think it would be nice to have a remoting system that uses the new C# 5.0 async system to allow an async handler to service a "standard" interface.


This Q/A below seems semi-related, and talks about using a custom serializer (protobuf) with WCF. However, this is different than what I'm looking for in a few ways. (1) it still serializes all data to a binary stream, there is no 'ongoing proxy channel' such as I need above to continue to issue proxy-batch calls through the remoting channel, (2) The manual WCF pattern of translating return results to EndpointAddress10 seems incompatible with the above pattern.

How to use custom serialization during .NET remoting?

Of course I know I can make a remoting interface which IS the batching interface, and then make clients manually deal with batching... or have them manually wrap the batching interface in their own instance of the EnumerableBatchHandlerClient. I'm trying to figure out how to do this in a transparent way because I'm going to have many many interfaces vended through a particular system, IEnumerables will be very common, and I'd like the remoting to be 'automagic' for anything that looks like a basic-datatype call or an IEnumerable (with batch support).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜