diff --git a/src/Microsoft.AspNet.Mvc.Formatters.Xml/DependencyInjection/MvcXmlMvcBuilderExtensions.cs b/src/Microsoft.AspNet.Mvc.Formatters.Xml/DependencyInjection/MvcXmlMvcBuilderExtensions.cs new file mode 100644 index 0000000000..edb622b2e7 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Formatters.Xml/DependencyInjection/MvcXmlMvcBuilderExtensions.cs @@ -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 +{ + /// + /// Extension methods for adding XML formatters to MVC. + /// + public static class MvcXmlMvcBuilderExtensions + { + /// + /// Adds the XML DataContractSerializer formatters to MVC. + /// + /// The . + /// The . + public static IMvcBuilder AddXmlDataContractSerializerFormatters([NotNull] this IMvcBuilder builder) + { + AddXmlDataContractSerializerFormatterServices(builder.Services); + return builder; + } + + /// + /// Adds the XML Serializer formatters to MVC. + /// + /// The . + /// The . + 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, MvcXmlDataContractSerializerMvcOptionsSetup>()); + } + + // Internal for testing. + internal static void AddXmlSerializerFormatterServices(IServiceCollection services) + { + services.TryAddEnumerable( + ServiceDescriptor.Transient, MvcXmlSerializerMvcOptionsSetup>()); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcOptionsExtensions.cs b/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcOptionsExtensions.cs index 0254e0d912..c25265e3b1 100644 --- a/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcOptionsExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcOptionsExtensions.cs @@ -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 { /// - /// Adds and - /// to the input and output formatter - /// collections respectively. + /// Adds and + /// to the + /// input and output formatter collections respectively. /// /// The MvcOptions 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); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcXmlDataContractSerializerMvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcXmlDataContractSerializerMvcOptionsSetup.cs new file mode 100644 index 0000000000..f54e6c79c8 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcXmlDataContractSerializerMvcOptionsSetup.cs @@ -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 +{ + /// + /// A implementation which will add the + /// data contract serializer formatters to . + /// + public class MvcXmlDataContractSerializerMvcOptionsSetup : ConfigureOptions + { + /// + /// Creates a new instance of . + /// + public MvcXmlDataContractSerializerMvcOptionsSetup() + : base(ConfigureMvc) + { + Order = DefaultOrder.DefaultFrameworkSortOrder + 10; + } + + /// + /// Adds the data contract serializer formatters to . + /// + /// The . + 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"); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcXmlSerializerMvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcXmlSerializerMvcOptionsSetup.cs new file mode 100644 index 0000000000..954cb584d7 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Formatters.Xml/MvcXmlSerializerMvcOptionsSetup.cs @@ -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 +{ + /// + /// A implementation which will add the + /// XML serializer formatters to . + /// + public class MvcXmlSerializerMvcOptionsSetup : ConfigureOptions + { + /// + /// Creates a new . + /// + public MvcXmlSerializerMvcOptionsSetup() + : base(ConfigureMvc) + { + Order = DefaultOrder.DefaultFrameworkSortOrder + 10; + } + + /// + /// Adds the XML serializer formatters to . + /// + /// The . + public static void ConfigureMvc(MvcOptions options) + { + options.OutputFormatters.Add(new XmlSerializerOutputFormatter()); + options.InputFormatters.Add(new XmlSerializerInputFormatter()); + } + } +}