![](/img/trans.png)
[英]Type or namespace name could not be found (missing using directive or assembly reference?)
[英]The type or namespace name 'BasicAckEventHandler' could not be found (are you missing a using directive or an assembly reference?)
我試圖在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 RabbitMQ.Client;
using RabbitMQ.Client.Events;
在3.5.0中,RabbitMqdotNet家伙用EventHandler替換了BasicAckEventHandler類型,請參閱我最終如何編寫發布者。 感謝@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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.