What happens under the cover when you return a Stream from a remote object via .NET Remoting
Asked Answered
P

1

9

I'm returning Streams from a remote service (.NET Remoting). But Streams are also disposables which as we all know are ment to be disposed.

I could call Dispose on the client side once I finished consuming those. However, I would like to know what exactly happens under the cover when I return a Stream from a remote object.

Especially:

  1. Should I better read everything into a byte[] and return that instead of a Stream?
  2. Or is .NET remoting doing exactly that for me under the covers anyway?
  3. If not, how is returning a Stream different from returning a byte[]? In the end, .NET Remoting must somehow serialize the data anyway?
  4. Does calling Dispose on the client side even has any affect at all? Is there any magical connection between the object on the client side and the object on the serverside? I think once it's deserialized behind the covers, there is no sense in calling Dispose() on the client side or is there?

I'm answering to Mike Bild here because I also want to improve the question a little

Ok, so the stream talking back to the server is (for me at least) unexpected.

To cosume a remote object one has to do something like this:

public static class ServiceFactory <T>
{   
    public static T CreateProxy()
    {
        Type interfaceType = typeof(T);

        string uri = ApplicationServer.ServerURL + interfaceType.FullName;

        return (T)Activator.GetObject(interfaceType, uri);
    }
}

So you are explicitly reaching out for a specific remote object at some URI to consume. And when a method on that remote object returns an object that inherits from MarshallByRefObject that means it automatically is associated with the object on the remote side? Ok, that should be easy to reproduce with a test object I build myself. So this also means I should call Dispose on the client side and it get's proxied back to the object on the server side?

Phillada answered 17/10, 2012 at 14:32 Comment(3)
MarshalByRefObject should ever implement IDisposable for lifetime management on client side. Yes, invoke Dispose() on client side dispose the "shared" object on server side.Paradies
my recommendation - implement IDisposable for all types that involves with MBR objects until you materialize the stream to memory like array of bytes or a new stream like a fileParadies
Ok, you are 100 % correct :) I just returned a custom object that inherits from MBR and set breakpoints to make it visible. I always have an insane demand to make things visible :) The funny aspect is: I wrote a generic ApplicationServer including a two way NotificationService in 2009 that wouldn't be possible without things working this way. mycsharp.de/wbb2/thread.php?threadid=75670 I must have forgotten about that in between. Anyway, great you took the time to refresh my brain!Phillada
P
3

A Stream is a MarshalByRefObject. Thats a special kind. It's a proxy.

  1. You can work with both in .NET remoting
  2. No, it's an other type over a generated proxy - look for inheritance from MarshalByRefObject
  3. http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx
  4. Yes, a little bit magic over the proxy class
Paradies answered 17/10, 2012 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.