From 43a0a5a9f12f8569c136a55b9d5fa7de78fb1430 Mon Sep 17 00:00:00 2001 From: Ryan Brandenburg Date: Fri, 19 Aug 2016 11:27:22 -0700 Subject: [PATCH] Replace ConfigureOptions with IConfigureOptions --- .../Internal/MvcCoreRouteOptionsSetup.cs | 15 ++-- ...DataAnnotationsLocalizationOptionsSetup.cs | 10 +-- .../MvcDataAnnotationsMvcOptionsSetup.cs | 51 +++++++++---- .../Internal/MvcJsonMvcOptionsSetup.cs | 71 +++++++++++-------- ...mlDataContractSerializerMvcOptionsSetup.cs | 14 +--- .../MvcXmlSerializerMvcOptionsSetup.cs | 14 +--- .../MvcRazorMvcCoreBuilderExtensions.cs | 2 + .../Internal/MvcRazorMvcViewOptionsSetup.cs | 28 ++++---- .../RazorViewEngineOptionsSetup.cs | 2 + .../Internal/MvcViewOptionsSetup.cs | 54 +++++++++----- .../Internal/TempDataMvcOptionsSetup.cs | 9 +-- .../MvcDataAnnotationsMvcOptionsSetup.cs | 37 ++++++++++ .../TestMvcOptions.cs | 37 ++++++---- ...yContextRazorViewEngineOptionsSetupTest.cs | 2 + .../RazorViewEngineOptionsSetupTest.cs | 8 ++- ...wEngineTest.cs => RazoreViewEngineTest.cs} | 4 +- .../MvcServiceCollectionExtensionsTest.cs | 4 +- 17 files changed, 227 insertions(+), 135 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.DataAnnotations.Test/Internal/MvcDataAnnotationsMvcOptionsSetup.cs rename test/Microsoft.AspNetCore.Mvc.Razor.Test/{RazorViewEngineTest.cs => RazoreViewEngineTest.cs} (99%) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcCoreRouteOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcCoreRouteOptionsSetup.cs index e31ac88abc..6ed400f506 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcCoreRouteOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcCoreRouteOptionsSetup.cs @@ -1,6 +1,7 @@ // 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.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Options; @@ -10,19 +11,19 @@ namespace Microsoft.AspNetCore.Mvc.Internal /// /// Sets up MVC default options for . /// - public class MvcCoreRouteOptionsSetup : ConfigureOptions + public class MvcCoreRouteOptionsSetup : IConfigureOptions { - public MvcCoreRouteOptionsSetup() - : base(ConfigureRouting) - { - } - /// /// Configures the . /// /// The . - public static void ConfigureRouting(RouteOptions options) + public void Configure(RouteOptions options) { + if (options == null) + { + throw new ArgumentNullException(nameof(options)); + } + options.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint)); } } diff --git a/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsLocalizationOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsLocalizationOptionsSetup.cs index 8cd70d5aeb..e91f13b255 100644 --- a/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsLocalizationOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsLocalizationOptionsSetup.cs @@ -9,14 +9,10 @@ namespace Microsoft.AspNetCore.Mvc.DataAnnotations.Internal /// /// Sets up default options for . /// - public class MvcDataAnnotationsLocalizationOptionsSetup : ConfigureOptions + public class MvcDataAnnotationsLocalizationOptionsSetup : IConfigureOptions { - public MvcDataAnnotationsLocalizationOptionsSetup() - : base(ConfigureMvc) - { - } - - public static void ConfigureMvc(MvcDataAnnotationsLocalizationOptions options) + /// + public void Configure(MvcDataAnnotationsLocalizationOptions options) { if (options == null) { diff --git a/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsMvcOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsMvcOptionsSetup.cs index 9c846ffd2a..6e28344dc2 100644 --- a/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsMvcOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/MvcDataAnnotationsMvcOptionsSetup.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; @@ -11,28 +10,52 @@ namespace Microsoft.AspNetCore.Mvc.DataAnnotations.Internal /// /// Sets up default options for . /// - public class MvcDataAnnotationsMvcOptionsSetup : ConfigureOptions + public class MvcDataAnnotationsMvcOptionsSetup : IConfigureOptions { - public MvcDataAnnotationsMvcOptionsSetup(IServiceProvider serviceProvider) - : base(options => ConfigureMvc(options, serviceProvider)) + private readonly IStringLocalizerFactory _stringLocalizerFactory; + private readonly IValidationAttributeAdapterProvider _validationAttributeAdapterProvider; + private readonly IOptions _dataAnnotationLocalizationOptions; + + public MvcDataAnnotationsMvcOptionsSetup( + IValidationAttributeAdapterProvider validationAttributeAdapterProvider, + IOptions dataAnnotationLocalizationOptions) { + if (validationAttributeAdapterProvider == null) + { + throw new ArgumentNullException(nameof(validationAttributeAdapterProvider)); + } + + if (dataAnnotationLocalizationOptions == null) + { + throw new ArgumentNullException(nameof(dataAnnotationLocalizationOptions)); + } + + _validationAttributeAdapterProvider = validationAttributeAdapterProvider; + _dataAnnotationLocalizationOptions = dataAnnotationLocalizationOptions; } - public static void ConfigureMvc(MvcOptions options, IServiceProvider serviceProvider) + public MvcDataAnnotationsMvcOptionsSetup( + IValidationAttributeAdapterProvider validationAttributeAdapterProvider, + IOptions dataAnnotationLocalizationOptions, + IStringLocalizerFactory stringLocalizerFactory) + : this(validationAttributeAdapterProvider, dataAnnotationLocalizationOptions) { - var dataAnnotationLocalizationOptions = - serviceProvider.GetRequiredService>(); + _stringLocalizerFactory = stringLocalizerFactory; + } - // This service will be registered only if AddDataAnnotationsLocalization() is added to service collection. - var stringLocalizerFactory = serviceProvider.GetService(); - var validationAttributeAdapterProvider = serviceProvider.GetRequiredService(); + public void Configure(MvcOptions options) + { + if (options == null) + { + throw new ArgumentNullException(nameof(options)); + } - options.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider(stringLocalizerFactory)); + options.ModelMetadataDetailsProviders.Add(new DataAnnotationsMetadataProvider(_stringLocalizerFactory)); options.ModelValidatorProviders.Add(new DataAnnotationsModelValidatorProvider( - validationAttributeAdapterProvider, - dataAnnotationLocalizationOptions, - stringLocalizerFactory)); + _validationAttributeAdapterProvider, + _dataAnnotationLocalizationOptions, + _stringLocalizerFactory)); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/Internal/MvcJsonMvcOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/Internal/MvcJsonMvcOptionsSetup.cs index d9a9cc013b..f924fd5c59 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/Internal/MvcJsonMvcOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/Internal/MvcJsonMvcOptionsSetup.cs @@ -1,6 +1,7 @@ // 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 System.Buffers; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.Extensions.Logging; @@ -15,52 +16,62 @@ namespace Microsoft.AspNetCore.Mvc.Formatters.Json.Internal /// /// Sets up JSON formatter options for . /// - public class MvcJsonMvcOptionsSetup : ConfigureOptions + public class MvcJsonMvcOptionsSetup : IConfigureOptions { - /// - /// Initializes a new instance of . - /// - /// The . - /// - /// - /// + private readonly ILoggerFactory _loggerFactory; + private readonly JsonSerializerSettings _jsonSerializerSettings; + private readonly ArrayPool _charPool; + private readonly ObjectPoolProvider _objectPoolProvider; + public MvcJsonMvcOptionsSetup( ILoggerFactory loggerFactory, IOptions jsonOptions, ArrayPool charPool, ObjectPoolProvider objectPoolProvider) - : base((options) => ConfigureMvc( - options, - jsonOptions.Value.SerializerSettings, - loggerFactory, - charPool, - objectPoolProvider)) { + if (loggerFactory == null) + { + throw new ArgumentNullException(nameof(loggerFactory)); + } + + if (jsonOptions == null) + { + throw new ArgumentNullException(nameof(jsonOptions)); + } + + if (charPool == null) + { + throw new ArgumentNullException(nameof(charPool)); + } + + if (objectPoolProvider == null) + { + throw new ArgumentNullException(nameof(objectPoolProvider)); + } + + _loggerFactory = loggerFactory; + _jsonSerializerSettings = jsonOptions.Value.SerializerSettings; + _charPool = charPool; + _objectPoolProvider = objectPoolProvider; } - public static void ConfigureMvc( - MvcOptions options, - JsonSerializerSettings serializerSettings, - ILoggerFactory loggerFactory, - ArrayPool charPool, - ObjectPoolProvider objectPoolProvider) + public void Configure(MvcOptions options) { + options.OutputFormatters.Add(new JsonOutputFormatter(_jsonSerializerSettings, _charPool)); - options.OutputFormatters.Add(new JsonOutputFormatter(serializerSettings, charPool)); - - var jsonInputLogger = loggerFactory.CreateLogger(); + var jsonInputLogger = _loggerFactory.CreateLogger(); options.InputFormatters.Add(new JsonInputFormatter( jsonInputLogger, - serializerSettings, - charPool, - objectPoolProvider)); + _jsonSerializerSettings, + _charPool, + _objectPoolProvider)); - var jsonInputPatchLogger = loggerFactory.CreateLogger(); + var jsonInputPatchLogger = _loggerFactory.CreateLogger(); options.InputFormatters.Add(new JsonPatchInputFormatter( jsonInputPatchLogger, - serializerSettings, - charPool, - objectPoolProvider)); + _jsonSerializerSettings, + _charPool, + _objectPoolProvider)); options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json")); diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlDataContractSerializerMvcOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlDataContractSerializerMvcOptionsSetup.cs index 031b796016..c09aa07685 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlDataContractSerializerMvcOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlDataContractSerializerMvcOptionsSetup.cs @@ -8,24 +8,16 @@ using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Mvc.Formatters.Xml.Internal { /// - /// A implementation which will add the + /// A implementation which will add the /// data contract serializer formatters to . /// - public class MvcXmlDataContractSerializerMvcOptionsSetup : ConfigureOptions + public class MvcXmlDataContractSerializerMvcOptionsSetup : IConfigureOptions { - /// - /// Creates a new instance of . - /// - public MvcXmlDataContractSerializerMvcOptionsSetup() - : base(ConfigureMvc) - { - } - /// /// Adds the data contract serializer formatters to . /// /// The . - public static void ConfigureMvc(MvcOptions options) + public void Configure(MvcOptions options) { options.ModelMetadataDetailsProviders.Add(new DataMemberRequiredBindingMetadataProvider()); diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlSerializerMvcOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlSerializerMvcOptionsSetup.cs index 89f7ff0cc9..0d5b184d80 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlSerializerMvcOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/Internal/MvcXmlSerializerMvcOptionsSetup.cs @@ -6,24 +6,16 @@ using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Mvc.Formatters.Xml.Internal { /// - /// A implementation which will add the + /// A implementation which will add the /// XML serializer formatters to . /// - public class MvcXmlSerializerMvcOptionsSetup : ConfigureOptions + public class MvcXmlSerializerMvcOptionsSetup : IConfigureOptions { - /// - /// Creates a new . - /// - public MvcXmlSerializerMvcOptionsSetup() - : base(ConfigureMvc) - { - } - /// /// Adds the XML serializer formatters to . /// /// The . - public static void ConfigureMvc(MvcOptions options) + public void Configure(MvcOptions options) { options.OutputFormatters.Add(new XmlSerializerOutputFormatter()); options.InputFormatters.Add(new XmlSerializerInputFormatter()); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs index 4c71427961..8e38d7f1b3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs @@ -142,8 +142,10 @@ namespace Microsoft.Extensions.DependencyInjection // DependencyContextRazorViewEngineOptionsSetup needs to run after RazorViewEngineOptionsSetup. // The ordering of the following two lines is important to ensure this behavior. +#pragma warning disable 0618 services.TryAddEnumerable( ServiceDescriptor.Transient, RazorViewEngineOptionsSetup>()); +#pragma warning restore 0618 services.TryAddEnumerable( ServiceDescriptor.Transient< IConfigureOptions, diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/MvcRazorMvcViewOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/MvcRazorMvcViewOptionsSetup.cs index 679bc00d2b..8b35da8ddf 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/MvcRazorMvcViewOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/MvcRazorMvcViewOptionsSetup.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Mvc.Razor.Internal @@ -10,37 +9,36 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal /// /// Configures to use . /// - public class MvcRazorMvcViewOptionsSetup : ConfigureOptions + public class MvcRazorMvcViewOptionsSetup : IConfigureOptions { + private readonly IRazorViewEngine _razorViewEngine; + /// /// Initializes a new instance of . /// /// The . public MvcRazorMvcViewOptionsSetup(IRazorViewEngine razorViewEngine) - : base(options => ConfigureMvc(razorViewEngine, options)) - { - } - - /// - /// Configures to use . - /// - /// The . - /// The to configure. - public static void ConfigureMvc( - IRazorViewEngine razorViewEngine, - MvcViewOptions options) { if (razorViewEngine == null) { throw new ArgumentNullException(nameof(razorViewEngine)); } + _razorViewEngine = razorViewEngine; + } + + /// + /// Configures to use . + /// + /// The to configure. + public void Configure(MvcViewOptions options) + { if (options == null) { throw new ArgumentNullException(nameof(options)); } - options.ViewEngines.Add(razorViewEngine); + options.ViewEngines.Add(_razorViewEngine); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngineOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngineOptionsSetup.cs index dbba63a6c3..271e8ed5e3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngineOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngineOptionsSetup.cs @@ -1,6 +1,7 @@ // 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 System.Linq; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Razor; @@ -12,6 +13,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// Sets up default options for . /// + [Obsolete("This type is for internal use and will be removed in a future version.")] public class RazorViewEngineOptionsSetup : ConfigureOptions { /// diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/MvcViewOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/MvcViewOptionsSetup.cs index 157c0039c2..5e59f56345 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/MvcViewOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/MvcViewOptionsSetup.cs @@ -4,7 +4,6 @@ using System; using Microsoft.AspNetCore.Mvc.DataAnnotations; using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; @@ -13,31 +12,52 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal /// /// Sets up default options for . /// - public class MvcViewOptionsSetup : ConfigureOptions + public class MvcViewOptionsSetup : IConfigureOptions { - /// - /// Initializes a new instance of . - /// - public MvcViewOptionsSetup(IServiceProvider serviceProvider) - : base(options => ConfigureMvc(options, serviceProvider)) + private readonly IOptions _dataAnnotationsLocalizationOptions; + private readonly IValidationAttributeAdapterProvider _validationAttributeAdapterProvider; + private readonly IStringLocalizerFactory _stringLocalizerFactory; + + public MvcViewOptionsSetup( + IOptions dataAnnotationLocalizationOptions, + IValidationAttributeAdapterProvider validationAttributeAdapterProvider) { + if (dataAnnotationLocalizationOptions == null) + { + throw new ArgumentNullException(nameof(dataAnnotationLocalizationOptions)); + } + + if (validationAttributeAdapterProvider == null) + { + throw new ArgumentNullException(nameof(validationAttributeAdapterProvider)); + } + + _dataAnnotationsLocalizationOptions = dataAnnotationLocalizationOptions; + _validationAttributeAdapterProvider = validationAttributeAdapterProvider; } - public static void ConfigureMvc( - MvcViewOptions options, - IServiceProvider serviceProvider) + public MvcViewOptionsSetup( + IOptions dataAnnotationOptions, + IValidationAttributeAdapterProvider validationAttributeAdapterProvider, + IStringLocalizerFactory stringLocalizerFactory) + : this(dataAnnotationOptions, validationAttributeAdapterProvider) { - var dataAnnotationsLocalizationOptions = - serviceProvider.GetRequiredService>(); - var stringLocalizerFactory = serviceProvider.GetService(); - var validationAttributeAdapterProvider = serviceProvider.GetRequiredService(); + if (stringLocalizerFactory == null) + { + throw new ArgumentNullException(nameof(stringLocalizerFactory)); + } + _stringLocalizerFactory = stringLocalizerFactory; + } + + public void Configure(MvcViewOptions options) + { // Set up client validators options.ClientModelValidatorProviders.Add(new DefaultClientModelValidatorProvider()); options.ClientModelValidatorProviders.Add(new DataAnnotationsClientModelValidatorProvider( - validationAttributeAdapterProvider, - dataAnnotationsLocalizationOptions, - stringLocalizerFactory)); + _validationAttributeAdapterProvider, + _dataAnnotationsLocalizationOptions, + _stringLocalizerFactory)); options.ClientModelValidatorProviders.Add(new NumericClientModelValidatorProvider()); } } diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/TempDataMvcOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/TempDataMvcOptionsSetup.cs index 6ef797c37c..9d471020b3 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/TempDataMvcOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/TempDataMvcOptionsSetup.cs @@ -8,14 +8,9 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal /// /// Sets up default options for . /// - public class TempDataMvcOptionsSetup : ConfigureOptions + public class TempDataMvcOptionsSetup : IConfigureOptions { - public TempDataMvcOptionsSetup() - : base(ConfigureMvc) - { - } - - public static void ConfigureMvc(MvcOptions options) + public void Configure(MvcOptions options) { options.Filters.Add(new SaveTempDataAttribute()); } diff --git a/test/Microsoft.AspNetCore.Mvc.DataAnnotations.Test/Internal/MvcDataAnnotationsMvcOptionsSetup.cs b/test/Microsoft.AspNetCore.Mvc.DataAnnotations.Test/Internal/MvcDataAnnotationsMvcOptionsSetup.cs new file mode 100644 index 0000000000..4a4f081687 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.DataAnnotations.Test/Internal/MvcDataAnnotationsMvcOptionsSetup.cs @@ -0,0 +1,37 @@ +// 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.Reflection; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Moq; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.DataAnnotations.Test.Internal +{ + public class MvcDataAnnotationsMvcOptionsSetupTests + { + [Fact] + public void MvcDataAnnotationsMvcOptionsSetup_ServiceConstructorWithoutIStringLocalizer() + { + // Arrange + var services = new ServiceCollection(); + + services.AddSingleton(Mock.Of()); + services.AddSingleton(); + services.AddSingleton>( + new TestOptionsManager()); + services.AddSingleton, MvcDataAnnotationsMvcOptionsSetup>(); + + var serviceProvider = services.BuildServiceProvider(); + + // Act + var optionsSetup = serviceProvider.GetRequiredService>(); + + // Assert + Assert.NotNull(optionsSetup); + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.IntegrationTests/TestMvcOptions.cs b/test/Microsoft.AspNetCore.Mvc.IntegrationTests/TestMvcOptions.cs index 87c4b2b351..17426c07d0 100644 --- a/test/Microsoft.AspNetCore.Mvc.IntegrationTests/TestMvcOptions.cs +++ b/test/Microsoft.AspNetCore.Mvc.IntegrationTests/TestMvcOptions.cs @@ -1,6 +1,7 @@ // 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 System.Buffers; using Microsoft.AspNetCore.Mvc.DataAnnotations; using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal; @@ -10,9 +11,11 @@ using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.ObjectPool; using Microsoft.Extensions.Options; +using Moq; namespace Microsoft.AspNetCore.Mvc.IntegrationTests { @@ -24,23 +27,31 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests var optionsSetup = new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory()); optionsSetup.Configure(Value); - var collection = new ServiceCollection().AddOptions(); - collection.AddSingleton(); - collection.AddSingleton(); - collection.AddSingleton(); - MvcDataAnnotationsMvcOptionsSetup.ConfigureMvc( - Value, - collection.BuildServiceProvider()); + var validationAttributeAdapterProvider = new ValidationAttributeAdapterProvider(); + var dataAnnotationLocalizationOptions = new TestOptionsManager(); + var stringLocalizer = new Mock(); + var stringLocalizerFactory = new Mock(); + stringLocalizerFactory + .Setup(s => s.Create(It.IsAny())) + .Returns(stringLocalizer.Object); + + var dataAnnotationOptionsSetup = new MvcDataAnnotationsMvcOptionsSetup( + validationAttributeAdapterProvider, + dataAnnotationLocalizationOptions, + stringLocalizerFactory.Object); + dataAnnotationOptionsSetup.Configure(Value); var loggerFactory = new LoggerFactory(); - var serializerSettings = JsonSerializerSettingsProvider.CreateSerializerSettings(); + var jsonOptions = new TestOptionsManager(); + var charPool = ArrayPool.Shared; + var objectPoolProvider = new DefaultObjectPoolProvider(); - MvcJsonMvcOptionsSetup.ConfigureMvc( - Value, - serializerSettings, + var mvcJsonMvcOptionsSetup = new MvcJsonMvcOptionsSetup( loggerFactory, - ArrayPool.Shared, - new DefaultObjectPoolProvider()); + jsonOptions, + charPool, + objectPoolProvider); + mvcJsonMvcOptionsSetup.Configure(Value); } public MvcOptions Value { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DependencyContextRazorViewEngineOptionsSetupTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DependencyContextRazorViewEngineOptionsSetupTest.cs index e7e9676fc9..d2a4d3ca1f 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DependencyContextRazorViewEngineOptionsSetupTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DependencyContextRazorViewEngineOptionsSetupTest.cs @@ -265,7 +265,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal var hostingEnvironment = new Mock(); hostingEnvironment.SetupGet(e => e.EnvironmentName) .Returns("Development"); +#pragma warning disable 0618 var viewEngineSetup = new RazorViewEngineOptionsSetup(hostingEnvironment.Object); +#pragma warning restore 0618 // Act viewEngineSetup.Configure(options); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineOptionsSetupTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineOptionsSetupTest.cs index 5969526041..12dbd0ed45 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineOptionsSetupTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineOptionsSetupTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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.AspNetCore.Hosting; @@ -23,7 +23,9 @@ namespace Microsoft.AspNetCore.Mvc .Returns(expected); hostingEnv.SetupGet(e => e.EnvironmentName) .Returns("Development"); +#pragma warning disable 0618 var optionsSetup = new RazorViewEngineOptionsSetup(hostingEnv.Object); +#pragma warning restore 0618 // Act optionsSetup.Configure(options); @@ -44,7 +46,9 @@ namespace Microsoft.AspNetCore.Mvc var hostingEnv = new Mock(); hostingEnv.SetupGet(e => e.EnvironmentName) .Returns(environment); +#pragma warning disable 0618 var optionsSetup = new RazorViewEngineOptionsSetup(hostingEnv.Object); +#pragma warning restore 0618 // Act optionsSetup.Configure(options); @@ -66,7 +70,9 @@ namespace Microsoft.AspNetCore.Mvc var hostingEnv = new Mock(); hostingEnv.SetupGet(e => e.EnvironmentName) .Returns(environment); +#pragma warning disable 0618 var optionsSetup = new RazorViewEngineOptionsSetup(hostingEnv.Object); +#pragma warning restore 0618 // Act optionsSetup.Configure(options); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazoreViewEngineTest.cs similarity index 99% rename from test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineTest.cs rename to test/Microsoft.AspNetCore.Mvc.Razor.Test/RazoreViewEngineTest.cs index b2da3a02f3..5407c4dcf4 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazorViewEngineTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/RazoreViewEngineTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -1564,7 +1564,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Test IEnumerable viewLocationFormats = null, IEnumerable areaViewLocationFormats = null) { +#pragma warning disable 0618 var optionsSetup = new RazorViewEngineOptionsSetup(Mock.Of()); +#pragma warning restore 0618 var options = new RazorViewEngineOptions(); optionsSetup.Configure(options); diff --git a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs index ea1adf99f7..19173f0e02 100644 --- a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -327,7 +327,9 @@ namespace Microsoft.AspNetCore.Mvc typeof(IConfigureOptions), new[] { +#pragma warning disable 0618 typeof(RazorViewEngineOptionsSetup), +#pragma warning restore 0618 typeof(DependencyContextRazorViewEngineOptionsSetup) } },