Sharing a serialized object between applications

Put the definition for the serializable object into a separate assembly and then add a reference to the shared assembly to each project. (The formatter is adding a reference to the assembly in your first project - they must in fact refer to the same class, not just an identical copy of the class)


If you are using BinaryFormatter, then it includes the full type name in the data, which includes the assembly that the DTO is in (types are always defined by their assembly). One option here is to create a separate DTO library that you reference from each - but note that BinaryFormatter is still pretty unreliable when it comes to versioning: I have seen people lose data because they edited their DTO and everything stopped working.

I would strongly advise using a non-type-dependent serializer; for example, XmlSerializer / DataContractSerializer / JSON.NET / ServiceStack's JsonSerializer, or protobuf-net. All of these will work fine but importantly will not fight you, in two different ways:

  • they are very versioning-friendly
  • they don't care if you move types between assemblies

Even with this, it is probably most convenient to maintain a separate DTO assembly for the serialized types, but it doesn't force you to. Ultimately, since these serializers are all happy to work cross-OS / cross-version / cross-language / cross-CPU, the mere fact of "different assemblies" is very much a "meh, whatever".

Key takeaway: BinaryFormatter can be brittle. I never recommend it for anything except in-flight data (for example, remoting between two AppDomain instances). I certainly wouldn't use it for anything that is persisted for any length of time, because I simply can't guarantee that I'll be able to reload it in the future.