public PostAccessHostedService(IBlogService blogService, IOptions <BlogOptions> options,
                                ILogger <PostAccessHostedService> logger)
 {
     _logger          = logger;
     _blogService     = blogService;
     _blogGrpcService = GrpcClientHelper.CreateClient <IBlogGrpcService>(options.Value.ApiLocalEndpoint);
 }
示例#2
0
    private async Task SendLogsAsync()
    {
        var logs = new List <LaobianLog>();

        while (_logQueue.TryDequeue(out var log))
        {
            logs.Add(log);
        }

        if (logs.Any())
        {
            try
            {
                var client  = GrpcClientHelper.CreateClient <ILogGrpcService>(_options.ApiLocalEndpoint);
                var request = new LogGrpcRequest {
                    Logger = LaobianSite.Admin.ToString(), Logs = logs
                };
                await client.AddLogsAsync(request);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Sent logs failed. {ex}");
                foreach (var laobianLog in logs)
                {
                    Console.WriteLine(laobianLog);
                }
            }
        }
    }
 public NoteController(IOptions <AdminOptions> option, ILogger <NoteController> logger)
 {
     _logger          = logger;
     _options         = option.Value;
     _request         = new NoteGrpcRequest();
     _noteGrpcService = GrpcClientHelper.CreateClient <INoteGrpcService>(_options.ApiLocalEndpoint);
 }
 public HomeController(ILogger <HomeController> logger, IOptions <AdminOptions> options,
                       IHostApplicationLifetime appLifetime)
 {
     _logger          = logger;
     _appLifetime     = appLifetime;
     _miscGrpcService = GrpcClientHelper.CreateClient <IMiscGrpcService>(options.Value.ApiLocalEndpoint);
 }
示例#5
0
 public BlogController(
     IOptions <AdminOptions> options, ILogger <BlogController> logger)
 {
     _logger          = logger;
     _options         = options.Value;
     _blogGrpcRequest = new BlogGrpcRequest();
     _blogGrpcService = GrpcClientHelper.CreateClient <IBlogGrpcService>(options.Value.ApiLocalEndpoint);
 }
示例#6
0
 public BlogService(IOptions <BlogOptions> options, ILogger <BlogService> logger)
 {
     _logger          = logger;
     BootTime         = DateTime.Now;
     _blogGrpcService = GrpcClientHelper.CreateClient <IBlogGrpcService>(options.Value.ApiLocalEndpoint);
     _readGrpcService = GrpcClientHelper.CreateClient <IReadGrpcService>(options.Value.ApiLocalEndpoint);
     _allTags         = new List <BlogTag>();
     _allReadItems    = new List <ReadItemRuntime>();
     _allPosts        = new List <BlogPostRuntime>();
     _postAccessQueue = new ConcurrentQueue <string>();
     _reloadLock      = new ManualResetEventSlim(true);
 }
 public DiaryController(IOptions <JarvisOptions> option, ILogger <DiaryController> logger)
 {
     _logger           = logger;
     _options          = option.Value;
     _diaryGrpcService = GrpcClientHelper.CreateClient <IDiaryGrpcService>(option.Value.ApiLocalEndpoint);
 }
 public RemoteLogHostedService(ILaobianLogQueue logQueue, IOptions <BlogOptions> options)
 {
     _logQueue       = logQueue;
     _logGrpcService = GrpcClientHelper.CreateClient <ILogGrpcService>(options.Value.ApiLocalEndpoint);
 }
 public ReadController(IOptions <AdminOptions> options, ILogger <ReadController> logger)
 {
     _logger          = logger;
     _readGrpcService = GrpcClientHelper.CreateClient <IReadGrpcService>(options.Value.ApiLocalEndpoint);
 }
        public static async void TestMain(string[] args)
        {
            //var channelCredentials = new SslCredentials(File.ReadAllText("roots.pem"));
            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            var     client  = new Greeter.GreeterClient(channel);

            #region 拦截器

            GrpcClientHelper.SafeInvoke("127.0.0.1", 50051, channel2 =>
            {
                //拦截器
                CallInvoker callInvoker = channel2.Intercept(new CommonClientInterceptor((str) =>
                {
                    NLogHelper.Info($"调用{str}");
                },
                                                                                         (str, b, ex, timeSpan) =>
                {
                    NLogHelper.Info($"调用{str}{(b?"成功":$"失败:{ex}")},耗时:{timeSpan}");
                }));
                Greeter.GreeterClient client2 = new Greeter.GreeterClient(callInvoker);
                var respone = client.SayHello(new Request());
                respone     = client2.SayHello(new Request());
                respone     = client2.SayHello(new Request());
                respone     = client2.SayHello(new Request());
                respone     = client2.SayHello(new Request());
                Console.WriteLine(respone.Response_);
            });


            #endregion


            #region Unary RPC

            for (int i = 0; i < 10; i++)
            {
                //同步调用,只用unary rpc有同步调用,其它三种全是异步
                var reply = client.SayHello(new Request()
                {
                    Request_ = "Hello"
                });
                Console.WriteLine(reply.Response_);
                //异步调用
                Response response = await client.SayHelloAsync(new Request());
            }

            #endregion

            #region Server streaming RPC

            //同步调用,大的数据分多次传输
            using (var serverStreamCall = client.SayHelloStreamServer(new Request()
            {
                Request_ = ""
            }))
            {
                var responseStream = serverStreamCall.ResponseStream;
                int i = 0;
                while (await responseStream.MoveNext())
                {
                    //读取当前返回值
                    Response response = responseStream.Current;
                    //TODO:处理响应

                    if (i++ % 100 == 0)
                    {
                        Console.WriteLine(response.Response_);
                    }
                }
            }

            #endregion

            #region Client streaming RPC

            using (var call = client.SayHelloStreamClient())
            {
                List <Request> list = new List <Request>();
                list.Add(new Request()
                {
                    Request_ = "1"
                });
                list.Add(new Request()
                {
                    Request_ = "2"
                });
                list.Add(new Request()
                {
                    Request_ = "3"
                });
                foreach (var request in list)
                {
                    //发送多个消息
                    await call.RequestStream.WriteAsync(request);
                }

                //通知服务器写完成,没有更多的写操作
                await call.RequestStream.CompleteAsync();

                //读取响应
                Response response = await call.ResponseAsync;
            }

            #endregion

            #region Bi-directional streaming

            using (var call = client.SayHelloStreamBidirectional())
            {
                //读和写可以分开线程执行,但读本身写本身不能多线程
                //在新线程开启读任务
                var readTask = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        //读取一个响应
                        var response = call.ResponseStream.Current;
                        //TODO: 处理响应
                    }
                });

                List <Request> list = new List <Request>();
                list.Add(new Request()
                {
                    Request_ = "1"
                });
                list.Add(new Request()
                {
                    Request_ = "2"
                });
                list.Add(new Request()
                {
                    Request_ = "3"
                });
                foreach (var request in list)
                {
                    //发送多个消息
                    await call.RequestStream.WriteAsync(request);
                }

                //通知服务器写完成,没有更多的写操作
                await call.RequestStream.CompleteAsync();

                //等待读线程结束
                await readTask;
            }

            #endregion


            //关闭信道
            channel.ShutdownAsync().Wait(5000);
        }