Note that there are some explanatory texts on larger screens.

plurals
  1. POusing MsgPack with Servicestack: how do I do KnownType?
    primarykey
    data
    text
    <p>I'm attempting to support the MessagePack protocol in my current Servicestack implementation. I need it to support (de)serializing a list of ISegment defined like this:</p> <pre><code>[KnownType(typeof(ArcSegment)), KnownType(typeof(LineSegment))] public class PathRequest { public List&lt;ISegment&gt; Segments {get;set;} } public interface ISegment { Point StartPoint {get;set;} Point EndPoint {get;set;} } public class ArcSegment: ISegment {...} public class LineSegment: ISegment {...} </code></pre> <p>Out of the box it told me that </p> <p>Type 'Asi.Geometry.ISegment' does not have both of default (parameterless) public constructor and public constructor with an Int32 parameter.</p> <p>That's very true. Apparently it does not use the KnownType attribute. After reading around online I discovered that I can make my own serializer. Hence I tried this:</p> <pre><code>class ArcLineSerializer: MessagePackSerializer&lt;ISegment&gt; { private readonly MessagePackSerializer&lt;ArcSegment&gt; _arcSerializer = MessagePackSerializer.Create&lt;ArcSegment&gt;(); private readonly MessagePackSerializer&lt;LineSegment&gt; _lineSerializer = MessagePackSerializer.Create&lt;LineSegment&gt;(); protected override void PackToCore(Packer packer, ISegment objectTree) { if(objectTree is ArcSegment) _arcSerializer.PackTo(packer, (ArcSegment)objectTree); else if (objectTree is LineSegment) _lineSerializer.PackTo(packer, (LineSegment)objectTree); else throw new NotSupportedException(); } protected override ISegment UnpackFromCore(Unpacker unpacker) { var data = unpacker.Data; if (data != null) { if (data.Value.IsTypeOf&lt;ArcSegment&gt;().GetValueOrDefault()) return _arcSerializer.UnpackFrom(unpacker); if (data.Value.IsTypeOf&lt;LineSegment&gt;().GetValueOrDefault()) return _lineSerializer.UnpackFrom(unpacker); throw new NotSupportedException(); } return null; } } </code></pre> <p>Alas, that gives me the same error trying to construct the _arcSerializer. How's it done?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload