我正在尝试使用MVC作为发送者应用程序在Apache ActiveMQ中对消息进行排队,并且我有一个用c#构建的窗口服务作为Apache ActiveMQ的监听器。 但是我在上面的听众端阅读消息时遇到异常。
发件人MVC代码。
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服务代码。
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();
}
}
Window服务端的例外 当我展开Message对象时 '((Apache.NMS.ActiveMQ.Commands.ActiveMQObjectMessage)(message))。Body'抛出了'System.Runtime.Serialization.SerializationException'类型的异常