I'm developing an application using a plugins architecture and I want to send objects between client and server without knowing the type of the object being sent.
Is there a way to send generic data type ?
According to Microsoft pages, the Any
field could be an answer to this problem, instead of using a string and a custom serialization/deserialization implementation to send these objects. However, I didn't find the provided c# examples understandable. I tried to solve the problem this way:
ClassTest myClassTest = new ClassTest();
Any packToSend = Any.Pack(myClassTest);
return Task.FromResult(new UnknownTEST
{
Pathm = hai
}); ;
But it seems that I need to implement the IMessage
interface in my class and I don't know how to do this.
If anyone could provide a basic example to help me understand how to do this, that would be great.
Thanks !
You need to create protobuf messages which represent the data you're sending. You don't need to create your own classes as you did with your "ClassTest" class.
Here's an example:
point.proto:
generic_dto_message.proto:
C# code:
In case you are using Grpc.Tools NuGet package to generate C# code for the above written .proto files, don't forget to add this
ItemGroup
section to your .csproj file:Hope it helps!