You are here: Usage Pitfalls > Storing Marshalbyref Objects

Storing MarshalByRef Objects

Problem

MarshalByRef objects from .NET Remoting can not be stored by db4o.

Reason

MarshalByRef objects are not really objects. They are proxy objects to instances, which live on another machine. There are 2 cases to distinguish:

  1. If the object instance is local, then the object is storable without an exception unless a remote client has a lifetime lease on the object instance.
  2. If the object instance is remote, then you're actually dealing with a DCOM proxy, which cannot be safely stored in db4o, since the remote object lifetime would expire as soon as the object was garbage collected.

Solution

1. Use marshal by value technology for the persistent object. In this case the object implements ISerializable interface and can be stored by db4o without any problems. The object is created on the client and is passed to the server using MarshalByRef proxy.

2. If using MarshalByRef object is mandatory for you, use the following configuration:

configuration.Common()
    .ObjectClass("System.Runtime.Remoting.ServerIdentity, mscorlib")
    .Translate(new TTransient());
configuration.Common()
     .ObjectClass("System.Threading.TimerCallback,mscorlib")
     .Translate(new TTransient());

The TTransient translator will prevent instances of ServerIdentity and TimerCallback from being stored within a db4o database. These classes are protected internal classes within the .NET Framework. When retrieving your MarshalByRef objects from db4o, you will need to re-marshal them.

Note: in many cases using db4o client-server version can be a better option for a remote persistence.