2

我正在尝试使用 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 服务端的异常当我展开消息对象'((Apache.NMS.ActiveMQ.Commands.ActiveMQObjectMessage)(message)).Body' 抛出类型为'System.Runtime.Serialization.SerializationException' 的异常

4

0 回答 0