-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNewtonsoftJsonFormatter.cs
386 lines (331 loc) · 14 KB
/
NewtonsoftJsonFormatter.cs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace JonGrant.Json
{
// Based on http://itq.nl/replacing-wcf-datacontractjsonserializer-with-newtonsoft-jsonserializer/
// See http://jongrant.org/2016/07/12/microsofts-wcf-datacontractjsonserializer-sucks/
public class NewtonsoftJsonDispatchFormatter : IDispatchMessageFormatter
{
OperationDescription operation;
Dictionary<string, int> parameterNames;
public NewtonsoftJsonDispatchFormatter(OperationDescription operation, bool isRequest)
{
this.operation = operation;
if (isRequest)
{
int operationParameterCount = operation.Messages[0].Body.Parts.Count;
if (operationParameterCount > 1)
{
this.parameterNames = new Dictionary<string, int>();
for (int i = 0; i < operationParameterCount; i++)
{
this.parameterNames.Add(operation.Messages[0].Body.Parts[i].Name, i);
}
}
}
}
public void DeserializeRequest(Message message, object[] parameters)
{
object bodyFormatProperty;
if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) ||
(bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw)
{
throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?");
}
var bodyReader = message.GetReaderAtBodyContents();
bodyReader.ReadStartElement("Binary");
byte[] rawBody = bodyReader.ReadContentAsBase64();
var ms = new MemoryStream(rawBody);
var sr = new StreamReader(ms);
var serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Converters.Add(new StringEnumConverter { AllowIntegerValues = false, CamelCaseText = false });
if (parameters.Length == 1)
{
// single parameter, assuming bare
parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type);
}
else
{
// multiple parameter, needs to be wrapped
Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr);
reader.Read();
if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject)
{
throw new InvalidOperationException("Input needs to be wrapped in an object");
}
reader.Read();
while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName)
{
var parameterName = reader.Value as string;
reader.Read();
if (this.parameterNames.ContainsKey(parameterName))
{
int parameterIndex = this.parameterNames[parameterName];
parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type);
}
else
{
reader.Skip();
}
reader.Read();
}
reader.Close();
}
sr.Close();
ms.Close();
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
return FormatObjectAsMessage(result, messageVersion, operation.Messages[1].Action, HttpStatusCode.OK);
}
public static Message FormatObjectAsMessage(object obj, MessageVersion messageVersion, string action, HttpStatusCode statusCode)
{
byte[] body;
var serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter { AllowIntegerValues = false, CamelCaseText = false });
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms, Encoding.UTF8))
{
using (JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
{
//writer.Formatting = Newtonsoft.Json.Formatting.Indented;
serializer.Serialize(writer, obj);
sw.Flush();
body = ms.ToArray();
}
}
}
Message replyMessage = Message.CreateMessage(messageVersion, action, new RawBodyWriter(body));
replyMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
var respProp = new HttpResponseMessageProperty();
respProp.Headers[HttpResponseHeader.ContentType] = "application/json";
respProp.StatusCode = statusCode;
replyMessage.Properties.Add(HttpResponseMessageProperty.Name, respProp);
return replyMessage;
}
}
public class RawBodyWriter : BodyWriter
{
byte[] content;
public RawBodyWriter(byte[] content)
: base(true)
{
this.content = content;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Binary");
writer.WriteBase64(content, 0, content.Length);
writer.WriteEndElement();
}
}
public class NewtonsoftJsonBehavior : WebHttpBehavior
{
private bool includeExceptionDetailInFaults = false;
public NewtonsoftJsonBehavior(bool includeExceptionDetailInFaults)
{
this.includeExceptionDetailInFaults = includeExceptionDetailInFaults;
}
public override void Validate(ServiceEndpoint endpoint)
{
base.Validate(endpoint);
var elements = endpoint.Binding.CreateBindingElements();
var webEncoder = elements.Find<WebMessageEncodingBindingElement>();
if (webEncoder == null)
{
throw new InvalidOperationException("This behavior must be used in an endpoint with the WebHttpBinding (or a custom binding with the WebMessageEncodingBindingElement).");
}
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
this.ValidateOperation(operation);
}
}
protected override IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
if (this.IsGetOperation(operationDescription))
{
// no change for GET operations
return base.GetRequestDispatchFormatter(operationDescription, endpoint);
}
if (operationDescription.Messages[0].Body.Parts.Count == 0)
{
// nothing in the body, still use the default
return base.GetRequestDispatchFormatter(operationDescription, endpoint);
}
return new NewtonsoftJsonDispatchFormatter(operationDescription, true);
}
protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
if (operationDescription.Messages.Count == 1 || operationDescription.Messages[1].Body.ReturnValue.Type == typeof(void))
{
return base.GetReplyDispatchFormatter(operationDescription, endpoint);
}
else
{
return new NewtonsoftJsonDispatchFormatter(operationDescription, false);
}
}
private void ValidateOperation(OperationDescription operation)
{
if (operation.Messages.Count > 1)
{
if (operation.Messages[1].Body.Parts.Count > 0)
{
throw new InvalidOperationException("Operations cannot have out/ref parameters.");
}
}
WebMessageBodyStyle bodyStyle = this.GetBodyStyle(operation);
int inputParameterCount = operation.Messages[0].Body.Parts.Count;
if (!this.IsGetOperation(operation))
{
var wrappedRequest = bodyStyle == WebMessageBodyStyle.Wrapped || bodyStyle == WebMessageBodyStyle.WrappedRequest;
if (inputParameterCount == 1 && wrappedRequest)
{
throw new InvalidOperationException("Wrapped body style for single parameters not implemented in this behavior.");
}
}
var wrappedResponse = bodyStyle == WebMessageBodyStyle.Wrapped || bodyStyle == WebMessageBodyStyle.WrappedResponse;
var isVoidReturn = operation.Messages.Count == 1 || operation.Messages[1].Body.ReturnValue.Type == typeof(void);
if (!isVoidReturn && wrappedResponse)
{
throw new InvalidOperationException("Wrapped response not implemented in this behavior.");
}
}
private WebMessageBodyStyle GetBodyStyle(OperationDescription operation)
{
var wga = operation.Behaviors.Find<WebGetAttribute>();
if (wga != null)
{
return wga.BodyStyle;
}
var wia = operation.Behaviors.Find<WebInvokeAttribute>();
if (wia != null)
{
return wia.BodyStyle;
}
return this.DefaultBodyStyle;
}
private bool IsGetOperation(OperationDescription operation)
{
var wga = operation.Behaviors.Find<WebGetAttribute>();
if (wga != null)
{
return true;
}
var wia = operation.Behaviors.Find<WebInvokeAttribute>();
if (wia != null)
{
return wia.Method == "HEAD";
}
return false;
}
public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new NewtonsoftJsonErrorHandler(includeExceptionDetailInFaults));
}
}
[DataContract(Name = "error")]
public class JsonErrorMessage
{
[DataMember(Name = "message")]
public string Message
{
get;
set;
}
[DataMember(Name = "stackTrace", EmitDefaultValue = false)]
public string StackTrace
{
get;
set;
}
[DataMember(Name = "inner", EmitDefaultValue = false)]
public JsonErrorMessage Inner
{
get;
set;
}
public JsonErrorMessage(string message)
{
this.Message = message;
}
public JsonErrorMessage(string message, string stackTrace)
{
this.Message = message;
this.StackTrace = stackTrace;
}
public static JsonErrorMessage FromException(Exception ex, bool includeExceptionDetailInFaults)
{
JsonErrorMessage result = null;
if (includeExceptionDetailInFaults)
{
result = new JsonErrorMessage(ex.Message, ex.StackTrace);
if (ex.InnerException != null) result.Inner = FromException(ex.InnerException, includeExceptionDetailInFaults);
}
else
{
result = new JsonErrorMessage(ex.Message);
}
return result;
}
}
public class NewtonsoftJsonErrorHandler : IErrorHandler
{
private bool includeExceptionDetailInFaults = false;
public NewtonsoftJsonErrorHandler(bool includeExceptionDetailInFaults)
{
this.includeExceptionDetailInFaults = includeExceptionDetailInFaults;
}
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
var wrapped = new { error = JsonErrorMessage.FromException(error, includeExceptionDetailInFaults) };
var statusCode = HttpStatusCode.InternalServerError;
if (error is WebFaultException) statusCode = ((WebFaultException)error).StatusCode;
fault = NewtonsoftJsonDispatchFormatter.FormatObjectAsMessage(wrapped, version, "fault", statusCode);
}
}
public class NewtonsoftJsonBehaviorExtension : BehaviorExtensionElement
{
[ConfigurationProperty("includeExceptionDetailInFaults")]
public bool IncludeExceptionDetailInFaults
{
get { return Boolean.Parse(this["includeExceptionDetailInFaults"].ToString()); }
set { this["includeExceptionDetailInFaults"] = value; }
}
public override Type BehaviorType
{
get { return typeof(NewtonsoftJsonBehavior); }
}
protected override object CreateBehavior()
{
return new NewtonsoftJsonBehavior(this.IncludeExceptionDetailInFaults);
}
}
public class NewtonsoftJsonContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}
}