|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Linq; |
5 | 6 | using System.Web.Http.Controllers; |
6 | 7 | using System.Web.Http.OData.Formatter; |
7 | 8 | using System.Web.Http.OData.Formatter.Deserialization; |
8 | 9 | using NuGet.Server.V2.OData.Serializers; |
| 10 | +using System.Collections.Generic; |
9 | 11 |
|
10 | 12 | namespace NuGet.Server.V2.OData |
11 | 13 | { |
12 | 14 | class NuGetODataControllerConfigurationAttribute : Attribute, IControllerConfiguration |
13 | 15 | { |
14 | | - public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) |
| 16 | + private static IList<ODataMediaTypeFormatter> _formatters; |
| 17 | + private static object _syncLock = new object(); |
| 18 | + |
| 19 | + private IList<ODataMediaTypeFormatter> GetFormatters() |
15 | 20 | { |
16 | | - var serProvider = new CustomSerializerProvider(provider => new NuGetEntityTypeSerializer(provider)); |
17 | | - var formatters = ODataMediaTypeFormatters.Create(serProvider, new DefaultODataDeserializerProvider()); |
| 21 | + if (_formatters == null) |
| 22 | + { |
| 23 | + lock (_syncLock) |
| 24 | + { |
| 25 | + if (_formatters == null) |
| 26 | + { |
| 27 | + var serProvider = new CustomSerializerProvider(provider => new NuGetEntityTypeSerializer(provider)); |
| 28 | + var createdFormatters = ODataMediaTypeFormatters.Create(serProvider, new DefaultODataDeserializerProvider()); |
| 29 | + |
| 30 | + var jsonFormatters = createdFormatters.Where(x => x.SupportedMediaTypes.Any(y => y.MediaType.Contains("json"))); |
| 31 | + createdFormatters.RemoveAll(x => jsonFormatters.Contains(x)); |
| 32 | + var xmlFormatterIndex = createdFormatters.IndexOf(createdFormatters.Last(x => x.SupportedMediaTypes.Any(y => y.MediaType.Contains("xml")))); |
| 33 | + foreach(var formatter in jsonFormatters) |
| 34 | + { |
| 35 | + createdFormatters.Insert(xmlFormatterIndex++, formatter); |
| 36 | + } |
| 37 | + _formatters = createdFormatters; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return _formatters; |
| 42 | + } |
18 | 43 |
|
| 44 | + |
| 45 | + public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) |
| 46 | + { |
19 | 47 | controllerSettings.Formatters.Clear(); |
20 | | - controllerSettings.Formatters.InsertRange(0, formatters); |
| 48 | + controllerSettings.Formatters.InsertRange(0, GetFormatters()); |
21 | 49 | } |
22 | 50 | } |
23 | 51 | } |
0 commit comments