-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathGrpcServerServiceCollectionExtensions.cs
67 lines (60 loc) · 2.77 KB
/
GrpcServerServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Reflection;
using Grpc.AspNetCore.Server;
using Havit.Blazor.Grpc.Core;
using Havit.Blazor.Grpc.Server.GlobalizationLocalization;
using Havit.Blazor.Grpc.Server.ServerExceptions;
using Havit.Diagnostics.Contracts;
using Microsoft.Extensions.DependencyInjection;
using ProtoBuf.Grpc.Configuration;
using ProtoBuf.Grpc.Server;
using ProtoBuf.Meta;
namespace Havit.Blazor.Grpc.Server;
public static class GrpcServerServiceCollectionExtensions
{
/// <summary>
/// Adds the necessary infrastructure for gRPC servers.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="assembliesToScanForDataContracts">Assembly to scan for data contracts</param>
/// <param name="configureOptions">gRPC Service options</param>
public static void AddGrpcServerInfrastructure(
this IServiceCollection services,
Assembly assemblyToScanForDataContracts,
Action<GrpcServiceOptions> configureOptions = null)
{
AddGrpcServerInfrastructure(services, [assemblyToScanForDataContracts], configureOptions);
}
/// <summary>
/// Adds the necessary infrastructure for gRPC servers.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="assembliesToScanForDataContracts">Assemblies to scan for data contracts</param>
/// <param name="configureOptions">gRPC Service options</param>
public static void AddGrpcServerInfrastructure(
this IServiceCollection services,
Assembly[] assembliesToScanForDataContracts,
Action<GrpcServiceOptions> configureOptions = null)
{
Contract.Requires<ArgumentNullException>(assembliesToScanForDataContracts is not null);
services.AddSingleton<GlobalizationLocalizationGrpcServerInterceptor>();
services.AddSingleton<ServerExceptionsGrpcServerInterceptor>();
services.AddSingleton(BinderConfiguration.Create(
marshallerFactories: CreateMarshallerFactories(assembliesToScanForDataContracts),
binder: new ServiceBinderWithServiceResolutionFromServiceCollection(services)));
services.AddCodeFirstGrpc(options =>
{
options.Interceptors.Add<GlobalizationLocalizationGrpcServerInterceptor>();
options.Interceptors.Add<ServerExceptionsGrpcServerInterceptor>();
options.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
configureOptions?.Invoke(options);
});
}
/// <summary>
/// Creates marshaller factories for the specified assemblies.
/// Each assembly has its own marshaller factory.
/// </summary>
private static List<MarshallerFactory> CreateMarshallerFactories(Assembly[] assembliesToScanForDataContracts) =>
assembliesToScanForDataContracts
.Select(assembly => ProtoBufMarshallerFactory.Create(RuntimeTypeModel.Create().RegisterApplicationContracts(assembly)))
.ToList();
}