denghongcai / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Serialize Exception #400

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Where I should start and what is extension point to add new pure valid like in 
guidelines .NET Exception?

By default all common exceptions are
  [Serializable]
  public class Exception : ISerializable

  [Serializable]
  public class XyzException : ISerializable

Pattern is clear. Can add serialization of this standard into protobuf-net? 
Or I would like to start add some classes to support it. Where I should start?

Our use case:
We use WCF locally and propagate exceptions via NetDataContractSerializer. Want 
to do the same with protobuf.

Original issue reported on code.google.com by Dzmitry....@gmail.com on 6 Aug 2013 at 10:02

GoogleCodeExporter commented 9 years ago
Can you be more explicit? Is your intent to serialize an exception with 
protobuf-net? or are you saying that the exceptions *in* protobuf-net should be 
serializable (which: I believe they are).

I'm going to assume the former (since the latter should already work). Assuming 
that, then exceptions simply contain a lot of information that is not highly 
amenable to contract based serializers (note: BinaryFormatter and 
NetDataContractSerializer are *not* contract-based serializers). You will have 
similar problems trying to pass an exception via JSON, XML, etc. It would be 
far preferable, IMO, to simply return information about the issue *as data* 
(rather than an exception).

This reality is actually *encouraged* in WCF, via the separation of "faults" 
from "exceptions". If you write a **fault** contract, it should be perfectly 
fine to serialize that fault. Faults are *intended* to be 
contract-serializable; exceptions are not. My advice, then: use faults, not 
contracts.

Original comment by marc.gravell on 6 Aug 2013 at 11:20

GoogleCodeExporter commented 9 years ago
Our local (single machine) WCF services throw different serializable exceptions.
We have hooks (IClientMessageInspector and I* Behaviors) to translate any 
XyzException into custom Fault details (serialized XyzException) and throw this 
XyzException on client side after. NetDataContractSerializer  is used for 
serialization. I want try to use protobuf-net for this.

I know why Faults are good. But in local scenario for .NET apps (Remoting is 
legacy and not used by us) our approach is good by reducing cognitive overhead 
on developers and ease debugging.

Original comment by Dzmitry....@gmail.com on 8 Aug 2013 at 10:15

GoogleCodeExporter commented 9 years ago
So: to be clear - what change would you like to see in protobuf-net? What 
doesn't work? protobuf-net doesn't have specific handling for exceptions, and 
I'm not sure that I would want to add them...

Original comment by marc.gravell on 8 Aug 2013 at 11:39

GoogleCodeExporter commented 9 years ago
Is  it possible to write such support code? Can I plug it externally? What 
classes look at? Or if I decide to have my own version of protobuf-net, what 
files I should look inside? It could be very helpful if protobuf-net having git 
or hg mirror for ease of forking and merging.

Original comment by Dzmitry....@gmail.com on 8 Aug 2013 at 3:36

GoogleCodeExporter commented 9 years ago
Again: what is it that you want to *do*? What would you have it do differently? 
It is hard to advise until I understand what you want to achieve.

Original comment by marc.gravell on 8 Aug 2013 at 3:44

GoogleCodeExporter commented 9 years ago
Given Exception
Or Given valid XyzException ([Serializable],ISerializable,protected 
Exception(SerializationInfo info, StreamingContext context),public virtual void 
GetObjectData(SerializationInfo info, StreamingContext context))
When I serialize it with protobuf-net
Then I can deserialize it

        [Test]
        //[ExpectedException(typeof(InvalidOperationException))]
        [Description("Default behaviour of Protobuf serializers does not handles Exception")]
        public void SerializeException()
        {
            var stream = new MemoryStream();
            var ex = new Exception("serializable exception");
            var model = TypeModel.Create();
            model.Add(typeof (Exception), true);
            model.Serialize(stream, ex);
            stream.Position = 0;
            var deserialized = model.Deserialize(stream, null, typeof (Exception));
            //var info = new SerializationInfo(typeof(Exception), new FormatterConverter());
            //var context = new StreamingContext(StreamingContextStates.CrossProcess);
            //ex.GetObjectData(info, context);
            //Serializer.Serialize(info, context, ex);
        }

Original comment by Dzmitry....@gmail.com on 8 Aug 2013 at 4:17