Skip to content

Commit da28d99

Browse files
authored
Refactor method discovery (#268)
1 parent 184d80a commit da28d99

33 files changed

+725
-501
lines changed

src/Grpc.AspNetCore.Server/GrpcEndpointRouteBuilderExtensions.cs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using Grpc.AspNetCore.Server;
2121
using Grpc.AspNetCore.Server.Internal;
22+
using Grpc.AspNetCore.Server.Model.Internal;
2223
using Grpc.Core;
2324
using Microsoft.AspNetCore.Routing;
2425
using Microsoft.Extensions.DependencyInjection;
@@ -44,57 +45,12 @@ public static IEndpointConventionBuilder MapGrpcService<TService>(this IEndpoint
4445
throw new ArgumentNullException(nameof(builder));
4546
}
4647

47-
return builder.MapGrpcService<TService>(configureOptions: null);
48-
}
49-
50-
/// <summary>
51-
/// Maps incoming requests to the specified <typeparamref name="TService"/> type.
52-
/// </summary>
53-
/// <typeparam name="TService">The service type to map requests to.</typeparam>
54-
/// <param name="builder">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
55-
/// <param name="configureOptions">A callback to configure binding options.</param>
56-
/// <returns>An <see cref="IEndpointConventionBuilder"/> for endpoints associated with the service.</returns>
57-
public static IEndpointConventionBuilder MapGrpcService<TService>(this IEndpointRouteBuilder builder, Action<GrpcBindingOptions<TService>>? configureOptions) where TService : class
58-
{
59-
if (builder == null)
60-
{
61-
throw new ArgumentNullException(nameof(builder));
62-
}
63-
6448
ValidateServicesRegistered(builder.ServiceProvider);
6549

66-
var options = new GrpcBindingOptions<TService>();
67-
options.BindAction = ReflectionBind;
68-
options.ModelFactory = new ReflectionMethodModelFactory<TService>();
69-
70-
configureOptions?.Invoke(options);
71-
72-
var callHandlerFactory = builder.ServiceProvider.GetRequiredService<ServerCallHandlerFactory<TService>>();
73-
var serviceMethodsRegistry = builder.ServiceProvider.GetRequiredService<ServiceMethodsRegistry>();
74-
var loggerFactory = builder.ServiceProvider.GetRequiredService<ILoggerFactory>();
75-
76-
var serviceBinder = new GrpcServiceBinder<TService>(builder, options.ModelFactory, callHandlerFactory, serviceMethodsRegistry, loggerFactory);
77-
78-
try
79-
{
80-
options.BindAction(serviceBinder, null);
81-
}
82-
catch (Exception ex)
83-
{
84-
throw new InvalidOperationException($"Error binding gRPC service '{typeof(TService).Name}'.", ex);
85-
}
86-
87-
serviceBinder.CreateUnimplementedEndpoints();
88-
89-
return new CompositeEndpointConventionBuilder(serviceBinder.EndpointConventionBuilders);
90-
}
91-
92-
private static void ReflectionBind<TService>(ServiceBinderBase binder, TService service)
93-
{
94-
var bindMethodInfo = BindMethodFinder.GetBindMethod(typeof(TService));
50+
var serviceRouteBuilder = builder.ServiceProvider.GetRequiredService<ServiceRouteBuilder<TService>>();
51+
var endpointConventionBuilders = serviceRouteBuilder.Build(builder);
9552

96-
// Invoke BindService(ServiceBinderBase, BaseType)
97-
bindMethodInfo.Invoke(null, new object?[] { binder, service });
53+
return new CompositeEndpointConventionBuilder(endpointConventionBuilders);
9854
}
9955

10056
private static void ValidateServicesRegistered(IServiceProvider serviceProvider)

src/Grpc.AspNetCore.Server/GrpcServiceExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using System;
2020
using Grpc.AspNetCore.Server;
2121
using Grpc.AspNetCore.Server.Internal;
22+
using Grpc.AspNetCore.Server.Model;
23+
using Grpc.AspNetCore.Server.Model.Internal;
2224
using Microsoft.Extensions.DependencyInjection.Extensions;
2325
using Microsoft.Extensions.Options;
2426

@@ -63,11 +65,15 @@ public static IGrpcServerBuilder AddGrpc(this IServiceCollection services)
6365
services.AddRouting();
6466
services.AddOptions();
6567
services.TryAddSingleton<GrpcMarkerService>();
66-
services.TryAddSingleton<ServiceMethodsRegistry>();
6768
services.TryAddSingleton(typeof(ServerCallHandlerFactory<>));
6869
services.TryAddScoped(typeof(IGrpcServiceActivator<>), typeof(DefaultGrpcServiceActivator<>));
6970
services.TryAddScoped(typeof(IGrpcInterceptorActivator<>), typeof(DefaultGrpcInterceptorActivator<>));
7071
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<GrpcServiceOptions>, GrpcServiceOptionsSetup>());
72+
73+
// Model
74+
services.TryAddSingleton<ServiceMethodsRegistry>();
75+
services.TryAddSingleton(typeof(ServiceRouteBuilder<>));
76+
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(BinderServiceMethodProvider<>)));
7177

7278
return new GrpcServerBuilder(services);
7379
}

src/Grpc.AspNetCore.Server/Internal/CallHandlers/ClientStreamingServerCallHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Threading.Tasks;
21+
using Grpc.AspNetCore.Server.Model;
2122
using Grpc.Core;
2223
using Microsoft.AspNetCore.Http;
2324
using Microsoft.Extensions.DependencyInjection;

src/Grpc.AspNetCore.Server/Internal/CallHandlers/DuplexStreamingServerCallHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Threading.Tasks;
21+
using Grpc.AspNetCore.Server.Model;
2122
using Grpc.Core;
2223
using Microsoft.AspNetCore.Http;
2324
using Microsoft.Extensions.DependencyInjection;

src/Grpc.AspNetCore.Server/Internal/CallHandlers/ServerStreamingServerCallHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Threading.Tasks;
21+
using Grpc.AspNetCore.Server.Model;
2122
using Grpc.Core;
2223
using Microsoft.AspNetCore.Http;
2324
using Microsoft.Extensions.DependencyInjection;

src/Grpc.AspNetCore.Server/Internal/CallHandlers/UnaryServerCallHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Threading.Tasks;
21+
using Grpc.AspNetCore.Server.Model;
2122
using Grpc.Core;
2223
using Microsoft.AspNetCore.Http;
2324
using Microsoft.Extensions.DependencyInjection;

src/Grpc.AspNetCore.Server/Internal/GrpcServiceBinder.cs

Lines changed: 0 additions & 188 deletions
This file was deleted.

src/Grpc.AspNetCore.Server/Internal/IGrpcMethodModelFactory.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)