0

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();
}
Rikard
  • 3,828
  • 1
  • 24
  • 39

1 Answers1

0

As the BrokeredMessage Constructor declaration :

public BrokeredMessage(
    Object serializableObject
)

It initializes a new instance of the BrokeredMessage class from a given object by using DataContractSerializer with a binary XmlDictionaryWriter. So the class BaseEvent has to support DataContractSerializer. As how to do it, please refer Serialization and Deserialization

Matt
  • 6,010
  • 25
  • 36
  • I have tried that but I still got the same Error. And also added the KnownType attrbute to BaseEvent. – Rikard Aug 25 '14 at 07:40