类型或命名空间名称'BasicAckEventHandler'找不到(您是否缺少using指令或程序集引
本文关键字:using 指令 是否 程序集 找不到 命名空间 BasicAckEventHandler 类型 | 更新日期: 2023-09-27 18:09:17
我正在尝试在c#中实现出版商确认,并尝试在SO上找到以下代码。
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: rabbitmq-dotnet-client-3.6.5-dotnet-4.5
是不是最近版本的BasicAckEventHandler也没了?还是我遗漏了什么?
注意:我有以下using语句
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
在3.5.0中,RabbitMqdotNet家伙将basicackeventandler类型替换为EventHandler,看看我是怎么给我的出版商写信的。感谢@Chris Dunaway的评论,让我看到了这个链接
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;
}
}