-
-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #864 from Cysharp/feature/ReworkBinder
Rework gRPC method binding logic
- Loading branch information
Showing
60 changed files
with
3,208 additions
and
1,599 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Grpc.Core; | ||
using MagicOnion.Server.Internal; | ||
|
||
namespace MagicOnion.Server.Binder; | ||
|
||
public interface IMagicOnionGrpcMethod | ||
{ | ||
MethodType MethodType { get; } | ||
Type ServiceImplementationType { get; } | ||
string ServiceName { get; } | ||
string MethodName { get; } | ||
MethodHandlerMetadata Metadata { get; } | ||
} | ||
|
||
public interface IMagicOnionGrpcMethod<TService> : IMagicOnionGrpcMethod | ||
where TService : class | ||
{ | ||
void Bind(IMagicOnionGrpcMethodBinder<TService> binder); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/MagicOnion.Server/Binder/IMagicOnionGrpcMethodBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace MagicOnion.Server.Binder; | ||
|
||
public interface IMagicOnionGrpcMethodBinder<TService> | ||
where TService : class | ||
{ | ||
void BindUnary<TRequest, TResponse, TRawRequest, TRawResponse>(IMagicOnionUnaryMethod<TService, TRequest, TResponse, TRawRequest, TRawResponse> method) | ||
where TRawRequest : class | ||
where TRawResponse : class; | ||
void BindClientStreaming<TRequest, TResponse, TRawRequest, TRawResponse>(MagicOnionClientStreamingMethod<TService, TRequest, TResponse, TRawRequest, TRawResponse> method) | ||
where TRawRequest : class | ||
where TRawResponse : class; | ||
void BindServerStreaming<TRequest, TResponse, TRawRequest, TRawResponse>(MagicOnionServerStreamingMethod<TService, TRequest, TResponse, TRawRequest, TRawResponse> method) | ||
where TRawRequest : class | ||
where TRawResponse : class; | ||
void BindDuplexStreaming<TRequest, TResponse, TRawRequest, TRawResponse>(MagicOnionDuplexStreamingMethod<TService, TRequest, TResponse, TRawRequest, TRawResponse> method) | ||
where TRawRequest : class | ||
where TRawResponse : class; | ||
void BindStreamingHub(MagicOnionStreamingHubConnectMethod<TService> method); | ||
} |
64 changes: 64 additions & 0 deletions
64
src/MagicOnion.Server/Binder/IMagicOnionGrpcMethodProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace MagicOnion.Server.Binder; | ||
|
||
public interface IMagicOnionGrpcMethodProvider | ||
{ | ||
void MapAllSupportedServiceTypes(MagicOnionGrpcServiceMappingContext context); | ||
IReadOnlyList<IMagicOnionGrpcMethod> GetGrpcMethods<TService>() where TService : class; | ||
IReadOnlyList<IMagicOnionStreamingHubMethod> GetStreamingHubMethods<TService>() where TService : class; | ||
} | ||
|
||
public class MagicOnionGrpcServiceMappingContext(IEndpointRouteBuilder builder) : IEndpointConventionBuilder | ||
{ | ||
readonly List<MagicOnionServiceEndpointConventionBuilder> innerBuilders = new(); | ||
|
||
public void Map<T>() | ||
where T : class, IServiceMarker | ||
{ | ||
innerBuilders.Add(new MagicOnionServiceEndpointConventionBuilder(builder.MapGrpcService<T>())); | ||
} | ||
|
||
public void Map(Type t) | ||
{ | ||
VerifyServiceType(t); | ||
|
||
innerBuilders.Add(new MagicOnionServiceEndpointConventionBuilder((GrpcServiceEndpointConventionBuilder)typeof(GrpcEndpointRouteBuilderExtensions) | ||
.GetMethod(nameof(GrpcEndpointRouteBuilderExtensions.MapGrpcService))! | ||
.MakeGenericMethod(t) | ||
.Invoke(null, [builder])!)); | ||
} | ||
|
||
static void VerifyServiceType(Type type) | ||
{ | ||
if (!typeof(IServiceMarker).IsAssignableFrom(type)) | ||
{ | ||
throw new InvalidOperationException($"Type '{type.FullName}' is not marked as MagicOnion service or hub."); | ||
} | ||
if (!type.GetInterfaces().Any(x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IService<>) || x.GetGenericTypeDefinition() == typeof(IStreamingHub<,>)))) | ||
{ | ||
throw new InvalidOperationException($"Type '{type.FullName}' has no implementation for Service or StreamingHub"); | ||
} | ||
if (type.IsAbstract) | ||
{ | ||
throw new InvalidOperationException($"Type '{type.FullName}' is abstract. A service type must be non-abstract class."); | ||
} | ||
if (type.IsInterface) | ||
{ | ||
throw new InvalidOperationException($"Type '{type.FullName}' is interface. A service type must be class."); | ||
} | ||
if (type.IsGenericType && type.IsGenericTypeDefinition) | ||
{ | ||
throw new InvalidOperationException($"Type '{type.FullName}' is generic type definition. A service type must be plain or constructed-generic class."); | ||
} | ||
} | ||
|
||
void IEndpointConventionBuilder.Add(Action<EndpointBuilder> convention) | ||
{ | ||
foreach (var innerBuilder in innerBuilders) | ||
{ | ||
innerBuilder.Add(convention); | ||
} | ||
} | ||
} |
Oops, something went wrong.