RabbitMQ将消息传递回调用客户端

时间:2013-03-27 16:47:31

标签: c# .net wcf interface rabbitmq

这是我第一次尝试在stackoverflow上发布问题。如果问题或方法有问题,请耐心等待。我做了很多搜索,找到了我的问题的答案,但没有弄清楚。这就是我想要做的。我写了一个连接到RabbitMQ localhost的WCF服务来检索消息。我写了一个使用WCF服务的控制台程序。现在,我希望WCF从RabbitMQ中获取的任何消息都传递回控制台程序,并且WCF仍在等待接收任何即将发送的消息。我看到的示例是使用委托和事件将消息传递回Windows窗体应用程序。我在控制台程序中实现这个很困难。以下是我的WCF代码。

public class MessageQueueSvc : IService1
{
    public string HOST_NAME = "localhost";
    public string EXCHANGE_NAME = "MyExchange";
    public string QUEUE_NAME = "MyMessageQ1";
    public string ROUTING_KEY = "";

    protected bool isConsuming;
    public delegate void onReceiveMessage(byte[] message);
    public event onReceiveMessage onMessageReceived;

    public IModel Model { get; set; }
    public IConnection Connection { get; set; }
    public Subscription mSubscription { get; set; }

    public string Hello(string name)
    {
        return "Hello";
    }

    public void StartConsuming()
    {
        isConsuming = true;

        var connectionFactory = new ConnectionFactory();
        connectionFactory.HostName = "localhost";
        Connection = connectionFactory.CreateConnection();

        //connect the model, exchange, queue and bind them together
        bool durable = true;

        //after connection create a channel so that you can communicate with the broker thru this channel. 
        IModel channel = Connection.CreateModel();

        //after this declare an exchange and a queue and bind them together to this channel 
        if (!String.IsNullOrEmpty(EXCHANGE_NAME))
            channel.ExchangeDeclare(EXCHANGE_NAME, ExchangeType.Direct, durable);

        if (!String.IsNullOrEmpty(QUEUE_NAME))
        {
            channel.QueueDeclare(QUEUE_NAME, false, false, false, null);
            channel.QueueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY, null);
        }

        //once model,exchange, queue is created then start cosuming it. 
        bool autoAck = false;

        //create a subscription
        mSubscription = new Subscription(Model, QUEUE_NAME, autoAck);

        while (isConsuming)
        {
            BasicDeliverEventArgs e = mSubscription.Next();
            byte[] body = e.Body;
            String tempStr = System.Text.Encoding.UTF8.GetString(body);
            tempStr = "Processed message = " + tempStr;
            body = System.Text.Encoding.UTF8.GetBytes(tempStr);
            if (onMessageReceived != null)
            {
                //this is not working. I have to write an event handler or some sort of delegate to pass the message back to the calling program
                //and still waiting here for further messages from the server. 
                onMessageReceived(body);
            }
            mSubscription.Ack(e);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我认为你的做法有点不对。

在实例化MessageQueueSvc类时,您需要创建连接,通道和使用者,就像您目前在StartConsuming方法中所做的那样。但是,我不会创建一个无限循环来进行消息消费。

相反,当控制台应用程序调用WCF客户端时,您应该从队列中消耗。

public string GetMessage()
{
  BasicDeliverEventArgs e = mSubscription.Next();
  byte[] body = e.Body;
  String tempStr = System.Text.Encoding.UTF8.GetString(body);
  tempStr = "Processed message = " + tempStr;
  body = System.Text.Encoding.UTF8.GetBytes(tempStr);
  mSubscription.Ack(e);

  if (onMessageReceived != null)
  {
    return tempStr;
  } 
  else
  {
    return null; //or throw some kind of exception...
  }
}