Pregunta

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?

¿Fue útil?

Solución

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top