From 2544926b2fc9231fa508070291eb9dc5bf2d58df Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 17 Oct 2018 13:58:09 -0700 Subject: [PATCH] Provide a convenience API to configure ApiBehaviorOptions --- .../MvcCoreMvcBuilderExtensions.cs | 25 +++++++++++++++++ .../MvcCoreMvcCoreBuilderExtensions.cs | 25 +++++++++++++++++ .../MvcBuilderExtensionsTest.cs | 28 +++++++++++++++++++ .../MvcCoreBuilderExtensionsTest.cs | 28 +++++++++++++++++++ 4 files changed, 106 insertions(+) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs index 6e1f9f9a1b..4830e87058 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs @@ -151,5 +151,30 @@ namespace Microsoft.Extensions.DependencyInjection builder.Services.Configure(o => o.CompatibilityVersion = version); return builder; } + + /// + /// Configures . + /// + /// The . + /// The configure action. + /// The . + public static IMvcBuilder ConfigureApiBehaviorOptions( + this IMvcBuilder builder, + Action setupAction) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + builder.Services.Configure(setupAction); + + return builder; + } } } diff --git a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs index 76ebe6c261..57499c930a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs @@ -169,6 +169,31 @@ namespace Microsoft.Extensions.DependencyInjection return builder; } + /// + /// Configures . + /// + /// The . + /// The configure action. + /// The . + public static IMvcCoreBuilder ConfigureApiBehaviorOptions( + this IMvcCoreBuilder builder, + Action setupAction) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + builder.Services.Configure(setupAction); + + return builder; + } + /// /// Sets the for ASP.NET Core MVC for the application. /// diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcBuilderExtensionsTest.cs index 4d1dbaec0d..9ef4358c3f 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcBuilderExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcBuilderExtensionsTest.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.MvcServiceCollectionExtensionsTestControllers; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Moq; using Xunit; @@ -110,6 +111,33 @@ namespace Microsoft.AspNetCore.Mvc Assert.Single(collection, d => d.ServiceType.Equals(typeof(ControllerTwo))); } + [Fact] + public void ConfigureApiBehaviorOptions_InvokesSetupAction() + { + // Arrange + var serviceCollection = new ServiceCollection() + .AddOptions(); + + var builder = new MvcBuilder( + serviceCollection, + new ApplicationPartManager()); + + var part = new TestApplicationPart(); + + // Act + var result = builder.ConfigureApiBehaviorOptions(o => + { + o.SuppressMapClientErrors = true; + }); + + // Assert + var options = serviceCollection. + BuildServiceProvider() + .GetRequiredService>() + .Value; + Assert.True(options.SuppressMapClientErrors); + } + private class ControllerOne { } diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs index 4fc6d41572..2311cb3987 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs @@ -6,6 +6,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Moq; using Xunit; @@ -51,5 +52,32 @@ namespace Microsoft.AspNetCore.Mvc.DependencyInjection Assert.Same(result, builder); Assert.Equal(new ApplicationPart[] { part }, builder.PartManager.ApplicationParts.ToArray()); } + + [Fact] + public void ConfigureApiBehaviorOptions_InvokesSetupAction() + { + // Arrange + var serviceCollection = new ServiceCollection() + .AddOptions(); + + var builder = new MvcCoreBuilder( + serviceCollection, + new ApplicationPartManager()); + + var part = new TestApplicationPart(); + + // Act + var result = builder.ConfigureApiBehaviorOptions(o => + { + o.SuppressMapClientErrors = true; + }); + + // Assert + var options = serviceCollection. + BuildServiceProvider() + .GetRequiredService>() + .Value; + Assert.True(options.SuppressMapClientErrors); + } } }