The EventData class contains a property Properties... that allow you to add metadata to the message:
Gets the user properties of the event data that the user explicitly added during send operations.
So to send a event :
var eventHubClient = EventHubClient.CreateFromConnectionString("connectionString", "eventHubName");
var mypoco = new POCO();
// ...
// Get the Json string
var stringBody = JsonConvert.SerializeObject(mypoco);
// Create the event data
var eventData = new EventData(Encoding.UTF8.GetBytes(stringBody));
// Add the event type.
eventData.Properties.Add("EventType", typeof(POCO).Assembly.FullName);
// Send the data.
eventHubClient.Send(eventData);
While receiving your message, you'll get the event type from the metada of the message:
async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (EventData eventData in messages)
{
var jsonBody = Encoding.UTF8.GetString(eventData.GetBytes());
//Get the event type
var eventTypeName = (string)eventData.Properties["EventType"];
var eventType = Type.GetType(eventTypeName);
// Deserialize the object
var myPoco = JsonConvert.DeserializeObject(jsonBody, eventType);
}
}
Otherwise you can get rid of the body type by using a JObject
async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (EventData eventData in messages)
{
var jsonBody = Encoding.UTF8.GetString(eventData.GetBytes());
// Deserialize the json as a JObject
var myPoco = JObject.Parse(jsonBody);
var a = myPoco["X"]["a"];
}
}