Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update
  • Loading branch information
JamesNK committed May 26, 2025
commit 472f973f51adba630a96605c66128cccf26e0cb0
15 changes: 3 additions & 12 deletions src/Grpc.AspNetCore.Server/GrpcEndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endregion

using System.Diagnostics.CodeAnalysis;
using Grpc.AspNetCore.Server;
using Grpc.AspNetCore.Server.Internal;
using Grpc.AspNetCore.Server.Model.Internal;
using Grpc.Core;
Expand All @@ -31,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder;
/// </summary>
public static class GrpcEndpointRouteBuilderExtensions
{
private sealed class ServerServiceDefinitionDummyService
{
}

/// <summary>
/// Maps incoming requests to the specified <typeparamref name="TService"/> type.
/// </summary>
Expand Down Expand Up @@ -65,10 +62,7 @@ public static GrpcServiceEndpointConventionBuilder MapGrpcService(this IEndpoint
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(serviceDefinition, nameof(serviceDefinition));

var serviceMethodsRegistry = builder.ServiceProvider.GetRequiredService<ServiceMethodsRegistry>();
serviceMethodsRegistry.ServiceDefinitions.Add(serviceDefinition);

var serviceRouteBuilder = builder.ServiceProvider.GetRequiredService<ServiceRouteBuilder<ServerServiceDefinitionDummyService>>();
var serviceRouteBuilder = builder.ServiceProvider.GetRequiredService<ServiceRouteBuilder<ServerServiceDefinitionMarker>>();
var endpointConventionBuilders = serviceRouteBuilder.Build(builder, serviceDefinition);

return new GrpcServiceEndpointConventionBuilder(endpointConventionBuilders);
Expand All @@ -88,10 +82,7 @@ public static GrpcServiceEndpointConventionBuilder MapGrpcService(this IEndpoint

var serviceDefinition = getServiceDefinition(builder.ServiceProvider);

var serviceMethodsRegistry = builder.ServiceProvider.GetRequiredService<ServiceMethodsRegistry>();
serviceMethodsRegistry.ServiceDefinitions.Add(serviceDefinition);

var serviceRouteBuilder = builder.ServiceProvider.GetRequiredService<ServiceRouteBuilder<ServerServiceDefinitionDummyService>>();
var serviceRouteBuilder = builder.ServiceProvider.GetRequiredService<ServiceRouteBuilder<ServerServiceDefinitionMarker>>();
var endpointConventionBuilders = serviceRouteBuilder.Build(builder, serviceDefinition);

return new GrpcServiceEndpointConventionBuilder(endpointConventionBuilders);
Expand Down
5 changes: 4 additions & 1 deletion src/Grpc.AspNetCore.Server/GrpcServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public static IGrpcServerBuilder AddGrpc(this IServiceCollection services)
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(BinderServiceMethodProvider<>)));
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(ServiceDefinitionMethodProvider<>)));

return new GrpcServerBuilder(services);
var builder = new GrpcServerBuilder(services);
builder.AddServiceOptions<ServerServiceDefinitionMarker>(options => options.SuppressCreatingService = true);

return builder;

static void ConfigureRouting(RouteOptions options)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Grpc.AspNetCore.Server/GrpcServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand Down Expand Up @@ -112,6 +112,8 @@ public IList<ICompressionProvider> CompressionProviders
/// Get a collection of interceptors to be executed with every call. Interceptors are executed in order.
/// </summary>
public InterceptorCollection Interceptors { get; } = new InterceptorCollection();

internal bool SuppressCreatingService { get; set; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Grpc.AspNetCore.Server.Internal;
using Grpc.Core;
using Grpc.Shared;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;

namespace Grpc.AspNetCore.Server.Model.Internal;
Expand Down Expand Up @@ -54,25 +55,37 @@ public ProviderServiceBinder(ServiceMethodProviderContext<TService> context)
public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse>? handler)
{
ArgumentNullThrowHelper.ThrowIfNull(handler, nameof(handler));
_context.AddUnaryMethod(method, new List<object>(), (service, request, context) => handler(request, context));
_context.AddUnaryMethod(method, CreateMetadata(), (service, request, context) => handler(request, context));
}

public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse> method, DuplexStreamingServerMethod<TRequest, TResponse>? handler)
{
ArgumentNullThrowHelper.ThrowIfNull(handler, nameof(handler));
_context.AddDuplexStreamingMethod(method, new List<object>(), (service, request, response, context) => handler(request, response, context));
_context.AddDuplexStreamingMethod(method, CreateMetadata(), (service, request, response, context) => handler(request, response, context));
}

public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse>? handler)
{
ArgumentNullThrowHelper.ThrowIfNull(handler, nameof(handler));
_context.AddServerStreamingMethod(method, new List<object>(), (service, request, response, context) => handler(request, response, context));
_context.AddServerStreamingMethod(method, CreateMetadata(), (service, request, response, context) => handler(request, response, context));
}

public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse> method, ClientStreamingServerMethod<TRequest, TResponse>? handler)
{
ArgumentNullThrowHelper.ThrowIfNull(handler, nameof(handler));
_context.AddClientStreamingMethod(method, new List<object>(), (service, request, context) => handler(request, context));
_context.AddClientStreamingMethod(method, CreateMetadata(), (service, request, context) => handler(request, context));
}

private static List<object> CreateMetadata()
{
// Accepting CORS preflight means gRPC will allow requests with OPTIONS + preflight headers.
// If CORS middleware hasn't been configured then the request will reach gRPC handler.
// gRPC will return 405 response and log that CORS has not been configured.
var metadata = new List<object>
{
new HttpMethodMetadata(new[] { "POST" }, acceptCorsPreflight: true)
};
return metadata;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#endregion

using Grpc.Core;

namespace Grpc.AspNetCore.Server.Model.Internal;

/// <summary>
Expand All @@ -26,6 +24,4 @@ namespace Grpc.AspNetCore.Server.Model.Internal;
internal sealed class ServiceMethodsRegistry
{
public List<MethodModel> Methods { get; } = new List<MethodModel>();

public List<ServerServiceDefinition> ServiceDefinitions { get; } = new List<ServerServiceDefinition>();
}
23 changes: 23 additions & 0 deletions src/Grpc.AspNetCore.Server/ServerServiceDefinitionMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

namespace Grpc.AspNetCore.Server;

internal sealed class ServerServiceDefinitionMarker
{
}
4 changes: 3 additions & 1 deletion src/Shared/Server/MethodOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static MethodOptions Create(IEnumerable<GrpcServiceOptions> serviceOption
bool? enableDetailedErrors = null;
string? responseCompressionAlgorithm = null;
CompressionLevel? responseCompressionLevel = null;
bool? suppressCreatingService = null;

foreach (var options in serviceOptions.Reverse())
{
Expand All @@ -139,6 +140,7 @@ public static MethodOptions Create(IEnumerable<GrpcServiceOptions> serviceOption
enableDetailedErrors ??= options.EnableDetailedErrors;
responseCompressionAlgorithm ??= options.ResponseCompressionAlgorithm;
responseCompressionLevel ??= options.ResponseCompressionLevel;
suppressCreatingService ??= options.SuppressCreatingService;
}

var interceptors = new InterceptorCollection();
Expand All @@ -153,7 +155,7 @@ public static MethodOptions Create(IEnumerable<GrpcServiceOptions> serviceOption
enableDetailedErrors: enableDetailedErrors,
responseCompressionAlgorithm: responseCompressionAlgorithm,
responseCompressionLevel: responseCompressionLevel,
suppressCreatingService: true
suppressCreatingService: suppressCreatingService ?? false
);
}

Expand Down