ASP.NET CORE结合RabbitMq消息队列
ASP.NET CORE combined with RabbitMq message queue
我正在使用asp.net核心创建项目,我想结合rabbitmq实现消息的发送和接收queue.I使用docker安装rabbitmq并运行successfully.But我在项目中注册的队列在RabbitMq管理界面没有显示
ConnectionFactory factory = new ConnectionFactory
{
UserName = "guest",
Password = "guest",
HostName = "*******"
};
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.QueueDeclare("test", false, false, false, null);
Console.WriteLine("\nRabbitMQ enter exit to exit!");
string input;
do
{
input = Console.ReadLine();
var sendBytes = Encoding.UTF8.GetBytes(input);
channel.BasicPublish("", "test", null, sendBytes);
} while (input.Trim().ToLower()!="exit");
channel.Close();
connection.Close();
以上是我的消息制作code.But我的RabbitMq仪表盘上没有显示。
enter image description here
您没有指定要连接的端口号和要连接的VirtualHost,我不确定您是否因此而导致。你有具体的错误吗?或者您的项目可以 运行 正常。我在这里写了一个发送消息的demo,你可以参考一下,可能对你有用
class Program
{
static void Main(string[] args)
{
try
{
var factory = new ConnectionFactory();
factory.VirtualHost = "/";
factory.HostName = "localhost";
factory.Port = 5672;
factory.UserName = "guest";//Default username guest
factory.Password = "guest";//Default password guest
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.Write(string.Format("RabbitMQ connection error :{0}\n", ex.ToString()));
}
}
}
参考官方文档示例:
https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
如果您 运行 此代码并收到此错误:
检查你对应的虚拟主机是否匹配,我这里设置了“/”,如果不匹配,请在这里添加你的主机:
结果:
控制台:
rabbitmq:
我正在使用asp.net核心创建项目,我想结合rabbitmq实现消息的发送和接收queue.I使用docker安装rabbitmq并运行successfully.But我在项目中注册的队列在RabbitMq管理界面没有显示
ConnectionFactory factory = new ConnectionFactory
{
UserName = "guest",
Password = "guest",
HostName = "*******"
};
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.QueueDeclare("test", false, false, false, null);
Console.WriteLine("\nRabbitMQ enter exit to exit!");
string input;
do
{
input = Console.ReadLine();
var sendBytes = Encoding.UTF8.GetBytes(input);
channel.BasicPublish("", "test", null, sendBytes);
} while (input.Trim().ToLower()!="exit");
channel.Close();
connection.Close();
以上是我的消息制作code.But我的RabbitMq仪表盘上没有显示。
enter image description here
您没有指定要连接的端口号和要连接的VirtualHost,我不确定您是否因此而导致。你有具体的错误吗?或者您的项目可以 运行 正常。我在这里写了一个发送消息的demo,你可以参考一下,可能对你有用
class Program
{
static void Main(string[] args)
{
try
{
var factory = new ConnectionFactory();
factory.VirtualHost = "/";
factory.HostName = "localhost";
factory.Port = 5672;
factory.UserName = "guest";//Default username guest
factory.Password = "guest";//Default password guest
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.Write(string.Format("RabbitMQ connection error :{0}\n", ex.ToString()));
}
}
}
参考官方文档示例:
https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
如果您 运行 此代码并收到此错误:
检查你对应的虚拟主机是否匹配,我这里设置了“/”,如果不匹配,请在这里添加你的主机:
结果:
控制台:
rabbitmq: