Ejemplo n.º 1
0
        public static IServiceCollection AddGrpc(this IServiceCollection services)
        {
            // Channels
            services.RegisterOutputChannel();

            // Internal logging
            services.AddLogging(logging =>
            {
                logging.Services.AddSingleton <ILoggerProvider, GrpcFunctionsHostLoggerProvider>();
                logging.Services.AddSingleton <IWorkerDiagnostics, GrpcWorkerDiagnostics>();
            });

            // gRPC Core services
            services.AddSingleton <IWorker, GrpcWorker>();
            services.AddSingleton <FunctionRpcClient>(p =>
            {
                IOptions <GrpcWorkerStartupOptions> argumentsOptions = p.GetService <IOptions <GrpcWorkerStartupOptions> >()
                                                                       ?? throw new InvalidOperationException("gRPC Services are not correctly registered.");

                GrpcWorkerStartupOptions arguments = argumentsOptions.Value;

                string uriString = $"http://{arguments.Host}:{arguments.Port}";
                if (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri? grpcUri))
                {
                    throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
                }

                GrpcChannel grpcChannel = GrpcChannel.ForAddress(grpcUri, new GrpcChannelOptions()
                {
                    MaxReceiveMessageSize = arguments.GrpcMaxMessageLength,
                    MaxSendMessageSize    = arguments.GrpcMaxMessageLength,
                    Credentials           = ChannelCredentials.Insecure
                });

                return(new FunctionRpcClient(grpcChannel));
            });

            services.AddOptions <GrpcWorkerStartupOptions>()
            .Configure <IConfiguration>((arguments, config) =>
            {
                config.Bind(arguments);
            });

            return(services);
        }
Ejemplo n.º 2
0
        public static IServiceCollection AddGrpc(this IServiceCollection services)
        {
            // Channels
            services.RegisterOutputChannel();

            // Internal logging
            services.AddLogging(logging =>
            {
                logging.Services.AddSingleton <ILoggerProvider, GrpcFunctionsHostLoggerProvider>();
                logging.Services.AddSingleton <IWorkerDiagnostics, GrpcWorkerDiagnostics>();
            });

            // gRPC Core services
            services.AddSingleton <IWorker, GrpcWorker>();
            services.AddSingleton <FunctionRpcClient>(p =>
            {
                IOptions <GrpcWorkerStartupOptions> argumentsOptions = p.GetService <IOptions <GrpcWorkerStartupOptions> >()
                                                                       ?? throw new InvalidOperationException("gRPC Serivces are not correctly registered.");

                GrpcWorkerStartupOptions arguments = argumentsOptions.Value;

                GrpcChannel grpcChannel = GrpcChannel.ForAddress($"http://{arguments.Host}:{arguments.Port}", new GrpcChannelOptions()
                {
                    Credentials = ChannelCredentials.Insecure
                });

                return(new FunctionRpcClient(grpcChannel));
            });

            services.AddOptions <GrpcWorkerStartupOptions>()
            .Configure <IConfiguration>((arguments, config) =>
            {
                config.Bind(arguments);
            });

            return(services);
        }