This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
XmlDataContractSerializerOutputFormatter.cs
290 lines (250 loc) · 11.2 KB
/
XmlDataContractSerializerOutputFormatter.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNetCore.Mvc.Formatters.Xml;
using Microsoft.AspNetCore.Mvc.Formatters.Xml.Internal;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Mvc.Formatters
{
/// <summary>
/// This class handles serialization of objects
/// to XML using <see cref="DataContractSerializer"/>
/// </summary>
public class XmlDataContractSerializerOutputFormatter : TextOutputFormatter
{
private readonly ConcurrentDictionary<Type, object> _serializerCache = new ConcurrentDictionary<Type, object>();
private readonly ILogger _logger;
private DataContractSerializerSettings _serializerSettings;
/// <summary>
/// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>
/// with default <see cref="XmlWriterSettings"/>.
/// </summary>
public XmlDataContractSerializerOutputFormatter()
: this(FormattingUtilities.GetDefaultXmlWriterSettings())
{
}
/// <summary>
/// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>
/// with default <see cref="XmlWriterSettings"/>.
/// </summary>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public XmlDataContractSerializerOutputFormatter(ILoggerFactory loggerFactory)
: this(FormattingUtilities.GetDefaultXmlWriterSettings(), loggerFactory)
{
}
/// <summary>
/// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>.
/// </summary>
/// <param name="writerSettings">The settings to be used by the <see cref="DataContractSerializer"/>.</param>
public XmlDataContractSerializerOutputFormatter(XmlWriterSettings writerSettings)
: this(writerSettings, loggerFactory: null)
{
}
/// <summary>
/// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>.
/// </summary>
/// <param name="writerSettings">The settings to be used by the <see cref="DataContractSerializer"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public XmlDataContractSerializerOutputFormatter(XmlWriterSettings writerSettings, ILoggerFactory loggerFactory)
{
if (writerSettings == null)
{
throw new ArgumentNullException(nameof(writerSettings));
}
SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationXml);
SupportedMediaTypes.Add(MediaTypeHeaderValues.TextXml);
SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyXmlSyntax);
WriterSettings = writerSettings;
_serializerSettings = new DataContractSerializerSettings();
WrapperProviderFactories = new List<IWrapperProviderFactory>()
{
new SerializableErrorWrapperProviderFactory(),
};
WrapperProviderFactories.Add(new EnumerableWrapperProviderFactory(WrapperProviderFactories));
_logger = loggerFactory?.CreateLogger(GetType());
}
/// <summary>
/// Gets the list of <see cref="IWrapperProviderFactory"/> to
/// provide the wrapping type for serialization.
/// </summary>
public IList<IWrapperProviderFactory> WrapperProviderFactories { get; }
/// <summary>
/// Gets the settings to be used by the XmlWriter.
/// </summary>
public XmlWriterSettings WriterSettings { get; }
/// <summary>
/// Gets or sets the <see cref="DataContractSerializerSettings"/> used to configure the
/// <see cref="DataContractSerializer"/>.
/// </summary>
public DataContractSerializerSettings SerializerSettings
{
get => _serializerSettings;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_serializerSettings = value;
}
}
/// <summary>
/// Gets the type to be serialized.
/// </summary>
/// <param name="type">The original type to be serialized</param>
/// <returns>The original or wrapped type provided by any <see cref="IWrapperProvider"/>s.</returns>
protected virtual Type GetSerializableType(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var wrapperProvider = WrapperProviderFactories.GetWrapperProvider(new WrapperProviderContext(
type,
isSerialization: true));
return wrapperProvider?.WrappingType ?? type;
}
/// <inheritdoc />
protected override bool CanWriteType(Type type)
{
if (type == null)
{
return false;
}
return GetCachedSerializer(GetSerializableType(type)) != null;
}
/// <summary>
/// Create a new instance of <see cref="DataContractSerializer"/> for the given object type.
/// </summary>
/// <param name="type">The type of object for which the serializer should be created.</param>
/// <returns>A new instance of <see cref="DataContractSerializer"/></returns>
protected virtual DataContractSerializer CreateSerializer(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
try
{
// Verify that type is a valid data contract by forcing the serializer to try to create a data contract
FormattingUtilities.XsdDataContractExporter.GetRootElementName(type);
// If the serializer does not support this type it will throw an exception.
return new DataContractSerializer(type, _serializerSettings);
}
catch (Exception ex)
{
_logger?.FailedToCreateDataContractSerializer(type.FullName, ex);
// We do not surface the caught exception because if CanWriteResult returns
// false, then this Formatter is not picked up at all.
return null;
}
}
/// <summary>
/// Creates a new instance of <see cref="XmlWriter"/> using the given <see cref="TextWriter"/> and
/// <see cref="XmlWriterSettings"/>.
/// </summary>
/// <param name="writer">
/// The underlying <see cref="TextWriter"/> which the <see cref="XmlWriter"/> should write to.
/// </param>
/// <param name="xmlWriterSettings">
/// The <see cref="XmlWriterSettings"/>.
/// </param>
/// <returns>A new instance of <see cref="XmlWriter"/></returns>
public virtual XmlWriter CreateXmlWriter(
TextWriter writer,
XmlWriterSettings xmlWriterSettings)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (xmlWriterSettings == null)
{
throw new ArgumentNullException(nameof(xmlWriterSettings));
}
// We always close the TextWriter, so the XmlWriter shouldn't.
xmlWriterSettings.CloseOutput = false;
return XmlWriter.Create(writer, xmlWriterSettings);
}
/// <summary>
/// Creates a new instance of <see cref="XmlWriter"/> using the given <see cref="TextWriter"/> and
/// <see cref="XmlWriterSettings"/>.
/// </summary>
/// <param name="context">The formatter context associated with the call.</param>
/// <param name="writer">
/// The underlying <see cref="TextWriter"/> which the <see cref="XmlWriter"/> should write to.
/// </param>
/// <param name="xmlWriterSettings">
/// The <see cref="XmlWriterSettings"/>.
/// </param>
/// <returns>A new instance of <see cref="XmlWriter"/>.</returns>
public virtual XmlWriter CreateXmlWriter(
OutputFormatterWriteContext context,
TextWriter writer,
XmlWriterSettings xmlWriterSettings)
{
return CreateXmlWriter(writer, xmlWriterSettings);
}
/// <inheritdoc />
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (selectedEncoding == null)
{
throw new ArgumentNullException(nameof(selectedEncoding));
}
var writerSettings = WriterSettings.Clone();
writerSettings.Encoding = selectedEncoding;
// Wrap the object only if there is a wrapping type.
var value = context.Object;
var wrappingType = GetSerializableType(context.ObjectType);
if (wrappingType != null && wrappingType != context.ObjectType)
{
var wrapperProvider = WrapperProviderFactories.GetWrapperProvider(new WrapperProviderContext(
declaredType: context.ObjectType,
isSerialization: true));
value = wrapperProvider.Wrap(value);
}
var dataContractSerializer = GetCachedSerializer(wrappingType);
using (var textWriter = context.WriterFactory(context.HttpContext.Response.Body, writerSettings.Encoding))
{
using (var xmlWriter = CreateXmlWriter(context, textWriter, writerSettings))
{
dataContractSerializer.WriteObject(xmlWriter, value);
}
// Perf: call FlushAsync to call WriteAsync on the stream with any content left in the TextWriter's
// buffers. This is better than just letting dispose handle it (which would result in a synchronous
// write).
await textWriter.FlushAsync();
}
}
/// <summary>
/// Gets the cached serializer or creates and caches the serializer for the given type.
/// </summary>
/// <returns>The <see cref="DataContractSerializer"/> instance.</returns>
protected virtual DataContractSerializer GetCachedSerializer(Type type)
{
if (!_serializerCache.TryGetValue(type, out var serializer))
{
serializer = CreateSerializer(type);
if (serializer != null)
{
_serializerCache.TryAdd(type, serializer);
}
}
return (DataContractSerializer)serializer;
}
}
}