如何使用gRPC工具生成代码

use*_*989 7 c# protocol-buffers grpc

我已经阅读了本教程,并且能够生成.cs文件,但其中不包含任何服务或rpc定义。

我已经protoc从项目目录中添加到PATH中。

protoc project1.proto --csharp_out="C:\output" --plugin=protoc-gen-grpc="c:\Users\me\.nuget\packages\grpc.tools\1.8.0\tools\windows_x64\grpc_csharp_plugin.exe"

控制台中无错误输出

Jon*_*eet 9

您需要添加--grpc_out命令行选项,例如add

--grpc_out="C:\output\"
Run Code Online (Sandbox Code Playgroud)

请注意,如果您没有任何服务,它将不会写入任何文件。

这是一个完整的例子。在根目录中,创建:

  • output目录
  • tools具有protoc.exe和的目录grpc_csharp_plugin.exe
  • protos与目录test.proto,如下所示:

test.proto

syntax = "proto3";

service StackOverflowService {
  rpc GetAnswer(Question) returns (Answer);
}

message Question {
  string text = 1;
  string user = 2;
  repeated string tags = 3;
}

message Answer {
  string text = 1;
  string user = 2;
}
Run Code Online (Sandbox Code Playgroud)

然后运行(全部一行;为了便于阅读,我将其破坏了):

tools\protoc.exe -I protos protos\test.proto --csharp_out=output
    --grpc_out=output --plugin=protoc-gen-grpc=tools\grpc_csharp_plugin.exe 
Run Code Online (Sandbox Code Playgroud)

output目录中,你会发现Test.csTestGrpc.cs

  • @user3953989:我自己刚刚尝试过,没问题。您确定没有组合两个命令行参数吗?您使用的完整命令行是什么? (2认同)

Dou*_*oug 5

对于其他发现此问题的人,这里只是一个闲聊的评论,有关此问题的文档非常过时,而且完全是错误的。

安装Grpc.Tools不会在packages文件夹中安装任何内容。那是遗留的行为,即使在Windows上也不再适用。

安装时Grpc.Tools,它将隐藏在本地程序包缓存中,您可以通过调用以下代码来查看:

$ dotnet nuget locals all --list
info : http-cache: /Users/doug/.local/share/NuGet/v3-cache
info : global-packages: /Users/doug/.nuget/packages/
info : temp: /var/folders/xx/s2hnzbrj3yn4hp1bg8q9gb_m0000gn/T/NuGetScratch
Run Code Online (Sandbox Code Playgroud)

所需的二进制文件将位于这些文件夹之一中。

最简单的方法是Grpc.Tools直接从nuget 下载软件包,然后在本地安装。

我已经破解了这个小的帮助程序脚本来执行此操作,该脚本可以在Windows / Mac / Linux上运行,这可以为其他人减轻入门难度:

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Mono.Unix;

namespace BuildProtocol
{
  public class Program
  {
    private const string ToolsUrl = "https://www.nuget.org/api/v2/package/Grpc.Tools/";
    private const string Service = "Greeter";
    private static string ProtocolPath = Path.Combine("..", "protos");
    private static string Protocol = Path.Combine(ProtocolPath, "helloworld.proto");
    private static string Output = Path.Combine("..", "Greeter");

    public static void Main(string[] args)
    {
      RequireTools().Wait();

      var protoc = ProtocPath();
      var plugin = ProtocPluginPath();

      Console.WriteLine($"Using: {protoc}");
      Console.WriteLine($"Using: {plugin}");

      var command = new string[]
      {
        $"-I{ProtocolPath}",
        $"--csharp_out={Output}",
        $"--grpc_out={Output}",
        $"--plugin=protoc-gen-grpc=\"{plugin}\"",
        Protocol,
      };

      Console.WriteLine($"Exec: {protoc} {string.Join(' ', command)}");

      var process = new Process
      {
        StartInfo = new ProcessStartInfo
        {
          UseShellExecute = false,
          FileName = protoc,
          Arguments = string.Join(' ', command)
        }
      };

      process.Start();
      process.WaitForExit();

      Console.WriteLine($"Completed status: {process.ExitCode}");
    }

    public static async Task RequireTools()
    {
      if (!Directory.Exists("Tools"))
      {
        Console.WriteLine("No local tools found, downloading binaries from nuget...");
        Directory.CreateDirectory("Tools");
        await DownloadTools();
        ExtractTools();
      }
    }

    private static void ExtractTools()
    {
      ZipFile.ExtractToDirectory(Path.Combine("Tools", "tools.zip"), Path.Combine("Tools", "bin"));
    }

    private static async Task DownloadTools()
    {
      using (var client = new HttpClient())
      {
        Console.WriteLine($"Fetching: {ToolsUrl}");
        using (var result = await client.GetAsync(ToolsUrl))
        {
          if (!result.IsSuccessStatusCode) throw new Exception($"Unable to download tools ({result.StatusCode}), check URL");
          var localArchive = Path.Combine("Tools", "tools.zip");
          Console.WriteLine($"Saving to: {localArchive}");
          File.WriteAllBytes(localArchive, await result.Content.ReadAsByteArrayAsync());
        }
      }
    }

    private static string ProtocPath()
    {
      var path = Path.Combine("Tools", "bin", "tools", DetermineArch(), "protoc");
      RequireExecutablePermission(path);
      return WithExeExtensionIfRequired(path);
    }

    private static string ProtocPluginPath()
    {
      var path = Path.Combine("Tools", "bin", "tools", DetermineArch(), "grpc_csharp_plugin");
      RequireExecutablePermission(path);
      return WithExeExtensionIfRequired(path);
    }

    private static void RequireExecutablePermission(string path)
    {
      if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;
      Console.WriteLine($"Ensuring +x on {path}");
      var unixFileInfo = new UnixFileInfo(path);
      unixFileInfo.FileAccessPermissions = FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite | FileAccessPermissions.UserExecute;
    }

    private static string WithExeExtensionIfRequired(string path)
    {
      if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
      {
        path += ".exe";
      }

      return path;
    }

    private static string DetermineArch()
    {
      var arch = RuntimeInformation.OSArchitecture;
      if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
      {
        return WithArch("windows_", arch);
      }

      if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
      {
        return WithArch("macosx_", arch);
      }

      if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
      {
        return WithArch("linux_", arch);
      }

      throw new Exception("Unable to determine runtime");
    }

    private static string WithArch(string platform, Architecture arch)
    {
      switch (arch)
      {
        case Architecture.X64:
          return $"{platform}x86";
        case Architecture.X86:
          return $"{platform}x64";
        default:
          throw new ArgumentOutOfRangeException(nameof(arch), arch, null);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • `nuget` 不是 .Net Core SDK 的一部分,在其他平台上安装有问题。安装包的标准方法是`dotnet add package`,这是大多数IDE现在所做的(VS取决于项目类型)。你说的是对的,但它并没有改变它是遗留行为的事实(类似于 msbuild 路径的硬编码版本,而不是使用 `dotnet msbuild` 来查找 msbuild 的“正确”安装副本)。 (2认同)
  • @DaisyShipton我不确定该说些什么。如果我的语气不合适,很抱歉,但是教程说明有误,并且不起作用。如果改变了,我会很高兴地更新此答案,但现在就这样。 (2认同)
  • gRPC C#快速入门确实确实过时了。我创建了https://github.com/grpc/grpc.github.io/pull/728来解决此问题。它包含有关使用dotnet SDK时如何进行代码生成的更新说明。 (2认同)