示例#1
0
        private void MapGrpcSettings()
        {
            var grpcOptions = new GrpcOptions();

            Configuration.GetSection("Grpc").Bind(grpcOptions);
            AppSettingsProvider.GrpcSettings = grpcOptions;
        }
        public HealthCheckService(IGrpcClientService grpcClientService, IOptions <HealthCheckOptions> healthCheckOptions, IOptions <GrpcOptions> grpcOptions,
                                  ILogger <HealthCheckService> logger)
        {
            _grpcClientService = grpcClientService;

            _healthCheckOptions = healthCheckOptions.Value;
            _grpcOptions        = grpcOptions.Value;

            _logger = logger;
        }
示例#3
0
        public GrpcRequestMiddleware(IOptionsMonitor <GrpcOptions> options, CacheHelper cacheHelper, Func <IEnumerable <JsonConverter> > convertersFactory)
        {
            _cacheHelper = cacheHelper;
            _options     = options.CurrentValue;
            options.OnChange(cfg => _options = cfg);
            var converters = convertersFactory?.Invoke();

            if (converters != null)
            {
                foreach (var jsonConverter in converters)
                {
                    _jsonSerializerSettings.Converters.Add(jsonConverter);
                }
            }
        }
示例#4
0
        public ApplicationWatcherService(IRegistryService registryService, IProcessService processService, IFileWrapperService fileWrapperService,
                                         IGrpcClientService grpcClient,
                                         IOptions <RegistryOptions> registryOptions, IOptions <HealthCheckOptions> healthCheckOptions, IOptions <GrpcOptions> grpcOptions,
                                         ILogger <ApplicationWatcherService> logger)
        {
            _registryService    = registryService;
            _processService     = processService;
            _fileWrapperService = fileWrapperService;
            _grpcClient         = grpcClient;

            _registryOptions    = registryOptions.Value;
            _healthCheckOptions = healthCheckOptions.Value;
            _grpcOptions        = grpcOptions.Value;

            _logger = logger;
        }
示例#5
0
        static void Main(string[] args)
        {
            var options = new GrpcOptions()
            {
                Host           = "127.0.0.1",
                Port           = 3002,
                NamespaceName  = "Atlantis.Simple",
                ServiceName    = "AtlantisService",
                ScanAssemblies = new string[]
                {
                    typeof(IPersonMessageServicer).Assembly.FullName
                }
            };

            IServiceCollection services = new ServiceCollection();

            services.AddLogging();
            services.AddKadderGrpcClient(builder =>
            {
                builder.RegClient(options);
                builder.RegShareInterceptor <Kadder.Simple.Client.LoggerInterceptor>();
            });

            var provider = services.BuildServiceProvider();

            provider.ApplyKadderGrpcClient();

            TestInterceptor(provider);

            Console.ReadLine();

            // var channel = new Channel("127.0.0.1", 3002, ChannelCredentials.Insecure);

            // channel.ConnectAsync().Wait();

            // AtlantisServiceClient client=new AtlantisServiceClient(channel);
            //
            // var result= client.Hello(message);

            // // var serailizer=new ProtobufBinarySerializer();
            // // var s=serailizer.Serialize(message);

            // // foreach(var b in s)
            // // {
            // //     Console.Write($" {b}");
            // }
        }
示例#6
0
        public static IServiceCollection AddProductServiceAsGrpc(this IServiceCollection services, Action <GrpcOptions> options)
        {
            GrpcClientFactory.AllowUnencryptedHttp2 = true;

            var grpcOptions = new GrpcOptions();

            options?.Invoke(grpcOptions);

            services.AddScoped(sp => GrpcChannel.ForAddress(grpcOptions.ServerAddress));
            services.AddScoped(sp =>
            {
                var channel = sp.GetRequiredService <GrpcChannel>();
                return(channel.CreateGrpcService <IProductService>());
            });

            return(services);
        }
示例#7
0
        public static void SetupGrpc(this IServiceCollection services, IConfiguration configuration)
        {
            GrpcOptions = services.GetTypedOptions <GrpcOptions>(configuration, "Grpc");

            if (!GrpcOptions.EnableGrpc || string.IsNullOrEmpty(GrpcOptions.GrpcServer))
            {
                return;
            }

            services.AddGrpc(options =>
            {
                options.MaxReceiveMessageSize = GrpcOptions.MaxReceiveMessageSize * 1024 * 1024; // 16 MB
            });

            var defaultMethodConfig = new MethodConfig
            {
                Names       = { MethodName.Default },
                RetryPolicy = GetRetryPolicy(GrpcOptions.RetryPatternOptions)
            };

            if (GrpcOptions.UseDevCertificate)
            {
                var httpHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = GetBypass
                };

                services.AddSingleton(GrpcChannel.ForAddress(GrpcOptions.GrpcServer, new GrpcChannelOptions {
                    HttpHandler = httpHandler, ServiceConfig = new ServiceConfig {
                        MethodConfigs = { defaultMethodConfig }
                    }
                }));
            }
            else
            {
                services.AddSingleton(GrpcChannel.ForAddress(GrpcOptions.GrpcServer, new GrpcChannelOptions
                {
                    ServiceConfig = new ServiceConfig {
                        MethodConfigs = { defaultMethodConfig }
                    }
                }));
            }
        }
示例#8
0
        static void Main(string[] args)
        {
            var options = new GrpcOptions()
            {
                Host           = "127.0.0.1",
                Port           = 3002,
                NamespaceName  = "Atlantis.Simple",
                ServiceName    = "AtlantisService",
                ScanAssemblies = new string[]
                {
                    typeof(IPersonMessageServicer).Assembly.FullName
                }
            };

            IServiceCollection services = new ServiceCollection();

            services.AddLogging();
            services.AddKadderGrpcClient(builder =>
            {
                builder.RegClient(options);
            });

            var provider = services.BuildServiceProvider();

            provider.ApplyKadderGrpcClient();

            try
            {
                var servicer = provider.GetService <IPersonMessageServicer>();
                var message  = new HelloMessage()
                {
                    Name = "DotNet"
                };
                var stopwatch = new Stopwatch();
                var resuslt   = servicer.HelloAsync(message).Result;
                stopwatch.Start();
                System.Threading.Tasks.Parallel.For(0, 10000, i => {
                    var result = servicer.HelloAsync(message).Result;
                    Console.WriteLine(result.Result);
                });
                stopwatch.Stop();
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // var channel = new Channel("127.0.0.1", 3002, ChannelCredentials.Insecure);

            // channel.ConnectAsync().Wait();

            // AtlantisServiceClient client=new AtlantisServiceClient(channel);
            //
            // var result= client.Hello(message);

            // // var serailizer=new ProtobufBinarySerializer();
            // // var s=serailizer.Serialize(message);

            // // foreach(var b in s)
            // // {
            // //     Console.Write($" {b}");
            // }
        }