I'm using Azure Topics and Subscriptions as a message bus. But when I do message.GetBody() I got a SerializationException. That I want to achieve is to have ONE subscriber that reads all events of type BaseEvent, and also each event has their own subscriber. But when I'm trying to serialize the body (BaseEvent) it fails. So how do I serialize the body to get both UserEvent and TeamEvent? My code looks like this:
public abstract class BaseEvent
{
public long ActivityId { get; set; }
}
public class UserEvent
{
public string Name { get; set; }
}
public class TeamEvent
{
public string Name { get; set; }
}
I'm adding them to the message bus like this:
public async Task HandleAsync(BaseEvent request)
{
var message = new BrokeredMessage(request)
{
CorrelationId = request.Id.ToString()
};
await topicClient.SendAsync(message);
await topicClient.CloseAsync();
}
When I'm going to read from the bus, I do like this:
BrokeredMessage message;
while ((message = client.Receive(new TimeSpan(0, 0, 5))) != null)
{
models.Add(message.GetBody<BaseEvent>()); // THIS FAILS SerlizationException
message.Complete();
}