Wpf的gRpc的Server/Client

Posted linxmouse

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
..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
  • 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);
        }
    }
}

以上是关于Wpf的gRpc的Server/Client的主要内容,如果未能解决你的问题,请参考以下文章

WPF .NET Core 中的 gRPC 错误

gRPC接口测试详解

gRPC的简介与实例详解

如何高效应对突发的流量洪峰?gRPC能派上大用场!

分布式服务框架gRPC,被低估了,还是被神话了?

高质量通信gRPC入门,有了它,谁还用Socket