コード例 #1
0
        public static Server AddGrpcServices(this
                                             IServiceProvider provider,
                                             ILoggerFactory loggerFactory,
                                             IConfiguration configuration)
        {
            var servicePort = GetPort(configuration);

            GrpcEnvironment.SetLogger(new LogLevelFilterLogger(new GrpcLogger(loggerFactory), LogLevel.Debug));
            GrpcEnvironment.Logger.Debug($@"Starting Build services on port {servicePort}");

            var logger = loggerFactory.CreateLogger <LoggingInterceptor>();

            var builder = new GrpcServerBuilder()
                          .AddInsecurePort(servicePort);

            var services = new List <ServerServiceDefinition>
            {
                KickstartBuildService.BindService(provider.GetRequiredService <KickstartBuildServiceImpl>())
                .Intercept(new LoggingInterceptor(logger, Microsoft.Extensions.Logging.LogLevel.Information)),

                Health.BindService(provider.GetRequiredService <HealthServiceImpl>())
                .Intercept(new LoggingInterceptor(logger, Microsoft.Extensions.Logging.LogLevel.Debug)),
            };

            //AddTracing(provider, services, configuration);

            builder.AddServices(services);

            builder.AddService(ServerReflection.BindService(new ReflectionServiceImpl(
                                                                KickstartBuildService.Descriptor,
                                                                Health.Descriptor)));

            return(builder.Build());
        }
        /// <summary>
        /// Registers one or more services in the dependency injection container to be included in a <see cref="Server"/> and be hosted in a <see cref="IHostedService"/>.
        /// </summary>
        /// <param name="serviceCollection">The target <see cref="IServiceCollection"/> to register the services to.</param>
        /// <param name="serverConfigurator">An action that configures a <see cref="IGrpcServerBuilder"/> with the desired services.</param>
        /// <param name="ports">The ports the services should listen to.</param>
        /// <param name="channelOptions">The options for the services' channels.</param>
        /// <returns>The <see cref="IServiceCollection"/> to which the services were registerd.</returns>
        public static IServiceCollection AddGrpcServer(
            this IServiceCollection serviceCollection,
            Action <IGrpcServerBuilder> serverConfigurator,
            IEnumerable <ServerPort> ports,
            IEnumerable <ChannelOption> channelOptions = null
            )
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            if (serverConfigurator == null)
            {
                throw new ArgumentNullException(nameof(serverConfigurator));
            }

            if (ports == null)
            {
                throw new ArgumentNullException(nameof(ports));
            }

            if (!ports.Any())
            {
                throw new ArgumentException(message: "At least one port must be specified", paramName: nameof(ports));
            }

            var builder = new GrpcServerBuilder(serviceCollection, ports, channelOptions);

            serverConfigurator(builder);
            builder.AddServerToServiceCollection();
            serviceCollection.AddGrpcBackgroundServiceIfNotAlreadyRegistered();
            return(serviceCollection);
        }