简体   繁体   English

为所有解决方案共享一个 gRPC proto 文件

[英]Sharing one gRPC proto file for all solutions

I've recently got my hands on gRPC on .net core and so far I'm very pleased with it..我最近在 .net 核心上接触了 gRPC,到目前为止我对它非常满意。

the only problem I have is the proto files, for example: If I make a change on MyProtos.proto file in my grpc server solution.我唯一的问题是 proto 文件,例如:如果我在我的 grpc 服务器解决方案中对 MyProtos.proto 文件进行更改。 i'll have to update MyProtos.proto files in all my client solutions..我将不得不更新我所有客户端解决方案中的 MyProtos.proto 文件..

so I was wondering if there are ways of sharing the proto files..所以我想知道是否有共享原始文件的方法..

I've tried creating a separate solution and placing the proto files there then reference it to all other solutions but couldn't make it work.我尝试创建一个单独的解决方案并将原型文件放在那里,然后将其引用到所有其他解决方案,但无法使其工作。

You can distribute proto files with nuget package.您可以使用 nuget package 分发 proto 文件。 Use .nuspec file to pack the files.使用.nuspec文件打包文件。 For example if you *.proto files are under proto folder My.Server.Proto.nuspec can look like this:例如,如果您的*.proto文件位于proto文件夹下, My.Server.Proto.nuspec可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>My.Server.Proto</id>
    <version>1.0.0</version>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>My Server Proto Files</description>
    <authors>My Company Ltd.</authors>
  </metadata>
  <files>
    <file src="proto/**/*.proto" />
  </files>
</package>

Then in the project where you want to consume the files install grpc dependencies然后在要使用文件的项目中安装 grpc 依赖项

<ItemGroup>
  <PackageReference Include="Google.Protobuf" Version="3.14.0" />
  <PackageReference Include="Grpc.Tools" Version="2.34.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
</ItemGroup>

Install your proto package:安装您的原型 package:

<ItemGroup>
  <PackageReference Include="My.Server.Proto" Version="1.0.0" GeneratePathProperty="true" />
</ItemGroup>

Note the GeneratePathProperty="true" .注意GeneratePathProperty="true" This will allow you to refer to nuget install folder.这将允许您参考 nuget 安装文件夹。 Now add Protobuf items现在添加Protobuf项目

<ItemGroup>
  <Protobuf Include="$(PkgMy_Server_Proto)/proto/**/*.proto" ProtoRoot="$(PkgMy_Server_Proto)" GrpcServices="Client" />
</ItemGroup>

The $(PkgMy_Server_Proto) variable will be resolved to My.Server.Proto nuget folder. $(PkgMy_Server_Proto)变量将解析为My.Server.Proto nuget 文件夹。 The variable name starts with Pkg and followed by package name when .变量名称以Pkg开头,后跟 package 名称 when . replaced by _ .替换为_

Using Connected Services can easily configure the proto reference of other projects, and you can delete the link in the project file.使用 Connected Services 可以轻松配置其他项目的 proto 引用,并且可以删除项目文件中的链接。 However, this can only refer to the proto of the local file, not the proto of the project.不过这个只能指本地文件的proto,不能指项目的proto。 Of course, there is nothing for the relative path of the same solution the difference You can also quote directly like this, the two have the same effect当然对于同解的相对路径没有什么区别也可以这样直接引用,两者效果一样

<ItemGroup>
  <Protobuf Include="..\Shared\Protos\greet.proto" GrpcServices="Server"/>
</ItemGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM