Skip to content

Commit

Permalink
update di settings (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
marabooy authored Nov 13, 2024
1 parent f7b2fac commit 9a0c7db
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.OData.ModelBuilder" Version="2.0.0" />
<PackageReference Include="Microsoft.OData.Core" Version="8.0.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="8.0.1" />
<PackageReference Include="Microsoft.Spatial" Version="8.0.1" />
<PackageReference Include="Microsoft.OData.Core" Version="8.0.2" />
<PackageReference Include="Microsoft.OData.Edm" Version="8.0.2" />
<PackageReference Include="Microsoft.Spatial" Version="8.0.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9471,9 +9471,9 @@
</member>
<member name="M:Microsoft.AspNetCore.OData.Query.Expressions.QueryBinder.CreateDynamicPropertyAccessExpression(System.Linq.Expressions.Expression,System.String,Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext)">
<summary>
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.
</summary>
<param name="containerPropertyAccessExpr">The dynamic properties container property access expression.</param>
<param name="containerPropertyAccessExpr">The container property access expression.</param>
<param name="propertyName">The dynamic property name.</param>
<param name="context">The query binder context.</param>
<returns>The LINQ <see cref="T:System.Linq.Expressions.Expression"/> created.</returns>
Expand Down Expand Up @@ -9522,7 +9522,7 @@
</member>
<member name="M:Microsoft.AspNetCore.OData.Query.Expressions.QueryBinder.CreateNestedDynamicPropertyAccessExpression(System.Linq.Expressions.Expression,System.String,Microsoft.OData.UriParser.QueryNodeKind,Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext)">
<summary>
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.
</summary>
<param name="sourceExpr">The source expression.</param>
<param name="propertyName">The property name.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IServiceScopeFactory>()
.CreateScope().ServiceProvider;
ODataMessageReaderSettings reader11 = scopedContainer1.GetService<ODataMessageReaderSettings>();
ODataMessageReaderSettings reader12 = scopedContainer1.GetService<ODataMessageReaderSettings>();

// Assert
Assert.NotNull(reader11);
Assert.NotNull(reader12);
Assert.Equal(reader11, reader12);

IServiceProvider scopedContainer2 = container.GetRequiredService<IServiceScopeFactory>()
.CreateScope().ServiceProvider;
ODataMessageReaderSettings reader21 = scopedContainer2.GetService<ODataMessageReaderSettings>();
ODataMessageReaderSettings reader22 = scopedContainer2.GetService<ODataMessageReaderSettings>();

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<IServiceScopeFactory>()
.CreateScope().ServiceProvider;
ODataMessageWriterSettings writer11 = scopedContainer1.GetService<ODataMessageWriterSettings>();
ODataMessageWriterSettings writer12 = scopedContainer1.GetService<ODataMessageWriterSettings>();

// Assert
Assert.NotNull(writer11);
Assert.NotNull(writer12);
Assert.Equal(writer11, writer12);

IServiceProvider scopedContainer2 = container.GetRequiredService<IServiceScopeFactory>()
.CreateScope().ServiceProvider;
ODataMessageWriterSettings writer21 = scopedContainer2.GetService<ODataMessageWriterSettings>();
ODataMessageWriterSettings writer22 = scopedContainer2.GetService<ODataMessageWriterSettings>();

Assert.NotNull(writer21);
Assert.NotNull(writer22);
Assert.Equal(writer21, writer22);

Assert.NotEqual(writer11, writer21);
}

private interface ITestService { }

private class TestService : ITestService { }
Expand Down

0 comments on commit 9a0c7db

Please sign in to comment.