aspnetcore/src/Microsoft.AspNet.Mvc.Format.../DependencyInjection/MvcXmlMvcBuilderExtensions.cs

64 lines
2.3 KiB
C#

// 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 Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Formatters.Xml.Internal;
using Microsoft.Framework.DependencyInjection.Extensions;
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(this IMvcBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(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(this IMvcBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(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>());
}
}
}