public static void Main(string[] args) { Server server = new Server { Services = { GrpcService.BindService(new GrpcImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("GrpcService server listening on port " + Port); Console.WriteLine("任意键退出..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }
/// <summary> /// 构造方法 /// </summary> /// <param name="requestHandler">请求消息处理方法</param> /// <param name="port">监听端口</param> /// <param name="host">监听地址</param> /// <param name="options">设置项,如果为空,则自动设置最大发送和接收数据大小为2147483647字节</param> internal GrpcServer(IGrpcMessageHandler messageHandler, int port, string host = "0.0.0.0", List <ChannelOption> options = null) { if (messageHandler == null) { throw new Exception("GrpcMessageHandler is null"); } this.serviceMessageHandler = new GrpcServiceMessageHandler(messageHandler); if (options == null) { options = new List <ChannelOption>(); options.Add(new ChannelOption(ChannelOptions.MaxSendMessageLength, int.MaxValue)); options.Add(new ChannelOption(ChannelOptions.MaxReceiveMessageLength, int.MaxValue)); } this.grpcServer = new Server(options) { Services = { GrpcService.BindService(this.serviceMessageHandler) }, Ports = { new ServerPort(host, port, ServerCredentials.Insecure) } }; }