Rider 2022.3.1无法编译Protobuf文件,该如何解决?
解决Protobuf文件未编译导致命名空间无法识别的问题
你在评估Rider 2022.3.1时创建了.NET 6类库项目,添加了如下Test.proto定义:
syntax = "proto3"; package Generated; message Fnord { string Foo = 1; int32 Bar = 2; }
并手动修改了项目文件:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Google.Protobuf" Version="3.21.12" /> </ItemGroup> <ItemGroup> <Protobuf Include="Test.proto" /> </ItemGroup> </Project>
构建无报错但Generated命名空间无法被识别,核心问题是缺少编译Protobuf文件的工具链,解决步骤如下:
添加Protobuf编译工具包
Google.Protobuf仅提供运行时依赖,无法编译.proto文件。需要添加Grpc.Tools包来获得编译工具,修改项目文件的PackageReference节点:<ItemGroup> <PackageReference Include="Google.Protobuf" Version="3.21.12" /> <PackageReference Include="Grpc.Tools" Version="2.51.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup>版本建议与
Google.Protobuf匹配,比如3.21.x版本的Google.Protobuf搭配2.51.x版本的Grpc.Tools。明确Protobuf生成配置(可选)
若添加工具包后仍有问题,可给<Protobuf>节点指定生成类型,确保仅生成消息类(无gRPC服务):<ItemGroup> <Protobuf Include="Test.proto" GrpcServices="None" /> </ItemGroup>清理并重新构建项目
在Rider中执行Build -> Clean Solution清理旧的构建缓存,再重新构建项目。编译后的C#代码默认生成在obj/Debug/net6.0/Generated目录下,此时Generated命名空间即可被正常识别。
内容的提问来源于stack exchange,提问作者Kevin Krumwiede
相关产品推荐
相关产品推荐

