I’m trying to replace our use of the binaryformatter for serialization with Protobuf-net. I’ve run into a number of problems and am wondering the recommended way of doing things.
I have a number (lots) of classes I want to serialize. They are in a number of projects in a solution.
If I try to call Serialize from the same project that the class that is to be serialized is in, it works, if they are in different projects, it fails, not knowing the contract. Our classes also use types (structs) defined in other projects. When we try to serialize a class using this struct, we get the same problem with it not knowing about that type. I can manually add the type using PrepareSerializer and it will serialize…but when I try to Deserialize I get an error about partially trusted callers not being allowed.
I tried also, to use the precompile utility, but our classes use private fields with public accessors and I get an error saying it can’t handle private fields, so we can’t use this to replace the binary formatter?
How can I get around these issues?
Edit – this is the stacktrace from the partially trusted callers exception:
"That assembly does not allow partially trusted callers."
at proto_4(Object , ProtoReader )
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source)
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source)
at ProtoBuf.ProtoReader.ReadTypedObject(Object value, Int32 key, ProtoReader reader, Type type)
at ProtoBuf.ProtoReader.ReadObject(Object value, Int32 key, ProtoReader reader)
at proto_2(Object , ProtoReader )
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source)
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source)
at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate)
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context)
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type)
at ProtoBuf.Serializer.Deserialize[T](Stream source)
at BinarySerializationTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at BinarySerializationTest.Program.Main()
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Interesting. I haven’t done much with partial trust setups, but if you could let me know what the stacktrace is when it blows up (and where you are calling from – i.e. whether the assembly calling serialize is different to the assembly calling deserialize), I might be able to add some
[AllowPartiallyTrustedCallers]in a few places to make it work.The precompilation route would have been my other suggestion, but indeed this cannot work with private fields, because the accessibility on a standalone assembly is more rigid.
A third option would be to make the affected
privatefields intointernalfields, and use[assembly:InternalsVisibleTo("NameOfGeneratedAssembly)]to make those fields accessible – and then use “precompiler”. This can then access the fields, and is supported from r602 onwards (which is the most recent build at the time of writing).