2016-09-28 81 views
0

我試圖在c#中實現發佈商確認,並嘗試在SO上找到以下代碼。無法找到類型或名稱空間名稱'BasicAckEventHandler'(您是否缺少使用指令或程序集引用?)

https://stackoverflow.com/a/18211367/3139595

_rabbitMqChannel.BasicAcks += new BasicAckEventHandler(_rabbitMqChannel_BasicAcks); 
_rabbitMqChannel.BasicNacks += new BasicNackEventHandler(_rabbitMqChannel_BasicNacks); 

_rabbitMqChannel.ExchangeDeclare(ExchangeName, ExchangeTypeVal.ToString()); 
_rabbitMqChannel.QueueDeclare(QueueName, QueueDurable, QueueExclusive, QueueDelete, null); 
_rabbitMqChannel.QueueBind(QueueName, ExchangeName, RoutingKey); 
and here is how the event handlers methods will look like... 

private void _rabbitMqChannel_BasicNacks(IModel model, BasicNackEventArgs args) 
{ 
    throw new NotImplementedException(); 
} 

private void _rabbitMqChannel_BasicAcks(IModel model, BasicAckEventArgs args) 
{ 
    throw new NotImplementedException(); 
} 

這個答案似乎已經爲他工作,但我得到了下面的錯誤。

The type or namespace name 'BasicAckEventHandler' could not be found (are you missing a using directive or an assembly reference?) 

我使用rabbitmqdotnet DLL的當前版本:rabbitmqdotnet-客戶端3.6.5-DOTNET-4.5

可以把它的蜜蜂,他們也斷BasicAckEventHandler從近期的版本? 或我我錯過了什麼?

注:我有以下using語句

using RabbitMQ.Client; 
using RabbitMQ.Client.Events; 
+1

這似乎是他們取消這些活動有利於'事件處理程序'。我發現** [此帖](https://groups.google.com/forum/#!msg/rabbitmq-users/sPSP3ulG6sg/PksDKvISWOwJ)** –

+0

謝謝克里斯,檢查了這一點。 –

+0

@ChrisDunaway你知道我如何將上面的腳本轉換爲使用這個新的EventHander嗎? 我需要更改這些行嗎? _rabbitMqChannel.BasicAcks + = new BasicAckEventHandler(_rabbitMqChannel_BasicAcks); _rabbitMqChannel.BasicNacks + = new BasicNackEventHandler(_rabbitMqChannel_BasicNacks); –

回答

1

在3.5.0,RabbitMqdotNet傢伙取代BasicAckEventHandler類型的 事件處理程序,看看我終於寫了我的出版商。 感謝@克里斯·達納韋的評論是向我指出這個link

public bool publish(string message) 
    { 
     var appSettings = config.getAppSettings(); 

     string HostName = appSettings["RABBITMQ_HOSTNAME"]; 
     string UserName = appSettings["RABBITMQ_USERNAME"]; 
     string Password = appSettings["RABBITMQ_PASSWORD"]; 

     var factory = new ConnectionFactory() 
     { 
      HostName = HostName, 
      UserName = UserName, 
      Password = Password 
     }; 

     using (var connection = factory.CreateConnection()) 
     using (var channel = connection.CreateModel()) 
     { 
      bool successful = false; 
      var responseReceivedEvent = new ManualResetEvent(false); 
      string exchangeName = appSettings["RABBITMQ_EXCHANGE"]; 
      string routingKey = appSettings["RABBITMQ_ROUTING_KEY"]; 
      Dictionary<string, object> headers = new Dictionary<string, object>(); 

      channel.BasicAcks += (model, args) => 
      { 
       successful = true; 
       responseReceivedEvent.Set(); 
      }; 

      channel.BasicNacks += (model, args) => 
      { 
       successful = false; 
       responseReceivedEvent.Set(); 
      }; 

      channel.ConfirmSelect(); 
      channel.ExchangeDeclare(exchangeName, ExchangeType.Topic, true, false, null); 
      var body = Encoding.UTF8.GetBytes(message); 
      IBasicProperties props = channel.CreateBasicProperties(); 
      props.ContentType = constants.RABBITMQ_MESSAGE_CONTENT_TYPE; 
      props.ContentEncoding = constants.RABBITMQ_MESSAGE_CONTENT_ENCODING; 
      props.DeliveryMode = constants.RABBITMQ_MESSAGE_DELIVERY_MODE_PERSISTENT; 
      props.MessageId = Guid.NewGuid().ToString(); 
      props.AppId = constants.APP_ID; 
      props.Type = constants.RABBITMQ_MESSAGE_TYPE; 
      props.Headers = (IDictionary<string,object>)headers; 
      props.Headers.Add("version", constants.VERSION); 
      props.Timestamp = new AmqpTimestamp(); 

      channel.BasicPublish(exchange: exchangeName, 
           routingKey: routingKey, 
           basicProperties: props, 
           body: body); 

      responseReceivedEvent.WaitOne(); 
      return successful; 
     } 

    }public bool publish(string message) 
    { 
     var appSettings = config.getAppSettings(); 

     string HostName = appSettings["RABBITMQ_HOSTNAME"]; 
     string UserName = appSettings["RABBITMQ_USERNAME"]; 
     string Password = appSettings["RABBITMQ_PASSWORD"]; 

     var factory = new ConnectionFactory() 
     { 
      HostName = HostName, 
      UserName = UserName, 
      Password = Password 
     }; 

     using (var connection = factory.CreateConnection()) 
     using (var channel = connection.CreateModel()) 
     { 
      bool successful = false; 
      var responseReceivedEvent = new ManualResetEvent(false); 
      string exchangeName = appSettings["RABBITMQ_EXCHANGE"]; 
      string routingKey = appSettings["RABBITMQ_ROUTING_KEY"]; 
      Dictionary<string, object> headers = new Dictionary<string, object>(); 

      channel.BasicAcks += (model, args) => 
      { 
       successful = true; 
       responseReceivedEvent.Set(); 
      }; 

      channel.BasicNacks += (model, args) => 
      { 
       successful = false; 
       responseReceivedEvent.Set(); 
      }; 

      channel.ConfirmSelect(); 
      channel.ExchangeDeclare(exchangeName, ExchangeType.Topic, true, false, null); 
      var body = Encoding.UTF8.GetBytes(message); 
      IBasicProperties props = channel.CreateBasicProperties(); 
      props.ContentType = constants.RABBITMQ_MESSAGE_CONTENT_TYPE; 
      props.ContentEncoding = constants.RABBITMQ_MESSAGE_CONTENT_ENCODING; 
      props.DeliveryMode = constants.RABBITMQ_MESSAGE_DELIVERY_MODE_PERSISTENT; 
      props.MessageId = Guid.NewGuid().ToString(); 
      props.AppId = constants.APP_ID; 
      props.Type = constants.RABBITMQ_MESSAGE_TYPE; 
      props.Headers = (IDictionary<string,object>)headers; 
      props.Headers.Add("version", constants.VERSION); 
      props.Timestamp = new AmqpTimestamp(); 

      channel.BasicPublish(exchange: exchangeName, 
           routingKey: routingKey, 
           basicProperties: props, 
           body: body); 

      responseReceivedEvent.WaitOne(); 
      return successful; 
     } 

    } 
相關問題