In a grpc service project, when I add a new item/file, I can select the file type as "Protocol Buffer File" and the "Build Action" (in file property) is "Protobuf compiler"
But when I have a non-grpc service project (say a class lib), there is no "Protocol Buffer File" type I can choose when I add a new file. Even I create a file with .proto ext, there is no "Protobuf compiler" in "Build Action" I can select.
Is there any way I can add a proto file in a non-grpc service project or I can only edit the project .csproj manually (I mean copy the xml node about the proto files from the grpc service project to a non-grpc service project)?
What you want to do is possible.
The "Protobuf compiler" option you mentioned comes from the Grpc.Tools Nuget Package, which uses the protobuf compiler protoc to generate C# code. When you build a project and you want to generate code-behind files from the protobuf files during the build, you either need this package or a metapackage like Grpc.AspNetCore, which holds a reference to it in the project. During the build, protoc then builds the code-behind files if you have a "Protobuf compiler" option set. Like you said, in the default "ASP.NET Core gRPC Service" project you have the "Protobuf compiler" option, that is because it has the Grpc.AspNetCore nuget package installed by default. EDIT: as mentioned by kzfid, the package Google.Protobuf is needed to compile the C# code that has been generated by protoc.
So, in short, what you have to do is: 1. Add the necessary nuget packages in the project where you want the "Protobuf compiler" options and 2. If the protoc files are in a different project than the project where you want to generate C# code, you have to add a project reference
1. Install required Nuget Packages
In any project where you want to create the code-behind c# files from the protoc files (and have the "Protobuf compiler" option for protoc files), you should install the following nuget packages:
Grpc.Tools
Google.Protobuf
or a package that references them like Grpc.AspNetCore. By adding these packages you will have the "Protobuf compiler" option for all proto files, even when they are referenced from a different project, like a shared class library.
2. Add a project reference for proto files
If you have a project where you want to create the code-behind files for proto files, but the *.proto files are in a different project, you have to add a reference. For example, if there is a connections.proto file in a namespace called Protos in a shared class library called Shared, and I want to use the generated code in a client project called Client, I have to install the nuget packages mentioned above in the client project, add a project reference in the client project for the shared class library and add the following in the Client.csproj:
<ItemGroup>
<Protobuf Include="..\Shared\Protos\connections.proto" GrpcServices="Client">
<Link>Protos\connections.proto</Link>
</Protobuf>
</ItemGroup>
Now there is the connections.proto file referenced in the client project and the "Protobuf compiler" options for it, but the file itself is located in a different project (the shared class library).