C#中使用gRPC

由于有nuget,使得c#在配置项目时非常简单。

1. 在NuGet中添加ProtocolBuffer和gRPC引用

protocol buffer 3.0版本,在NuGet插件界面选择Include Prerelease,查找google protocol buffer。
如果不选择include rerelease,查找到的protocol buffer是2.4的,无法编译

2. 定义proto

设计proto协议文件,包括服务协议和数据。gRPC必须使用protocol buffer3.0版本,所以syntax设置为proto3。
Greeter是服务名称
HelloRequest是请求数据
HelloReply是回复数据

syntax = "proto3";option java_multiple_files = true;option java_package = "io.grpc.examples.helloworld";option java_outer_classname = "HelloWorldProto";option objc_class_prefix = "HLW";package helloworld;// The greeting service definition.service Greeter {  // Sends a greeting  rpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest {  string name = 1;}// The response message containing the greetingsmessage HelloReply {  string message = 1;}

登录后复制

3. 生成proto访问类

定义proto文件后,通过protocol buffer3.0提供的protoc.exe工具生成访问类。这里使用gRPC定义的protoc的C#插件grpc_csharp_plugin.exe,而不是使用protoGen.exe。
将以下几个文件放在同一个文件夹中:

grpc_csharp_plugin.exehelloworld.protoprotoc.exe

登录后复制

创建一个bat文件,编写如下命令行:

protoc.exe -I=. --csharp_out=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe helloworld.proto

登录后复制

执行bat文件,得到proto的访问类:

helloworld.cshelloworldGrpc.cs

登录后复制

4. 创建C#项目

将两个访问类文件添加到C#项目中,将gRPC的C# example拷贝到Program.cs中,编译通过。

以上就是C#中使用gRPC的内容,更多相关内容请关注PHP中文网(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2551544.html

(0)
上一篇 2025年3月6日 06:12:27
下一篇 2025年2月22日 18:48:19

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • C#理解泛型

    简介 Visual C# 2.0 的一个最受期待的(或许也是最让人畏惧)的一个特性就是对于泛型的支持。这篇文章将告诉你泛型用来解决什么样的问题,以及如何使用它们来提高你的代码质量,还有你不必恐惧泛型的原因。 泛型是什么? 很多人觉得泛型很难…

    编程技术 2025年3月6日
    200
  • C#基础之yield与Singleton

    1.实例解析yiled的作用 最近参加java笔试题第一次见到yield这个关键字,既然遇见了那肯定要掌握,下面是c#中关于yield关键字的总结。yield这个关键字作用于迭代器块中,其最本质的功能有2个:一是“依次”向枚举对象提供值,二…

    2025年3月6日
    200
  • C# 继承

    继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类来定义一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。 当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计…

    编程技术 2025年3月6日
    200
  • C# 多线程经典示例 吃苹果

    本文主要讲述了多线程开发中经典示例,通过本示例,可以加深对多线程的理解。 示例概述:   下面用一个模拟吃苹果的实例,说明C#中多线程的实现方法。要求开发一个程序实现如下情况:一个家庭有三个孩子,爸爸妈妈不断削苹果往盘子里面放,老大、老二、…

    2025年3月6日
    200
  • C#希尔排序

    c#希尔排序 using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  namespace Sort  {      …

    2025年3月6日 编程技术
    200
  • C# 选择排序

    c# 选择排序 using System;    using System.Collections.Generic;  using System.Linq;  using System.Text;  namespace Sort  {   …

    2025年3月6日
    200
  • C# 快速排序

    c# 快速排序 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Sort{    class Quick…

    2025年3月6日
    200
  • C# 归并排序

     c# 归并排序 using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  namespace Sort  {    …

    2025年3月6日
    200
  • C# 插入排序

    c#  ,插入排序 using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  namespace Sort  {   …

    2025年3月6日
    200
  • C# 堆排序

    c#  堆排序 using System;  using System.Collections;  namespace Sort  {      public class HeapSorter      {          public …

    编程技术 2025年3月6日
    200

发表回复

登录后才能评论