I am trying to queue messages in Apache ActiveMQ using MVC as a sender app and i have a window service built in c# as a listener to the Apache ActiveMQ. But i am getting an exception while reading the message at the listener end above.
Sender MVC code.
public void GenerateMQMessages(EmailModel mail)
{
try
{
//// Create the Connection Factory
ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");
NmsTemplate nmsTemplate = new NmsTemplate(factory);
nmsTemplate.Send("EmailQueue", new GenericMessageCreator<dummyClass>(new dummyClass { Prop1 = 1, Prop2 = "Hello There" }));
}
catch (System.Exception e)
{
}
}
public class GenericMessageCreator<T> : IMessageCreator
{
public T Body { get; set; }
public GenericMessageCreator(T body)
{
this.Body = body;
}
IMessage IMessageCreator.CreateMessage(ISession session)
{
ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
msg.Body = this.Body;
return msg;
}
}
[Serializable]
public class dummyClass : ISerializable
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
//throw new NotImplementedException();
}
}
C# Windows service code.
protected override void OnStart(string[] args)
{
try
{
//// Create the Connection factory
ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");
SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
listenerContainer.ConnectionFactory = factory;
listenerContainer.DestinationName = "EmailQueue";
listenerContainer.MessageListener = new GenericMessageListener<dummyClass>();
listenerContainer.AfterPropertiesSet();
}
catch (System.Exception e)
{
}
}
public class GenericMessageListener<T> : IMessageListener
{
public void OnMessage(IMessage message)
{
try
{
ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
if (msg != null)
{
T body = (T)msg.Body;
//dummyClass body = (dummyClass)msg.Body;
typeof(T).GetProperties()
.ToList()
.ForEach(prop =>
{
var p = string.Format("{0} = {1},", prop.Name, prop.GetValue(body, new object[] { }));
});
}
}
catch (Exception e)
{
}
}
}
[Serializable]
public class dummyClass : ISerializable
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
//throw new NotImplementedException();
}
}
Exception on Window service side When i expand Message object '((Apache.NMS.ActiveMQ.Commands.ActiveMQObjectMessage)(message)).Body' threw an exception of type 'System.Runtime.Serialization.SerializationException'