You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/grpc/protobuf.md
+37-10Lines changed: 37 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,6 +69,10 @@ Protobuf supports a range of native scalar value types. The following table list
69
69
|`string`|`string`|
70
70
|`bytes`|`ByteString`|
71
71
72
+
Scalar values always have a default value and can't be set to `null`. This constraint includes `string` and `ByteString` which are C# classes. `string` defaults to an empty string value and `ByteString` defaults to an empty bytes value. Attempting to set them to `null` throws an error.
73
+
74
+
[Nullable wrapper types](#nullable-types) can be used to support null values.
75
+
72
76
### Dates and times
73
77
74
78
The native scalar types don't provide for date and time values, equivalent to .NET's <xref:System.DateTimeOffset>, <xref:System.DateTime>, and <xref:System.TimeSpan>. These types can be specified by using some of Protobuf's *Well-Known Types* extensions. These extensions provide code generation and runtime support for complex field types across the supported platforms.
@@ -129,19 +133,42 @@ message Person {
129
133
}
130
134
```
131
135
132
-
Protobuf uses .NET nullable types, for example, `int?`, for the generated message property.
136
+
`wrappers.proto` types aren't exposed in generated properties. Protobuf automatically maps them to appropriate .NET nullable types in C# messages. For example, a `google.protobuf.Int32Value` field generates an `int?` property. Reference type properties like `string` and `ByteString` are unchanged except `null` can be assigned to them without error.
133
137
134
138
The following table shows the complete list of wrapper types with their equivalent C# type:
135
139
136
-
| C# type | Well-Known Type wrapper |
137
-
| --------- | ----------------------------- |
138
-
|`bool?`|`google.protobuf.BoolValue`|
139
-
|`double?`|`google.protobuf.DoubleValue`|
140
-
|`float?`|`google.protobuf.FloatValue`|
141
-
|`int?`|`google.protobuf.Int32Value`|
142
-
|`long?`|`google.protobuf.Int64Value`|
143
-
|`uint?`|`google.protobuf.UInt32Value`|
144
-
|`ulong?`|`google.protobuf.UInt64Value`|
140
+
| C# type | Well-Known Type wrapper |
141
+
| ------------ | ----------------------------- |
142
+
|`bool?`|`google.protobuf.BoolValue`|
143
+
|`double?`|`google.protobuf.DoubleValue`|
144
+
|`float?`|`google.protobuf.FloatValue`|
145
+
|`int?`|`google.protobuf.Int32Value`|
146
+
|`long?`|`google.protobuf.Int64Value`|
147
+
|`uint?`|`google.protobuf.UInt32Value`|
148
+
|`ulong?`|`google.protobuf.UInt64Value`|
149
+
|`string`|`google.protobuf.StringValue`|
150
+
|`ByteString`|`google.protobuf.BytesValue`|
151
+
152
+
### Bytes
153
+
154
+
Binary payloads are supported in Protobuf with the `bytes` scalar value type. A generated property in C# uses `ByteString` as the property type.
155
+
156
+
Use `ByteString.CopyFrom(byte[] data)` to create a new instance from a byte array:
157
+
158
+
```csharp
159
+
vardata=awaitFile.ReadAllBytesAsync(path);
160
+
161
+
varpayload=newPayloadResponse();
162
+
payload.Data=ByteString.CopyFrom(data);
163
+
```
164
+
165
+
`ByteString` data is accessed directly using `ByteString.Span` or `ByteString.Memory`. Or call `ByteString.ToByteArray()` to convert an instance back into a byte array:
0 commit comments