Skip to content
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 @@ -389,8 +389,8 @@ internal static void EmitServiceParameterPreparation(this EndpointParameter endp
// Unlike other scenarios, this will result in an exception being thrown
// at runtime.
var assigningCode = endpointParameter.IsOptional ?
$"httpContext.RequestServices.GetService<{endpointParameter.Type}>();" :
$"httpContext.RequestServices.GetRequiredService<{endpointParameter.Type}>()";
$"httpContext.RequestServices.GetService<{endpointParameter.Type.ToDisplayString(EmitterConstants.DisplayFormat)}>();" :
$"httpContext.RequestServices.GetRequiredService<{endpointParameter.Type.ToDisplayString(EmitterConstants.DisplayFormat)}>()";
codeWriter.WriteLine($"var {endpointParameter.EmitHandlerArgument()} = {assigningCode};");
}

Expand All @@ -404,8 +404,8 @@ internal static void EmitKeyedServiceParameterPreparation(this EndpointParameter
codeWriter.EndBlock();

var assigningCode = endpointParameter.IsOptional ?
$"httpContext.RequestServices.GetKeyedService<{endpointParameter.Type}>({endpointParameter.KeyedServiceKey});" :
$"httpContext.RequestServices.GetRequiredKeyedService<{endpointParameter.Type}>({endpointParameter.KeyedServiceKey})";
$"httpContext.RequestServices.GetKeyedService<{endpointParameter.Type.ToDisplayString(EmitterConstants.DisplayFormat)}>({endpointParameter.KeyedServiceKey});" :
$"httpContext.RequestServices.GetRequiredKeyedService<{endpointParameter.Type.ToDisplayString(EmitterConstants.DisplayFormat)}>({endpointParameter.KeyedServiceKey})";
codeWriter.WriteLine($"var {endpointParameter.EmitHandlerArgument()} = {assigningCode};");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.Http.Generated
{
var wasParamCheckFailure = false;
// Endpoint Parameter: svc (Type = Microsoft.AspNetCore.Http.Generators.Tests.TestService, IsOptional = False, IsParsable = False, IsArray = False, Source = Service)
var svc_local = httpContext.RequestServices.GetRequiredService<Microsoft.AspNetCore.Http.Generators.Tests.TestService>();
var svc_local = httpContext.RequestServices.GetRequiredService<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService>();

if (wasParamCheckFailure)
{
Expand All @@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.Http.Generated
{
var wasParamCheckFailure = false;
// Endpoint Parameter: svc (Type = Microsoft.AspNetCore.Http.Generators.Tests.TestService, IsOptional = False, IsParsable = False, IsArray = False, Source = Service)
var svc_local = httpContext.RequestServices.GetRequiredService<Microsoft.AspNetCore.Http.Generators.Tests.TestService>();
var svc_local = httpContext.RequestServices.GetRequiredService<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService>();

if (wasParamCheckFailure)
{
Expand Down Expand Up @@ -207,7 +207,7 @@ namespace Microsoft.AspNetCore.Http.Generated
{
var wasParamCheckFailure = false;
// Endpoint Parameter: svc (Type = System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>, IsOptional = False, IsParsable = False, IsArray = False, Source = Service)
var svc_local = httpContext.RequestServices.GetRequiredService<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();
var svc_local = httpContext.RequestServices.GetRequiredService<global::System.Collections.Generic.IEnumerable<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();

if (wasParamCheckFailure)
{
Expand All @@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.Http.Generated
{
var wasParamCheckFailure = false;
// Endpoint Parameter: svc (Type = System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>, IsOptional = False, IsParsable = False, IsArray = False, Source = Service)
var svc_local = httpContext.RequestServices.GetRequiredService<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();
var svc_local = httpContext.RequestServices.GetRequiredService<global::System.Collections.Generic.IEnumerable<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();

if (wasParamCheckFailure)
{
Expand Down Expand Up @@ -308,9 +308,9 @@ namespace Microsoft.AspNetCore.Http.Generated
{
var wasParamCheckFailure = false;
// Endpoint Parameter: svc (Type = Microsoft.AspNetCore.Http.Generators.Tests.TestService?, IsOptional = True, IsParsable = False, IsArray = False, Source = Service)
var svc_local = httpContext.RequestServices.GetService<Microsoft.AspNetCore.Http.Generators.Tests.TestService?>();;
var svc_local = httpContext.RequestServices.GetService<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService?>();;
// Endpoint Parameter: svcs (Type = System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>, IsOptional = False, IsParsable = False, IsArray = False, Source = Service)
var svcs_local = httpContext.RequestServices.GetRequiredService<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();
var svcs_local = httpContext.RequestServices.GetRequiredService<global::System.Collections.Generic.IEnumerable<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();

if (wasParamCheckFailure)
{
Expand All @@ -333,9 +333,9 @@ namespace Microsoft.AspNetCore.Http.Generated
{
var wasParamCheckFailure = false;
// Endpoint Parameter: svc (Type = Microsoft.AspNetCore.Http.Generators.Tests.TestService?, IsOptional = True, IsParsable = False, IsArray = False, Source = Service)
var svc_local = httpContext.RequestServices.GetService<Microsoft.AspNetCore.Http.Generators.Tests.TestService?>();;
var svc_local = httpContext.RequestServices.GetService<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService?>();;
// Endpoint Parameter: svcs (Type = System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>, IsOptional = False, IsParsable = False, IsArray = False, Source = Service)
var svcs_local = httpContext.RequestServices.GetRequiredService<System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();
var svcs_local = httpContext.RequestServices.GetRequiredService<global::System.Collections.Generic.IEnumerable<global::Microsoft.AspNetCore.Http.Generators.Tests.TestService>>();

if (wasParamCheckFailure)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ internal static string GetMapActionString(string sources, string className = "Te
using Microsoft.AspNetCore.Http.Generators.Tests;
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.DependencyInjection;
using Http;

public static class {{className}}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;

Expand Down Expand Up @@ -225,6 +226,32 @@ public async Task ThrowsIfDiContainerDoesNotSupportKeyedServices()
Assert.Equal("Unable to resolve service referenced by FromKeyedServicesAttribute. The service provider doesn't support keyed services.", exception.Message);
}

// See: https://github.com/dotnet/aspnetcore/issues/58633
[Fact]
public async Task RequestDelegateGeneratesCompilableCodeForKeyedServiceInNamespaceHttp()
{
var source = """
app.MapGet("/hello", ([FromKeyedServices("example")] global::Http.ExampleService e) => e.Act("To be or not to be…"));
""";
var (results, compilation) = await RunGeneratorAsync(source);

// Ironically, the same error this is testing would bite us here, so we must globally qualify the type name.
var serviceProvider = CreateServiceProvider((serviceCollection) => serviceCollection.AddKeyedSingleton<global::Http.ExampleService>("example"));
var endpoint = GetEndpointFromCompilation(compilation, serviceProvider: serviceProvider);

VerifyStaticEndpointModel(results, endpointModel =>
{
Assert.Equal("MapGet", endpointModel.HttpMethod);
var p = Assert.Single(endpointModel.Parameters);
Assert.Equal(EndpointParameterSource.KeyedService, p.Source);
Assert.Equal("e", p.SymbolName);
});

var httpContext = CreateHttpContext(serviceProvider);
await endpoint.RequestDelegate(httpContext);
await VerifyResponseBodyAsync(httpContext, "To be or not to be…");
}

private class MockServiceProvider : IServiceProvider, ISupportRequiredService
{
public object GetService(Type serviceType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,30 @@ public async Task RequestDelegateHandlesStringValuesFromExplicitQueryStringSourc
Assert.Equal(new[] { 4, 5, 6 }, (int[])httpContext.Items["headers"]!);
Assert.Equal(new[] { 7, 8, 9 }, (int[])httpContext.Items["form"]!);
}

// See: https://github.com/dotnet/aspnetcore/issues/58633
[Fact]
public async Task RequestDelegateGeneratesCompilableCodeForServiceInNamespaceHttp()
{
var source = """
app.MapGet("/hello", ([FromServices] ExampleService e) => e.Act("To be or not to be…"));
""";
var (results, compilation) = await RunGeneratorAsync(source);

// Ironically, the same error this is testing would bite us here, so we must globally qualify the type name.
var serviceProvider = CreateServiceProvider((serviceCollection) => serviceCollection.AddSingleton<global::Http.ExampleService>());
var endpoint = GetEndpointFromCompilation(compilation, serviceProvider: serviceProvider);

VerifyStaticEndpointModel(results, endpointModel =>
{
Assert.Equal("MapGet", endpointModel.HttpMethod);
var p = Assert.Single(endpointModel.Parameters);
Assert.Equal(EndpointParameterSource.Service, p.Source);
Assert.Equal("e", p.SymbolName);
});

var httpContext = CreateHttpContext(serviceProvider);
await endpoint.RequestDelegate(httpContext);
await VerifyResponseBodyAsync(httpContext, "To be or not to be…");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;

namespace Http;

#nullable enable

public sealed class ExampleService
{
public string Act(string line) => line;
}
Loading