AspNetCore.Docs
AspNetCore.Docs copied to clipboard
Location of proto files in project directory structure
This one took me a while to figure out.
Under the steps for Usage at point 3. there should be some details on where in the project directory to place the files. i.e. in our projects we usually have a directory called "Protos" where we place all of our protos that we have created, so in the first instance I created the sub folders /google/api/
and placed the two *.protos in that directory. When this did not work, the error message that you get in VS 2022 17.4.0 Preview 2.1 is a little cryptic but hints that the compile was unable to locate the file. Then I attempted to add the proto to the project via the "Connected Services" and generate the Server code for them.
Build started...
1>------ Build started: Project: Project.Endpoint, Configuration: Debug Any CPU ------
1>C:\Program Files\dotnet\sdk\7.0.100-rc.1.22431.12\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(219,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
1>google/api/annotations.proto : error : File not found.
1>Proto/greeter.proto(8,1): error : Import "google/api/annotations.proto" was not found or had errors.
1>Done building project "Project.Endpoint.csproj" -- FAILED.
Originally I had the folder structure look like this:
│─── Project
│ ├─── Project.DatabaseAccessLayer
│ │ ├─── Entities
│ │ ├─── Migrations
│ ├─── Project.Endpoint
│ │ ├─── DataTransferObjects
│ │ ├─── Proto
│ │ │ ├─── google
│ │ │ │ └─── api
│ │ ├─── Services
Looking at examples online I found that there were multiple places that were being used, I eventually found that moving the /google/api
into a higher level director worked so that the folder layout for the repo looked something like this:
│─── Project
│ ├─── Project.DatabaseAccessLayer
│ │ ├─── Entities
│ │ ├─── Migrations
│ ├─── Project.Endpoint
│ │ ├─── DataTransferObjects
│ │ ├─── google
│ │ │ └─── api
│ │ ├─── Proto
│ │ ├─── Services
So others do not have the same problem as I did, the steps could be modified to something such as:
- Add a package reference to Microsoft.AspNetCore.Grpc.JsonTranscoding.
- Register transcoding in server startup code by adding AddJsonTranscoding. For example, services.AddGrpc().AddJsonTranscoding().
- Create the directory structure
/google/api
at the same level that your *.csproj is. - Add google/api/http.proto and google/api/annotations.proto files to your new directory.
- Annotate gRPC methods in your .proto files with HTTP bindings and routes:
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 25375ca1-0ce8-a3ca-0c3a-446328f09ba1
- Version Independent ID: 25375ca1-0ce8-a3ca-0c3a-446328f09ba1
- Content: gRPC JSON transcoding in ASP.NET Core gRPC apps
- Content Source: aspnetcore/grpc/json-transcoding.md
- Product: aspnet-core
- Technology: aspnetcore-grpc
- GitHub Login: @JamesNK
- Microsoft Alias: jamesnk
Thanks @skywalkerisnull for filing this issue and putting some thought into what would better. I agree we could clarify here.