Protobuf Enumerations
Explore how to define and use enumerations in Protobuf to categorize data for gRPC services in ASP.NET Core. Understand enum basics, the allow_alias option, and how to implement enums in C# client and server applications for smoother communication.
We'll cover the following...
Any C# developer will be familiar with enum types, which represent categorical data. Protobuf has an equivalent, which we'll look at in this lesson.
Adding enum to proto files
We'll use the code widget below to add the enum-related functionality to the gRPC client and serevr applications we built previously:
syntax = "proto3";
option csharp_namespace = "BasicGrpcService";
package basic_grpc_service;
service Chatbot {
rpc SendMessage (ChatRequest) returns (ChatReply);
}
message ChatRequest {
string name = 1;
string message = 2;
}
message ChatReply {
string message = 1;
bool answer_found = 2;
bytes reply_in_bytes = 3;
map <int32, ChatHistoryEntry> message_history = 4;
}
message ChatHistoryEntry
{
string request_message = 1;
string response_message = 2;
}The first thing we'll do is open the chatbot.proto file inside the BasicGrpcService project, and modify its content as follows, where the highlighted lines show the modifications:
In this example, we have two enum definitions: AnswerType on line 39 and ResponseType on line 29. The AnswerType definition is an enum defined in the same scope as the service and the messages. The ResponseType definition, on the other hand, has been defined inside the ...