[Mono-list] advanced .Net remoting: serialization formatter

Lluis Sanchez lluis@ideary.com
Thu, 27 Feb 2003 00:47:44 +0100


Hi!

> I have one other question:
>
> Is there a way of installing standard serialization translation formatter
to
> a give channel?

Yes, you can implement your own remoting formatter sink and use it in any
channel. You could do something like this:

MyClientFormatterSink clientFormatter = new MyClientFormatterSink();
MyServerFormatterSink serverFormatter = new MyServerFormatterSink();
TcpChannel ch = new TcpChannel(null, clientFormatter, serverFormatter);
ChannelServices.RegisterChannel (ch);

You can also do it using config files.

> What I want is the following:
> System.Drawing.Image for instance is derived from MashalByRefObject, but
> when a proxy version of an image is used (like
Save(stream,ImageFormat.Jpeg)
> or similar) the serialization fails.  Image is not marked as serializable,
> but it would be very easy to do:
>
> public static byte[] ImageToStream(Image image)
> {
> MemoryStream s = new MemoryStream();
> image.Save(s,ImageFormat.Jpeg);
> s.Close();
> return s.GetBuffer();
> }
>
> public static Image StreamToImage(byte[] image)
> {
> return Image.FromStream(new MemoryStream(image));
> }
>
> But the problem is that I might have a structure like:
> class ServerSide : MarshalByRefObject
> {
> public void Method(Image image) { ... }
> }
>
> When I call from the client:
>
> ServerSide s = Activation.GetObject(...) as ServerSide;
> s.Method(Image.FromFile("/tmp/test.gif"));
>
> I would like the formatter to kick in, and automatically serialize the
image
> to a byte[] and de-serialize back.  Is that possible on a channel
formatter
> level, so that I don't have to change the method signature?
>
> This applied for other objects like object[], Hashtable, and others that
> could be serialized very easily in general purpose code.

The serialization infrastructure is very flexible and allows you to make
such custom serializations. However, if you want to use it with remoting,
you'll need to write your own formatter sink, because there is no way to
customize the behavior of the serialization formatter that is used by the
standard binary and soap formatter sinks.

The best way of customizing Image serialization, is to use a serialization
surrogate. You need to create a class that implements
ISerializationSurrogate, which has two methods: GetObjectData and
SetObjectData. Those methods would serialize/deserialize the image as you
want. Then create and instance of SurrogateSelector and register your
surrogate using the Image type. You also need to chain an instance of
RemotingSurrogateSelector to your selector (by calling ChainSelector), which
does the marshalling of MarshalByRefObjects. Finally, assign the selector to
the formatter.

You should look at the implementation of BinaryClientFormatterSink. It will
give you more ideas.

Regards,
Lluis.

>
> - URS C. MUFF
>
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>