zoukankan      html  css  js  c++  java
  • Wpf的gRpc的Server/Client

    • 创建Wpf库项目Hello、Wpf项目HelloServer、Wpf项目HelloClient

    • 库项目Hello中引入Nuget

    Grpc、Grpc.Tools

    • Wpf项目Client、Server分别引入Nuget

    Grpc

    • 编写proto
    syntax = "proto3";
    
    option csharp_namespace = "greeter.hello";
    
    service Greeter{
        rpc SayHello (HelloRequest) returns (HelloResponse);
        rpc SayWorld (HelloRequest) returns (HelloResponse);
    }
    
    message HelloRequest{
        string name = 1;
    }
    
    message HelloResponse{
        string message = 1;
    }
    
    • 使用命令行方式编译proto、rpc
    ..packagesGrpc.Tools.2.27.0	oolswindows_x64protoc.exe -I.Proto --csharp_out ./ --grpc_out ./ --plugin=protoc-gen-grpc=..packagesGrpc.Tools.2.27.0	oolswindows_x64grpc_csharp_plugin.exe .Proto
    outine.proto
    
    • 使用Grpc.Tools在vs配置编译选项编译groto、rpc

      proto文件的属性, Build Action、gRpc Stub Classes(server and client/server only/client only)、Class Access, 此属性面板的功能由Nuget包Grpc.Tools提供

    • Server实现gRpcService

    using System.Threading.Tasks;
    using greeter.hello;
    using Grpc.Core;
    
    namespace HelloServer.gRpcService
    {
        class GreeterService: Greeter.GreeterBase
        {
            public override Task<HelloResponse> SayHello(HelloRequest request, ServerCallContext context)
            {
                return Task.FromResult(new HelloResponse() {Message = "server responsed: " + "hello " + request.Name});
            }
    
            public override Task<HelloResponse> SayWorld(HelloRequest request, ServerCallContext context)
            {
                return Task.FromResult(new HelloResponse() {Message = "server responsed: " + request.Name + " world"});
            }
        }
    }
    
    • 创建服务端
    using System.ComponentModel;
    using System.Windows;
    using greeter.hello;
    using Grpc.Core;
    using HelloServer.gRpcService;
    
    namespace HelloServer
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            private Server server;
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.Loaded += OnLoaded;
            }
    
            private void OnLoaded(object sender, RoutedEventArgs e)
            {
                server = new Server()
                {
                    Services = { Greeter.BindService(new GreeterService())},
                    Ports = { new ServerPort("localhost", 8099, ServerCredentials.Insecure)}
                };
                server.Start();
            }
    
            protected override void OnClosing(CancelEventArgs e)
            {
                server?.ShutdownAsync().Wait();
                base.OnClosing(e);
            }
        }
    }
    
    • 创建客户端
    using System.ComponentModel;
    using System.Windows;
    using greeter.hello;
    using Grpc.Core;
    
    namespace HelloClient
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            private Channel channel;
            private Greeter.GreeterClient client;
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.Loaded += OnLoaded;
            }
    
            private void OnLoaded(object sender, RoutedEventArgs e)
            {
                channel = new Channel("localhost", 8099, ChannelCredentials.Insecure);
                client = new Greeter.GreeterClient(channel);
                var response = client.SayHello(new HelloRequest() { Name = "Link Sun" });
                lvResponse.Items.Add(response);
            }
    
            private void TbHello_OnClick(object sender, RoutedEventArgs e)
            {
                string name = tbName.Text;
                var response = client.SayHello(new HelloRequest() { Name = name });
                lvResponse.Items.Add(response);
            }
    
            private void TbWorld_OnClick(object sender, RoutedEventArgs e)
            {
                string name = tbName.Text;
                var response = client.SayWorld(new HelloRequest() { Name = name });
                lvResponse.Items.Add(response);
            }
    
            protected override void OnClosing(CancelEventArgs e)
            {
                channel?.ShutdownAsync().Wait();
                base.OnClosing(e);
            }
        }
    }
    
  • 相关阅读:
    c# 类名不同,字段相同,如何快速给类赋值
    .net 中 Json 与List 相互转
    echarts Hello world 入门
    Could not load file or assembly 'Oracle.ManagedDataAccessDTC.DLL' or one of its dependencies.
    省厅报件7.0 读取mdb 生成xml 文件
    【0 基础学Dojo】第【1】篇 HelloWord
    UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in position 9865: illegal multibyte sequence 解决办法
    算法的稳定性(Stability of Sorting Algorithms)
    选择排序(Selection Sort)
    快速排序(Quick Sort)
  • 原文地址:https://www.cnblogs.com/linxmouse/p/12396197.html
Copyright © 2011-2022 走看看