Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update DI settings #1344

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we had AddSingleton in the first place. Also check whether the comments above are still relevant, we no longer use "prototypes".

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