2011-11-04 63 views
7

我正在尝试编写一个通过rabbitMQ绑定工作的wcf服务。我能够成功创建服务器和客户端,并让客户端通过队列将消息发送到服务器。我对2个问题感到困惑。带WCF和持久队列的RabbitMQ

  1. 只要服务关闭,队列就会被删除。有没有办法配置wcf和rabbitMQ,使队列持久?这样我不必担心如果我的服务器崩溃丢失数据。

  2. 我似乎无法控制队列的名称。当我运行rabbitmqctl.bat list_queues时,我看到队列叫做amq.gen-3IgZD30XvTEQWNRsezSUUA==。有没有办法控制队列的名称?

回答

9

这不能用WCF绑定来完成。详细信息请参见this邮件列表线程。

基本上,你无法控制通过WCF的队列名称,这意味着你只限于匿名队列(就像一个你所看到的),这反过来,意味着你只能使用非持久队列。

如果您需要比WCF绑定提供的更多控制,则应考虑使用完整的.NET客户端。它很容易使用,并且有很多帮助你开始使用的(他们使用Java,但.NET API非常相似)。

+4

谢谢。这是我正在寻找的答案。如果它不能给你这个控制,它基本上使得甚至没有rabbitMQ的WCF绑定是没有意义的。 –

5

我遇到了和你一样的问题,我做的是编辑rabbitMQDotNetClient的源代码。

文件:RabbitMQInputChannel.cs

public override void Open(TimeSpan timeout) 
    {    
     if (State != CommunicationState.Created && State != CommunicationState.Closed) 
      throw new InvalidOperationException(string.Format("Cannot open the channel from the {0} state.", base.State)); 

     OnOpening(); 
#if VERBOSE 
     DebugHelper.Start(); 
#endif 
     //Create a queue for messages destined to this service, bind it to the service URI routing key 
#if USE_DEFINED_QUEUE_NAMES 
     //here we create a queue that uses the name given in the service address in the wcf binding. 
     //if the address in the web.config is: soap.amq:///QueueName 
     //the name of the queue will be: QueueName 
     //LVV 
     string queue = m_model.QueueDeclare(base.LocalAddress.Uri.PathAndQuery, true, false, false, null); 
#else 
     string queue = m_model.QueueDeclare(); 
#endif 
     m_model.QueueBind(queue, Exchange, base.LocalAddress.Uri.PathAndQuery, null); 

     //Listen to the queue 
     m_messageQueue = new QueueingBasicConsumer(m_model); 
     m_model.BasicConsume(queue, false, m_messageQueue); 

#if VERBOSE 
     DebugHelper.Stop(" ## In.Channel.Open {{\n\tAddress={1}, \n\tTime={0}ms}}.", LocalAddress.Uri.PathAndQuery); 
#endif 
     OnOpened(); 
    } 

编译旗USE_DEFINED_QUEUE_NAMES。这将使用您在app.config或web.config文件中给出的名称创建一个队列名称。如果您希望队列的行为与我创建的队列行为不同,您可以随时更改QueueDeclare(...)上的队列选项。 干杯!