找不到类型或命名空间名称“BasicAckEventHandler"(您是否缺少 using 指令或程序集引用?) [英] The type or namespace name 'BasicAckEventHandler' could not be found (are you missing a using directive or an assembly reference?)
问题描述
我正在尝试在 c# 中实现发布者确认,并尝试使用我在 SO 上找到的以下代码.
I am trying to implement publisher confirms in c#, and trying the following code i found on 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();
}
这个答案似乎对他有用,但我收到以下错误.
this answer seems to have worked for him, but i am getting the following error .
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
I am using the current version of rabbitmqdotnet dll : rabbitmq-dotnet-client-3.6.5-dotnet-4.5
他们会不会从最近的版本中删除 BasicAckEventHandler ?还是我在那里遗漏了什么?
could it bee they too off BasicAckEventHandler from the recent version ? or i'm i missing anything there?
注意:我有以下 using 语句
NB: i have the following using statements
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
推荐答案
在 3.5.0 中,RabbitMqdotNet 家伙将 BasicAckEventHandler 类型替换为EventHandler ,看看我最终是如何写我的发布者的.感谢@Chris Dunaway 的评论,让我看到了这个链接
In 3.5.0, RabbitMqdotNet guy replaced BasicAckEventHandler types with EventHandler , see how i finally wrote my publisher. Thanks to @Chris Dunaway's comment that pointed me to this 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;
}
}
这篇关于找不到类型或命名空间名称“BasicAckEventHandler"(您是否缺少 using 指令或程序集引用?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!