public static void Main(string[] args) { // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755 Server server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }) { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) } }; server.Start(); Channel channel = new Channel("localhost", server.Ports.Single().BoundPort, ChannelCredentials.Insecure); try { var client = new Greeter.GreeterClient(channel); String user = "******"; var reply = client.SayHello(new HelloRequest { Name = user }); Console.WriteLine("Greeting: " + reply.Message); Console.WriteLine("Success!"); } finally { channel.ShutdownAsync().Wait(); server.ShutdownAsync().Wait(); } }
public static void Main(string[] args) { string host = "127.0.0.1"; if (args != null && args.Length > 0) { host = args[0]; } string dbHost = "127.0.0.1"; if (args != null && args.Length > 1) { dbHost = args[1]; } ConnectionString = $"Server={dbHost};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3"; Console.WriteLine($"Server Host:{host}| DB Host:{dbHost}"); Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort(host, Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }
/// <summary> /// Main方法 /// </summary> /// <param name="args"></param> public static void RunMain(string[] args) { var options = new List <ChannelOption>() { new ChannelOption(ChannelOptions.MaxSendMessageLength, 32 * 1024 * 1024), //最大可以发送的消息长度 new ChannelOption(ChannelOptions.MaxReceiveMessageLength, 16 * 1024 * 1024), //最大允许接收的消息长度 new ChannelOption(ChannelOptions.MaxConcurrentStreams, 1024), //最大允许的并发连接 new ChannelOption(ChannelOptions.SoReuseport, 1), //重用端口 }; //Server可以服务多个services,绑定多个端口 Server server = new Server(options) { //可以注册多个service Services = { Greeter.BindService(new GreeterServiceImpl()).Intercept(new GreeterInterceptor()) /*拦截*/, }, //可以注册多个端口 //0.0.0.0监听在本机的所有IP地址 Ports = { new ServerPort(IPAddress.Any.ToString() /*0.0.0.0*/, Port, ServerCredentials.Insecure /*没有安全验证*/) }, }; //启动后后面的代码继续执行 server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadLine(); //关闭服务 server.ShutdownAsync().Wait(8000); }
public Task StartAsync(CancellationToken cancellationToken) { //构建Server var serverBuilder = new ServerBuilder(); var serverOptions = _conf.GetSection("services:GreeterServer").Get <LocalServiceOption>(); _server = serverBuilder.UseGrpcOptions(serverOptions) .UseInterceptor(_serverInterceptors) //使用中间件 .UseGrpcService(Greeter.BindService(new GreeterImpl())) .UseLogger(log => //使用日志 { log.LoggerMonitor = info => Console.WriteLine(info); log.LoggerError = exception => Console.WriteLine(exception); }) .Build(); var innerLogger = new Grpc.Core.Logging.LogLevelFilterLogger(new Grpc.Core.Logging.ConsoleLogger(), Grpc.Core.Logging.LogLevel.Debug); GrpcEnvironment.SetLogger(innerLogger); _server.UseDashBoard() //使用DashBoard,需要使用FM.GrpcDashboard网站 .StartAndRegisterService(); //启动服务并注册到consul return(Task.CompletedTask); }
private void SayHello(Button button) { Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); // use loopback on host machine: https://developer.android.com/studio/run/emulator-networking //10.0.2.2:30051 Channel channel = new Channel("localhost:30051", ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); string user = "******" + count; var reply = client.SayHello(new HelloRequest { Name = user }); button.Text = "Greeting: " + reply.Message; channel.ShutdownAsync().Wait(); server.ShutdownAsync().Wait(); count++; }
static void Main(string[] args) { var server = new NamedPipeServer("MY_PIPE_NAME"); Greeter.BindService(server.ServiceBinder, new GreeterService()); server.Start(); }
public static void Main(string[] args) { var cacert = File.ReadAllText(@"C:\Sertifika\ca.crt"); var servercert = File.ReadAllText(@"C:\Sertifika\server.crt"); var serverkey = File.ReadAllText(@"C:\Sertifika\server.key"); var keypair = new KeyCertificatePair(servercert, serverkey); var sslCredentials = new SslServerCredentials(new List <KeyCertificatePair>() { keypair }, cacert, false); Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, sslCredentials) } }; server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }
public async Task GreeterServiceTest() { //服务信息 var serviceInfo = new ServiceInformation() { Host = "127.0.0.1", Port = 1234, Description = "我的小服务, 老火了", Key = "Foo.Services", Name = "我的小服务", ServiceType = ServiceType.Grpc, }; //启动服务 Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort(serviceInfo.Host, serviceInfo.Port, ServerCredentials.Insecure) } }; server.Start(); //注册服务 using (var zon = await ServiceBus.Register(serviceInfo)) { //发现服务 string serviceKey = serviceInfo.Key; ServiceInformation service = ServiceBus.Get(serviceKey).Result.FeelingLucky(); //使用服务 string target = $"{service.Host}:{service.Port}"; Channel channel = new Channel(target, ChannelCredentials.Insecure); Greeter.GreeterClient client = new Greeter.GreeterClient(channel); HelloRequest request = new HelloRequest() { Name = "徐云金", SecretSignal = "天王盖地虎", }; request.Gifts.AddRange(new List <Gift>() { new Gift { Name = "兔子" }, new Gift { Name = "橘猫" }, }); Console.WriteLine($"request: \n{JsonConvert.SerializeObject(request)}"); HelloReply reply = client.SayHello(request); Console.WriteLine($"reply: \n{JsonConvert.SerializeObject(reply)}"); Assert.AreEqual(request.Name, reply.Name); channel.ShutdownAsync().Wait(); } }
private static void RunServer(Options options) { var hostName = options.Hostname ?? Dns.GetHostName(); var serviceDescriptors = new [] { Greeter.Descriptor, Health.Descriptor, ServerReflection.Descriptor }; var greeterImpl = new GreeterImpl(hostName); var healthServiceImpl = new HealthServiceImpl(); var reflectionImpl = new ReflectionServiceImpl(serviceDescriptors); Server server = new Server { Services = { Greeter.BindService(greeterImpl), Health.BindService(healthServiceImpl), ServerReflection.BindService(reflectionImpl) }, Ports = { new ServerPort("[::]", options.Port, ServerCredentials.Insecure) } }; server.Start(); // Mark all services as healthy. foreach (var serviceDescriptor in serviceDescriptors) { healthServiceImpl.SetStatus(serviceDescriptor.FullName, HealthCheckResponse.Types.ServingStatus.Serving); } // Mark overall server status as healthy. healthServiceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving); Console.WriteLine("Greeter server listening on port " + options.Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }
public static void Main(string[] args) { ServerCredentials serverCredentials = null; var securityOption = Environment.GetEnvironmentVariable("GREETER_SERVER_SECURITY"); switch (securityOption) { case "insecure": serverCredentials = ServerCredentials.Insecure; break; case "tls": serverCredentials = CreateSslServerCredentials(mutualTls: false); break; case "mtls": serverCredentials = CreateSslServerCredentials(mutualTls: true); break; default: throw new ArgumentException("Illegal security option."); } Console.WriteLine("Starting server with security: " + securityOption); Server server = new Server() { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("0.0.0.0", Port, serverCredentials) }, }; server.Start(); Console.WriteLine("Started server on port " + Port); server.ShutdownTask.Wait(); }
static void Main(string[] args) { // Build the server Console.WriteLine("Starting C# server on port 9002"); Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", 9002, ServerCredentials.Insecure) } }; server.Start(); // Call the Java server on port 9000 Console.WriteLine("Press enter to call the Java server..."); Console.ReadKey(); // Set up gRPC client Channel channel = new Channel("localhost:9000", ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); // Call the service var req = new HelloRequest { Name = "C#" }; var resp = client.SayHello(req); foreach (string msg in resp.Message) { Console.WriteLine(msg); } // Block for server termination Console.ReadKey(); channel.ShutdownAsync().Wait(); server.ShutdownAsync().Wait(); }
private void OnLoaded(object sender, RoutedEventArgs e) { _server = new Server { Services = { Greeter.BindService(new GreeterService()), WpfCommunication.BindService(new WpfCommunicationService()), }, Ports = { new ServerPort("localhost", 8099, ServerCredentials.Insecure) } }; _server.Start(); //var host = new ControlHost(ActualHeight, ActualWidth); //host.Visibility = Visibility.Hidden; //Content = host; //var window = new Window(); //window.Content = host; //window.Show(); //grid.Children.Add(new ControlHost()); }
public static async Task Main(string[] args) { GrpcEnvironment.SetLogger(new Grpc.Core.Logging.ConsoleLogger()); await new HostBuilder() .ConfigureLogging(logging => logging.AddSimpleConsole()) .ConfigureServices((hostContext, services) => { // register grpc service implementation services.AddSingleton <HealthImpl>(); services.AddSingleton <GreeterImpl>(); services.AddSingleton <MyVersionInfoImpl>(); var provider = services.BuildServiceProvider(); Server server = new Server { Services = { Greeter.BindService(provider.GetService <GreeterImpl>()), MyVersionInfo.BindService(provider.GetService <MyVersionInfoImpl>()), }, Ports = { new ServerPort("0.0.0.0", Port, ServerCredentials.Insecure) } }; RegisterHealthCheck(server, "Check", provider); services.AddSingleton <Server>(server); services.AddSingleton <IHostedService, GrpcHostedService>(); }) .RunConsoleAsync(); // SIGTERM, SIGKILL }
public static HelloReply Greet(string greeting) { const int Port = 50051; Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); var reply = client.SayHello(new HelloRequest { Name = greeting }); channel.ShutdownAsync().Wait(); server.ShutdownAsync().Wait(); return(reply); }
private string SayHello() { Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Channel channel = new Channel("localhost:30051", ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); string user = "******" + count; var reply = client.SayHello(new HelloRequest { Name = user }); channel.ShutdownAsync().Wait(); server.ShutdownAsync().Wait(); count++; return("Greeting: " + reply.Message); }
public static void Main(string[] args) { Console.WriteLine("gRPC Server: Process Started."); Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("gRPC Server: Greeter server listening on port " + Port); try { Notifier.NotifyReadyToClient("testpipe").Wait().Shutdown(); Console.WriteLine("gRPC Server: Pipe closed."); } catch (IOException e) { Console.WriteLine("gRPC Server: Pipe broken: " + e.Message); } catch (Exception e) { Console.WriteLine("gRPC Server: Error: " + e.Message); } Console.WriteLine("gRPC Server: Shutting down gRPC server..."); server.ShutdownAsync().Wait(); Console.WriteLine("gRPC Server: Exit process."); }
private static async Task StartService(string host, int port) { //服务信息 var serviceInfo = new ServiceInformation() { Host = host, Port = port, Description = "我的小服务, 老火了", Key = "Foo.Services", Name = "我的小服务", ServiceType = ServiceType.Grpc, }; //启动服务 Server service = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort(serviceInfo.Host, port, ServerCredentials.Insecure) } }; service.Start(); using (IZookeeperClient zon = await ServiceBus.Register(serviceInfo)) { Console.WriteLine("Greeter server listening on port " + port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); service.ShutdownAsync().Wait(); } }
/// <summary> /// Optional override to create listeners (e.g., TCP, HTTP) for this service replica to handle client or user requests. /// </summary> /// <returns>A collection of listeners.</returns> protected override IEnumerable <ServiceInstanceListener> CreateServiceInstanceListeners() { return(new [] { new ServiceInstanceListener(serviceContext => new GrpcCommunicationListener(new [] { Greeter.BindService(new GreeterService(Context)) }, serviceContext, ServiceEventSource.Current, "ServiceEndpoint")) }); }
private void OnLoaded(object sender, RoutedEventArgs e) { server = new Server() { Services = { Greeter.BindService(new GreeterService()) }, Ports = { new ServerPort("localhost", 8099, ServerCredentials.Insecure) } }; server.Start(); }
public void TestInitialize() { server = new Server { Services = { Greeter.BindService(new GreeterServer()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); }
public GRpcListener(StatelessServiceContext ctx, int port) { _ctx = ctx; _port = port; _ipAddress = FabricRuntime.GetNodeContext().IPAddressOrFQDN; _server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort(_ipAddress, port, ServerCredentials.Insecure) } }; }
public void Setup() { _server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort(TestCredentials.DefaultHostOverride, Port, ServerCredentials.Insecure), new ServerPort(TestCredentials.DefaultHostOverride, SslPort, serverCredentials) } }; _server.Start(); }
public static void Main(string[] args) { //// Work around für Dependency Injection //var collection = new ServiceCollection(); //collection.AddSingleton<ISupplierService, SupplierService>(); //var provider = collection.BuildServiceProvider(); // Testdaten laden, Listen initialisieren... var products = new BlockingCollection <ProductRequest>(); products.Add(new ProductRequest { Id = "00000000-0000-0000-0000-000000000000", Preferredsupplier = "00000000-0000-0000-0000-000000000001", Color = ProductRequest.Types.Color.Green, Price = 12.0, Name = "Produkt_1", Description = "Ich bin Produkt_1", CurrentStock = 100 }); products.Add(new ProductRequest { Id = "00000000-0000-0000-0000-000000000001", Preferredsupplier = "00000000-0000-0000-0000-000000000002", Color = ProductRequest.Types.Color.Blue, Price = 43.0, Name = "Produkt_2", Description = "Ich bin Produkt_2", CurrentStock = 50 }); products.Add(new ProductRequest { Id = "00000000-0000-0000-0000-000000000002", Preferredsupplier = "00000000-0000-0000-0000-000000000003", Color = ProductRequest.Types.Color.Red, Price = 100.0, Name = "Produkt_3", Description = "Ich bin Produkt_3", CurrentStock = 25 }); var suppliers = new BlockingCollection <PreferredSupplier>(); suppliers.Add(new PreferredSupplier { Id = "00000000-0000-0000-0000-000000000000", Name = "Alpha", Email = "*****@*****.**", Phone = "+49 89 123456 789", Address = "Muster-Adresse" }); suppliers.Add(new PreferredSupplier { Id = "00000000-0000-0000-0000-000000000001", Name = "Beta", Email = "*****@*****.**", Phone = "+49 89 123456 789", Address = "Muster-Adresse" }); suppliers.Add(new PreferredSupplier { Id = "00000000-0000-0000-0000-000000000002", Name = "Gamma", Email = "*****@*****.**", Phone = "+49 89 123456 789", Address = "Muster-Adresse" }); suppliers.Add(new PreferredSupplier { Id = "00000000-0000-0000-0000-000000000003", Name = "Omega", Email = "*****@*****.**", Phone = "+49 89 123456 789", Address = "Muster-Adresse" }); suppliers.Add(new PreferredSupplier { Id = "00000000-0000-0000-0000-000000000004", Name = "Epsylon", Email = "*****@*****.**", Phone = "+49 89 123456 789", Address = "Muster-Adresse" }); Server server = new Server { Services = { Greeter.BindService(new GreeterImpl(suppliers, products)) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); // Lists leeren suppliers.Dispose(); products.Dispose(); server.ShutdownAsync().Wait(); }
private static Server StarServer(int port) { Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) } }; server.Start(); return(server); }
public async Task StartAsync(CancellationToken cancellationToken) { _server = new Grpc.Core.Server { Services = { Greeter.BindService(_controller) }, Ports = { new ServerPort("localhost", GreetHostService.Port, ServerCredentials.Insecure) } }; _server.Start(); _logger.LogInformation("Greeter server listening on port " + GreetHostService.Port); }
public static void TestMain(String[] args) { GrpcServer server = new GrpcServer(new List <ServerServiceDefinition>() { Greeter.BindService(new GreeterServiceImpl()) }, 5142); //server.StartWithSsl(); server.Start(); Console.ReadLine(); Console.ReadLine(); }
public void Start() { server = new Server { Services = { Greeter.BindService(new LogImpl()) }, Ports = { new ServerPort(LogConfig.host, LogConfig.port, ServerCredentials.Insecure) } }; server.Start(); run = true; LogPersistence(); SendMail(); }
static async Task Main(string[] args) { Environment.SetEnvironmentVariable("GRPC_TRACE", "all,-timer,-timer_check"); Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "INFO"); GrpcEnvironment.SetLogger(new ConsoleLogger()); var server = new Server(); server.Ports.Add("127.0.0.1", InsecurePort, ServerCredentials.Insecure); server.Ports.Add("127.0.0.1", SecurePort, MakeBadSslServerCredentials()); server.Services.Add(Greeter.BindService(new GreeterImpl())); try { Console.WriteLine("Starting...."); server.Start(); } catch (IOException ex) { Console.WriteLine($"Caught {ex}"); } foreach (var p in server.Ports) { Console.WriteLine($"{p.Host} @ {p.Port} bound to {p.BoundPort}"); } // Prior to 2.32.0, one needed to uncomment the following line // to clean up the one bound port & see the expected // RpcException. With 2.32.0, the behavior is what would be // expected. //await server.ShutdownAsync(); var channel = new Channel("127.0.0.1", InsecurePort, ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); try { Console.WriteLine("Making call. Ideally we'll get a RpcException with a connection failure."); var call = client.SayHelloAsync(new HelloRequest { Name = "This is the outgoing request name." }); HelloReply reply = await call; Console.WriteLine($"Got: {reply.Message}"); } catch (RpcException ex) { Console.WriteLine($"Client RPC exception: {ex}"); } }
public static void Main() { var server = new Server { Services = { Greeter.BindService(new GreeterServer()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.ReadKey(); server.ShutdownAsync().Wait(); }
public static IWebHostBuilder UseGrpc <T>(this IWebHostBuilder hostBuilder) where T : Greeter.GreeterBase { return(hostBuilder.ConfigureServices(services => { services.AddSingleton <IServer, GrpcServer>(provider => { var serverOptions = provider.GetService <IOptions <ServiceOptions> >().Value; var contract = provider.GetService <T>(); var serviceDifinition = Greeter.BindService(contract); return new GrpcServer(serverOptions.Host, serverOptions.Port, serviceDifinition); }); })); }