C#发送rabbitMQ
时间: 2023-10-19 15:58:58 浏览: 101
在C#中发送RabbitMQ消息可以通过使用RabbitMQ客户端库来实现。下面是一个示例代码,展示了如何在C#中发送RabbitMQ消息:
```csharp
using RabbitMQ.Client;
using System;
using System.Text;
class Program
{
static void Main(string[] args)
{
// 创建连接工厂
var factory = new ConnectionFactory()
{
HostName = "10.10.10.10", // RabbitMQ服务器的IP地址
UserName = "admin", // 用户名
Password = "adminPWD" // 密码
};
// 创建连接
using (var connection = factory.CreateConnection())
{
// 创建通道
using (var channel = connection.CreateModel())
{
// 声明队列
var queueName = "qTEST";
channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
// 消息内容
var message = "hello123";
var body = Encoding.UTF8.GetBytes(message);
// 发布消息到队列
channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
Console.WriteLine("消息已发送:{0}", message);
}
}
Console.WriteLine("点击 [enter 结束本次对话。");
Console.ReadLine();
}
}
```
请注意,上述代码中的`HostName`应该替换为你的RabbitMQ服务器的IP地址,`UserName`和`Password`应该替换为你的RabbitMQ服务器的用户名和密码。另外,你还可以根据需要修改队列的声明和消息发布的逻辑。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C#使用rabbitmq (简单例子)](https://blog.csdn.net/MFCdestoryer/article/details/125046944)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [C# WPF RabbitMq收发消息实例解析](https://blog.csdn.net/weixin_68436326/article/details/129374016)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [C# rabbitMQ 接收消息和发送消息](https://blog.csdn.net/i_qiangqiang/article/details/114707700)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文