From a6504fab68297b0159070d9dfd67389328a608b0 Mon Sep 17 00:00:00 2001 From: Paolo Possanzini Date: Wed, 25 Sep 2024 17:26:16 +0200 Subject: [PATCH] Added GRPC over HTTP2 --- Arbitrer.GRPC/Arbitrer.GRPC.csproj | 1 + Arbitrer.GRPC/Extensions/Extensions.cs | 38 ++++++++++++++++++++++++-- arbitrer.sln.DotSettings.user | 2 ++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Arbitrer.GRPC/Arbitrer.GRPC.csproj b/Arbitrer.GRPC/Arbitrer.GRPC.csproj index 96a8f41..785745b 100644 --- a/Arbitrer.GRPC/Arbitrer.GRPC.csproj +++ b/Arbitrer.GRPC/Arbitrer.GRPC.csproj @@ -21,6 +21,7 @@ + diff --git a/Arbitrer.GRPC/Extensions/Extensions.cs b/Arbitrer.GRPC/Extensions/Extensions.cs index 5ba621d..259bc82 100644 --- a/Arbitrer.GRPC/Extensions/Extensions.cs +++ b/Arbitrer.GRPC/Extensions/Extensions.cs @@ -2,6 +2,7 @@ using System.Security.Cryptography; using System.Text; using Arbitrer.GRPC; +using Grpc.AspNetCore.Server; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Routing; @@ -10,29 +11,60 @@ namespace Arbitrer.GRPC.Extensions { + + public static class Extensions { + + const string ArbitrerGrpcCorsDefaultPolicy = "ArbitrerGRPCDefault"; + /// /// Add the Arbitrer RabbitMQ message dispatcher to the service collection, allowing it to be resolved and used. /// /// The service collection to add the message dispatcher to. /// The configuration settings for the message dispatcher. + /// Grpc service configuration options, if not specified you need to call AddGrpc() + /// registration method. /// The updated service collection. - public static IServiceCollection AddArbitrerGrpcDispatcher(this IServiceCollection services, Action config) + public static IServiceCollection AddArbitrerGrpcDispatcher(this IServiceCollection services, + Action config, + Action grpcOptions = null) { - services.AddGrpc(); + + if (grpcOptions != null) + { + services.Configure(grpcOptions); + services.AddGrpc(); + } + + services.AddCors(o => o.AddPolicy(ArbitrerGrpcCorsDefaultPolicy, builder => + { + builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader() + .WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding"); + })); + services.Configure(config); services.AddSingleton(); return services; } - + public static IEndpointRouteBuilder UseGrpcDispatcher(this IEndpointRouteBuilder host) { host.MapGrpcService(); return host; } + public static IEndpointRouteBuilder UseGrpcWebDispatcher(this IEndpointRouteBuilder host) + { + host.MapGrpcService() + .EnableGrpcWeb() + .RequireCors(ArbitrerGrpcCorsDefaultPolicy); + return host; + } + public static string GetHash(this string input, HashAlgorithm hashAlgorithm) { byte[] data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input)); diff --git a/arbitrer.sln.DotSettings.user b/arbitrer.sln.DotSettings.user index 33f3dd6..665e2db 100644 --- a/arbitrer.sln.DotSettings.user +++ b/arbitrer.sln.DotSettings.user @@ -1,4 +1,6 @@  + ForceIncluded + ForceIncluded INFO