'gRPC API classes change when auto-generated by Visual Studio?
I'm trying to consume a gRPC API from my .NET Core 3.1 application. The idea is to call the gRPC API using serialized JSON objects. I have imported the proto file for the API and so far everything has worked as expected.
However some of the methods take object as arguments and when I deliver these objects in JSON format (as they are described in the proto) in order to deserialize them and call the API, the result of the deserialization doesn't correspond with what the class expects and therefore the object becomes an "empty" one { }.
I found that odd and tried to create an object from of that specific type, which turned out to be different from what the proto file describes. Can anyone give me an idea as to why this is?
Example:
Proto file describes this (among other things of cause):
message BlockHash {
string block_hash = 1;
}
If I try to call the API using BloomRPC (a RPC client application) with the following payload:
{
"block_hash": "bf713572e026d0e76ea4b5770b002e5e62b443181bbe39cefa160a5d81e9b356"
}
The call is successful and I get the expected response from the API.
However, if I send this JSON to my own application, which attempts to deserialize it, the object is empty { }.
If I try to create an instance of this object in my code (C#) like this:
BlockHash bh = new BlockHash()
{
BlockHash_ = "bf713572e026d0e76ea4b5770b002e5e62b443181bbe39cefa160a5d81e9b356"
};
As you can see, the object has an attribute called "BlockHash_" and not "block_hash". Sending this created object as payload works as well, but this is not how I wanted it to be done - I want it to be done via a JSON object, like with the BloomRPC client.
It gets a little more odd if I try to serialize the object I just created in code, because that looks like this:
{
"blockHash": "bf713572e026d0e76ea4b5770b002e5e62b443181bbe39cefa160a5d81e9b356"
}
Which is a 3rd representation of the same object, notice that the attribute is now called "blochHash" - not "block_hash nor "BlockHash_", but "blockHash"...
Can anyone tell me what's going on here, and why, and maybe how I can make this work like I want it to? :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
