Cannot send anonymous objects using EventGridClient
See original GitHub issueHello,
I am attempting to use the EventGridClient class to send some events to EventGrid. I was unsuccessful when it came to sending custom data in the data property of the EventGridEvent. EventGrid would just receive a null value. If I set the data property to a string value, it was received by EventGrid correctly, however using a complex object, the object was not serialized to JSON.
I’ve tracked the issue down to the EventGridClient initializing the Json serializer with an instance of the rather ambiguously named ReadOnlyJsonContractResolver(). The description of this class is:
JSON contract resolver that ignores read-only properties during serialization.
So currently, the EventGridClient will not serialize any custom event data that is exposed via readonly properties - ie: all properties on your custom data object have to be read/write. This in turn rules out anonymous objects as your custom event data payload, as anonymous objects only implement readonly properties.
Currently, the workaround is to explicitly override the contract resolver:
eventGridClient.SerializationSettings.ContractResolver = null; // Use the default resolver
What rationale is there for initializing the JSON serializer with ReadOnlyJsonContractResolver? It doesn’t seem to be the correct contract resolver to be using for one way serialization.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:8 (3 by maintainers)
Top GitHub Comments
I just ran into this issue, but I’ve found a workaround which seems to be working exactly as I would like. Just make sure to not set your object directly in the
Data
property, but wrap it withJObject.FromObject
. This way, the properties should be converted using your object configuration, while made available for theReadOnlyJsonContractResolver
.Example:
Same here. Anonymous objects should be supported.