forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWireServiceClient.cs
More file actions
141 lines (122 loc) · 4.06 KB
/
WireServiceClient.cs
File metadata and controls
141 lines (122 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
namespace ServiceStack.Wire
{
using global::Wire;
using System;
using System.IO;
using System.Runtime.Serialization;
using ServiceStack.Web;
using ServiceStack.Text;
[Obsolete("Use better supported formats like ProtoBuf or MsgPack")]
public class WireServiceClient : ServiceClientBase
{
public override string Format => "x-wire";
public WireServiceClient(string baseUri)
{
SetBaseUri(baseUri);
}
public WireServiceClient(string syncReplyBaseUri, string asyncOneWayBaseUri)
: base(syncReplyBaseUri, asyncOneWayBaseUri) { }
public override void SerializeToStream(IRequest req, object request, Stream stream)
{
if (request == null) return;
try
{
WireFormat.Serialize(req, request, stream);
}
catch (Exception ex)
{
WireFormat.HandleException(ex, request.GetType());
}
}
public override T DeserializeFromStream<T>(Stream stream)
{
try
{
return (T)WireFormat.Deserialize(typeof(T), stream);
}
catch (Exception ex)
{
return (T)WireFormat.HandleException(ex, typeof(T));
}
}
public override string ContentType => MimeTypes.Wire;
public override StreamDeserializerDelegate StreamDeserializer => WireFormat.Deserialize;
}
[Obsolete("Use better supported formats like ProtoBuf or MsgPack")]
public class WireFormat : IPlugin, IWirePlugin
{
public static Serializer WireSerializer = new Serializer(new SerializerOptions(
versionTolerance:true,
preserveObjectReferences:false,
surrogates:null,
serializerFactories:null,
knownTypes:null));
public void Register(IAppHost appHost)
{
appHost.ContentTypes.Register(MimeTypes.Wire,
Serialize,
Deserialize);
}
public static void Serialize(IRequest requestContext, object dto, Stream outputStream)
{
Serialize(dto, outputStream);
}
public static void Serialize(object dto, Stream outputStream)
{
if (dto == null) return;
try
{
WireSerializer.Serialize(dto, outputStream);
}
catch (Exception ex)
{
HandleException(ex, dto.GetType());
}
}
public static object Deserialize(Type type, Stream fromStream)
{
try
{
return WireSerializer.Deserialize(fromStream);
}
catch (Exception ex)
{
return HandleException(ex, type);
}
}
/// <summary>
/// MsgPack throws an exception for empty DTO's - normalizing the behavior to
/// follow other types and return an empty instance.
/// </summary>
/// <param name="ex"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object HandleException(Exception ex, Type type)
{
if (ex is SerializationException
&& ex.Message.Contains("does not have any serializable fields nor properties"))
return type.CreateInstance();
throw ex;
}
}
public static class WireExtensions
{
private static readonly Serializer serializer = new Serializer();
public static byte[] ToWire<T>(this T obj)
{
using (var ms = MemoryStreamFactory.GetStream())
{
serializer.Serialize(obj, ms);
var bytes = ms.ToArray();
return bytes;
}
}
public static T FromWire<T>(this byte[] bytes)
{
using (var ms = MemoryStreamFactory.GetStream(bytes))
{
return serializer.Deserialize<T>(ms);
}
}
}
}