Fix #2874 part 1 - XML formatter support for IMvcBuilder

This commit is contained in:
Ryan Nowak 2015-07-30 11:14:28 -07:00
parent fcad4c5c57
commit 4b3bac9dbb
4 changed files with 129 additions and 12 deletions

View File

@ -0,0 +1,51 @@
// 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 Microsoft.AspNet.Mvc;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods for adding XML formatters to MVC.
/// </summary>
public static class MvcXmlMvcBuilderExtensions
{
/// <summary>
/// Adds the XML DataContractSerializer formatters to MVC.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddXmlDataContractSerializerFormatters([NotNull] this IMvcBuilder builder)
{
AddXmlDataContractSerializerFormatterServices(builder.Services);
return builder;
}
/// <summary>
/// Adds the XML Serializer formatters to MVC.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddXmlSerializerFormatters([NotNull] this IMvcBuilder builder)
{
AddXmlSerializerFormatterServices(builder.Services);
return builder;
}
// Internal for testing.
internal static void AddXmlDataContractSerializerFormatterServices(IServiceCollection services)
{
services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcXmlDataContractSerializerMvcOptionsSetup>());
}
// Internal for testing.
internal static void AddXmlSerializerFormatterServices(IServiceCollection services)
{
services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcXmlSerializerMvcOptionsSetup>());
}
}
}

View File

@ -1,8 +1,6 @@
// 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 Microsoft.AspNet.Mvc.Formatters.Xml;
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc
@ -10,20 +8,14 @@ namespace Microsoft.AspNet.Mvc
public static class MvcOptionsExtensions
{
/// <summary>
/// Adds <see cref="XmlDataContractSerializerInputFormatter"/> and
/// <see cref="XmlDataContractSerializerOutputFormatter"/> to the input and output formatter
/// collections respectively.
/// Adds <see cref="Microsoft.AspNet.Mvc.Formatters.Xml.XmlDataContractSerializerInputFormatter"/> and
/// <see cref="Microsoft.AspNet.Mvc.Formatters.Xml.XmlDataContractSerializerOutputFormatter"/> to the
/// input and output formatter collections respectively.
/// </summary>
/// <param name="options">The MvcOptions</param>
public static void AddXmlDataContractSerializerFormatter([NotNull] this MvcOptions options)
{
options.ModelMetadataDetailsProviders.Add(new DataMemberRequiredBindingMetadataProvider());
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
options.ValidationExcludeFilters.Add(typeFullName: "System.Xml.Linq.XObject");
options.ValidationExcludeFilters.Add(typeFullName: "System.Xml.XmlNode");
MvcXmlDataContractSerializerMvcOptionsSetup.ConfigureMvc(options);
}
}
}

View File

@ -0,0 +1,40 @@
// 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 Microsoft.AspNet.Mvc.Formatters.Xml;
using Microsoft.AspNet.Mvc.ModelBinding.Metadata;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// A <see cref="ConfigureOptions{TOptions}"/> implementation which will add the
/// data contract serializer formatters to <see cref="MvcOptions"/>.
/// </summary>
public class MvcXmlDataContractSerializerMvcOptionsSetup : ConfigureOptions<MvcOptions>
{
/// <summary>
/// Creates a new instance of <see cref="MvcXmlDataContractSerializerMvcOptionsSetup"/>.
/// </summary>
public MvcXmlDataContractSerializerMvcOptionsSetup()
: base(ConfigureMvc)
{
Order = DefaultOrder.DefaultFrameworkSortOrder + 10;
}
/// <summary>
/// Adds the data contract serializer formatters to <see cref="MvcOptions"/>.
/// </summary>
/// <param name="options">The <see cref="MvcOptions"/>.</param>
public static void ConfigureMvc(MvcOptions options)
{
options.ModelMetadataDetailsProviders.Add(new DataMemberRequiredBindingMetadataProvider());
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
options.ValidationExcludeFilters.Add(typeFullName: "System.Xml.Linq.XObject");
options.ValidationExcludeFilters.Add(typeFullName: "System.Xml.XmlNode");
}
}
}

View File

@ -0,0 +1,34 @@
// 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 Microsoft.AspNet.Mvc.Formatters.Xml;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// A <see cref="ConfigureOptions{TOptions}"/> implementation which will add the
/// XML serializer formatters to <see cref="MvcOptions"/>.
/// </summary>
public class MvcXmlSerializerMvcOptionsSetup : ConfigureOptions<MvcOptions>
{
/// <summary>
/// Creates a new <see cref="MvcXmlSerializerMvcOptionsSetup"/>.
/// </summary>
public MvcXmlSerializerMvcOptionsSetup()
: base(ConfigureMvc)
{
Order = DefaultOrder.DefaultFrameworkSortOrder + 10;
}
/// <summary>
/// Adds the XML serializer formatters to <see cref="MvcOptions"/>.
/// </summary>
/// <param name="options">The <see cref="MvcOptions"/>.</param>
public static void ConfigureMvc(MvcOptions options)
{
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
options.InputFormatters.Add(new XmlSerializerInputFormatter());
}
}
}