question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot send anonymous objects using EventGridClient

See original GitHub issue

Hello,

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:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
bobvandevijvercommented, Jun 27, 2019

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 with JObject.FromObject. This way, the properties should be converted using your object configuration, while made available for the ReadOnlyJsonContractResolver.

Example:

return new EventGridEvent {
    Id          = Guid.NewGuid().ToString(),
    EventType   = ObjectType,
    Topic       = Topic,
    Subject     = $"/company/{CompanyUuid.Value}/{ObjectType}",
    DataVersion = DataVersion,
    EventTime   = eventTime,
    Data        = JObject.FromObject(yourObject)
};
2reactions
SeanFeldmancommented, Jul 14, 2018

Same here. Anonymous objects should be supported.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Distributed tracing with Azure Functions Event Grid triggers
However, the connection between send calls on producer and Function ... If you publish CloudEvents using Azure Event Grid client library ...
Read more >
Send Anonymous Object to Service Bus Queue in Azure
You have to mark the class and properties of your object as serializable - from How to Use Service Bus Queues: Messages sent...
Read more >
Azure Event Grid client library for .NET
To send events to a topic or domain using Azure Active Directory, ... Depending on the type of consumer being delivered to, the...
Read more >
Azure Event Grid Simulator
Events received by topic will be sent to this address. ... will only deliver events to the subscription if they are of a...
Read more >
azure-eventgrid
Install the Azure Event Grid client library for Python with pip: ... If events of a schema type are sent to a topic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found