From 9a0c7dbbfbc141f21f9bad50504f7f3978742ead Mon Sep 17 00:00:00 2001 From: David W <1511024+marabooy@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:46:36 +0300 Subject: [PATCH] update di settings (#1344) --- .../ODataServiceCollectionExtensions.cs | 4 +- .../Microsoft.AspNetCore.OData.csproj | 6 +- .../Microsoft.AspNetCore.OData.xml | 6 +- .../Abstracts/DefaultContainerBuilderTests.cs | 63 +++++++++++++++++++ 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNetCore.OData/Abstracts/ODataServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.OData/Abstracts/ODataServiceCollectionExtensions.cs index 35b6b495a..1d715d643 100644 --- a/src/Microsoft.AspNetCore.OData/Abstracts/ODataServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.OData/Abstracts/ODataServiceCollectionExtensions.cs @@ -38,7 +38,7 @@ public static IServiceCollection AddDefaultWebApiServices(this IServiceCollectio // ReaderSettings and WriterSettings are registered as prototype services. // There will be a copy (if it is accessed) of each prototype for each request. #pragma warning disable CS0618 // ReadUntypedAsString is obsolete in ODL 8. - services.AddSingleton(new ODataMessageReaderSettings + services.AddScoped(sp => new ODataMessageReaderSettings { EnableMessageStreamDisposal = false, MessageQuotas = new ODataMessageQuotas { MaxReceivedMessageSize = Int64.MaxValue }, @@ -53,7 +53,7 @@ public static IServiceCollection AddDefaultWebApiServices(this IServiceCollectio }); #pragma warning restore CS0618 // Type or member is obsolete - services.AddSingleton(new ODataMessageWriterSettings + services.AddScoped(sp => new ODataMessageWriterSettings { EnableMessageStreamDisposal = false, MessageQuotas = new ODataMessageQuotas { MaxReceivedMessageSize = Int64.MaxValue }, diff --git a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.csproj b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.csproj index 5f9076d34..0b955c1c7 100644 --- a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.csproj +++ b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.csproj @@ -28,9 +28,9 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml index 820bd16b4..2ec713bde 100644 --- a/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml +++ b/src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml @@ -9471,9 +9471,9 @@ - Creates an expression for retrieving a dynamic property from the dynamic properties container property. + Creates an expression for retrieving a dynamic property from the container property. - The dynamic properties container property access expression. + The container property access expression. The dynamic property name. The query binder context. The LINQ created. @@ -9522,7 +9522,7 @@ - Creates an expression for retrieving a dynamic property from the dynamic properties container property. + Creates an expression for retrieving a dynamic property from the container property. The source expression. The property name. diff --git a/test/Microsoft.AspNetCore.OData.Tests/Abstracts/DefaultContainerBuilderTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Abstracts/DefaultContainerBuilderTests.cs index 8a7d16561..966708995 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Abstracts/DefaultContainerBuilderTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Abstracts/DefaultContainerBuilderTests.cs @@ -136,6 +136,69 @@ public void AddScopedService_Works() Assert.NotEqual(o11, o21); } + [Fact] + public void MessageReaderIsScoped() + { + // Arrange + IServiceCollection services = new ServiceCollection(); + services.AddDefaultWebApiServices(); + IServiceProvider container = services.BuildServiceProvider(); + + // Act + IServiceProvider scopedContainer1 = container.GetRequiredService() + .CreateScope().ServiceProvider; + ODataMessageReaderSettings reader11 = scopedContainer1.GetService(); + ODataMessageReaderSettings reader12 = scopedContainer1.GetService(); + + // Assert + Assert.NotNull(reader11); + Assert.NotNull(reader12); + Assert.Equal(reader11, reader12); + + IServiceProvider scopedContainer2 = container.GetRequiredService() + .CreateScope().ServiceProvider; + ODataMessageReaderSettings reader21 = scopedContainer2.GetService(); + ODataMessageReaderSettings reader22 = scopedContainer2.GetService(); + + Assert.NotNull(reader21); + Assert.NotNull(reader22); + Assert.Equal(reader21, reader22); + + Assert.NotEqual(reader11, reader21); + } + + + [Fact] + public void MessageWriterIsScoped() + { + // Arrange + IServiceCollection services = new ServiceCollection(); + services.AddDefaultWebApiServices(); + IServiceProvider container = services.BuildServiceProvider(); + + // Act + IServiceProvider scopedContainer1 = container.GetRequiredService() + .CreateScope().ServiceProvider; + ODataMessageWriterSettings writer11 = scopedContainer1.GetService(); + ODataMessageWriterSettings writer12 = scopedContainer1.GetService(); + + // Assert + Assert.NotNull(writer11); + Assert.NotNull(writer12); + Assert.Equal(writer11, writer12); + + IServiceProvider scopedContainer2 = container.GetRequiredService() + .CreateScope().ServiceProvider; + ODataMessageWriterSettings writer21 = scopedContainer2.GetService(); + ODataMessageWriterSettings writer22 = scopedContainer2.GetService(); + + Assert.NotNull(writer21); + Assert.NotNull(writer22); + Assert.Equal(writer21, writer22); + + Assert.NotEqual(writer11, writer21); + } + private interface ITestService { } private class TestService : ITestService { }