diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs index 78118f01de..5e1ebc4364 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs @@ -66,6 +66,28 @@ namespace Microsoft.Extensions.DependencyInjection return builder; } + /// + /// Registers as the default + /// in the . + /// + /// The . + /// The . + public static IMvcBuilder AddSessionStateTempDataProvider(this IMvcBuilder builder) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + // Ensure the TempData basics are registered. + MvcViewFeaturesMvcCoreBuilderExtensions.AddViewServices(builder.Services); + + var descriptor = ServiceDescriptor.Singleton(typeof(ITempDataProvider), typeof(SessionStateTempDataProvider)); + builder.Services.Replace(descriptor); + + return builder; + } + /// /// Registers as the default in the /// . diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensions.cs index 25452fc589..3c4b4e96e8 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensions.cs @@ -198,7 +198,7 @@ namespace Microsoft.Extensions.DependencyInjection // Temp Data // // This does caching so it should stay singleton - services.TryAddSingleton(); + services.TryAddSingleton(); // // Antiforgery diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs index 03648a534e..241cf022ca 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs @@ -7,22 +7,23 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; -using Microsoft.AspNetCore.Internal; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.Net.Http.Headers; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { - public class TempDataInCookiesTest : TempDataTestBase, IClassFixture> + public class TempDataInCookiesTest : TempDataTestBase, IClassFixture> { - public TempDataInCookiesTest(MvcTestFixture fixture) + public TempDataInCookiesTest(MvcTestFixture fixture) { Client = fixture.Client; } protected override HttpClient Client { get; } + [Theory] [InlineData(ChunkingCookieManager.DefaultChunkSize)] [InlineData(ChunkingCookieManager.DefaultChunkSize * 1.5)] @@ -39,8 +40,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests // Assert 1 Assert.Equal(HttpStatusCode.OK, response.StatusCode); - IEnumerable setCookieValues; - Assert.True(response.Headers.TryGetValues(HeaderNames.SetCookie, out setCookieValues)); + Assert.True(response.Headers.TryGetValues(HeaderNames.SetCookie, out IEnumerable setCookieValues)); setCookieValues = setCookieValues.Where(cookie => cookie.Contains(CookieTempDataProvider.CookieName)); Assert.NotEmpty(setCookieValues); // Verify that all the cookies from CookieTempDataProvider are within the maximum size @@ -99,8 +99,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests // Assert 1 Assert.Equal(HttpStatusCode.OK, response.StatusCode); - IEnumerable setCookieValues; - Assert.True(response.Headers.TryGetValues(HeaderNames.SetCookie, out setCookieValues)); + Assert.True(response.Headers.TryGetValues(HeaderNames.SetCookie, out IEnumerable setCookieValues)); var setCookieHeader = setCookieValues .Select(setCookieValue => SetCookieHeaderValue.Parse(setCookieValue)) .FirstOrDefault(setCookieHeaderValue => setCookieHeaderValue.Name == CookieTempDataProvider.CookieName); @@ -153,8 +152,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - IEnumerable setCookieValues; - Assert.True(response.Headers.TryGetValues(HeaderNames.SetCookie, out setCookieValues)); + Assert.True(response.Headers.TryGetValues(HeaderNames.SetCookie, out IEnumerable setCookieValues)); var setCookieHeader = setCookieValues .Select(setCookieValue => SetCookieHeaderValue.Parse(setCookieValue)) .FirstOrDefault(setCookieHeaderValue => setCookieHeaderValue.Name == CookieTempDataProvider.CookieName); diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs index da2db04159..39e3edc551 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs @@ -6,9 +6,9 @@ using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { - public class TempDataInSessionTest : TempDataTestBase, IClassFixture> + public class TempDataInSessionTest : TempDataTestBase, IClassFixture> { - public TempDataInSessionTest(MvcTestFixture fixture) + public TempDataInSessionTest(MvcTestFixture fixture) { Client = fixture.Client; } diff --git a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs index 968d9957d8..1e19f32783 100644 --- a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs @@ -289,7 +289,7 @@ namespace Microsoft.AspNetCore.Mvc // Assert var descriptor = Assert.Single(services, item => item.ServiceType == typeof(ITempDataProvider)); - Assert.Equal(typeof(SessionStateTempDataProvider), descriptor.ImplementationType); + Assert.Equal(typeof(CookieTempDataProvider), descriptor.ImplementationType); } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensionsTest.cs index 682ea48bc3..3ca3128324 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensionsTest.cs @@ -22,7 +22,7 @@ namespace Microsoft.Extensions.DependencyInjection // Assert var descriptor = Assert.Single(services, item => item.ServiceType == typeof(ITempDataProvider)); - Assert.Equal(typeof(SessionStateTempDataProvider), descriptor.ImplementationType); + Assert.Equal(typeof(CookieTempDataProvider), descriptor.ImplementationType); } [Fact] diff --git a/test/WebSites/BasicWebSite/Startup.cs b/test/WebSites/BasicWebSite/Startup.cs index 726628bb5e..83bd6dd7ff 100644 --- a/test/WebSites/BasicWebSite/Startup.cs +++ b/test/WebSites/BasicWebSite/Startup.cs @@ -24,9 +24,6 @@ namespace BasicWebSite services.AddSingleton(); services.AddSingleton(); services.AddScoped(); - services.AddMemoryCache(); - services.AddDistributedMemoryCache(); - services.AddSession(); } public void Configure(IApplicationBuilder app) @@ -38,8 +35,6 @@ namespace BasicWebSite // Initializes the RequestId service for each request app.UseMiddleware(); - app.UseSession(); - // Add MVC to the request pipeline app.UseMvc(routes => { @@ -55,4 +50,3 @@ namespace BasicWebSite } } } - diff --git a/test/WebSites/BasicWebSite/StartupWithCookieTempDataProvider.cs b/test/WebSites/BasicWebSite/StartupWithSessionTempDataProvider.cs similarity index 67% rename from test/WebSites/BasicWebSite/StartupWithCookieTempDataProvider.cs rename to test/WebSites/BasicWebSite/StartupWithSessionTempDataProvider.cs index eec17b5544..289a99a2b7 100644 --- a/test/WebSites/BasicWebSite/StartupWithCookieTempDataProvider.cs +++ b/test/WebSites/BasicWebSite/StartupWithSessionTempDataProvider.cs @@ -6,18 +6,21 @@ using Microsoft.Extensions.DependencyInjection; namespace BasicWebSite { - public class StartupWithCookieTempDataProvider + public class StartupWithSessionTempDataProvider { public void ConfigureServices(IServiceCollection services) { + // CookieTempDataProvider is the default ITempDataProvider, so we must override it with session. services .AddMvc() - .AddCookieTempDataProvider(); + .AddSessionStateTempDataProvider(); + services.AddSession(); } public void Configure(IApplicationBuilder app) { app.UseCultureReplacer(); + app.UseSession(); app.UseMvcWithDefaultRoute(); } }