Skip to content

Commit 23bd3b3

Browse files
authored
Call AddMetrics inside AddRouting (#50168)
1 parent 7328bc4 commit 23bd3b3

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Http/Routing/src/DependencyInjection/RoutingServiceCollectionExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public static IServiceCollection AddRoutingCore(this IServiceCollection services
4343
{
4444
ArgumentNullException.ThrowIfNull(services);
4545

46+
// Required for IMeterFactory dependency.
47+
services.AddMetrics();
48+
4649
services.TryAddTransient<IInlineConstraintResolver, DefaultInlineConstraintResolver>();
4750
services.TryAddTransient<ObjectPoolProvider, DefaultObjectPoolProvider>();
4851
services.TryAddSingleton<ObjectPool<UriBuildingContext>>(s =>

src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Reference Include="Microsoft.AspNetCore.Http.Features" />
4747
<Reference Include="Microsoft.AspNetCore.Http.Abstractions" />
4848
<Reference Include="Microsoft.AspNetCore.Routing.Abstractions" />
49+
<Reference Include="Microsoft.Extensions.Diagnostics" />
4950
<Reference Include="Microsoft.Extensions.Features" />
5051
<Reference Include="Microsoft.Extensions.ObjectPool" />
5152
<Reference Include="Microsoft.Extensions.Options" />

src/Http/Routing/test/UnitTests/EndpointRoutingMiddlewareTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.Routing.Matching;
1111
using Microsoft.AspNetCore.Routing.TestObjects;
1212
using Microsoft.AspNetCore.Testing;
13+
using Microsoft.Extensions.DependencyInjection;
1314
using Microsoft.Extensions.Logging;
1415
using Microsoft.Extensions.Logging.Abstractions;
1516
using Microsoft.Extensions.Logging.Testing;
@@ -393,6 +394,36 @@ public async Task Endpoint_HasBodySizeFeature_SetUsingSizeLimitMetadata(bool isR
393394
Assert.Equal(expectedRequestSizeLimit, actualRequestSizeLimit);
394395
}
395396

397+
[Fact]
398+
public async Task Create_WithoutHostBuilder_Success()
399+
{
400+
// Arrange
401+
var httpContext = CreateHttpContext();
402+
403+
var services = new ServiceCollection();
404+
services.AddLogging();
405+
services.AddOptions();
406+
services.AddSingleton<DiagnosticListener>(s => new DiagnosticListener("Test"));
407+
services.AddRouting();
408+
409+
var applicationBuilder = new ApplicationBuilder(services.BuildServiceProvider());
410+
411+
applicationBuilder.UseRouting();
412+
applicationBuilder.UseEndpoints(endpoints =>
413+
{
414+
endpoints.MapGet("/", (HttpContext c) => Task.CompletedTask);
415+
});
416+
417+
var requestDelegate = applicationBuilder.Build();
418+
419+
// Act
420+
await requestDelegate(httpContext);
421+
422+
// Assert
423+
var endpointFeature = httpContext.Features.Get<IEndpointFeature>();
424+
Assert.NotNull(endpointFeature);
425+
}
426+
396427
private class RequestSizeLimitMetadata(long? maxRequestBodySize) : IRequestSizeLimitMetadata
397428
{
398429

0 commit comments

Comments
 (0)