好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Windows 消息队列服务器设置步骤及C#开发调用方法

一、启用消息队列服务

?通过控制面板启用功能?:打开 ?控制面板? → ?程序? → ?启用或关闭 Windows 功能?。

勾选 ?Microsoft 消息队列 (MSMQ) 服务器?,并展开子项选择如下功能:

?1、MSMQ 服务器核心?(必选)

?2、目录服务集成?(用于域环境集成)

?3、HTTP 支持?(需 Web 服务交互时启用)

总之全选肯定是没错的,然后再点击 ?确定?,系统自动安装组件,可能需要重启。

?操作注意事项?:

Windows Server 需通过 ?服务器管理器? → ?添加功能? 完成安装。安装完成后,通过 services.msc 检查 ?Message Queuing? 服务是否启动。

二、配置消息队列(如果不知道权限啥的建议用程序自动建立,不用在这里配置)

?1、创建消息队列?:打开 ?计算机管理? → ?服务和应用程序? → ?消息队列?。右键 ?专用队列? → ?新建专用队列?,输入队列名称(如 hdhcms_queue_test),勾选 ?事务性?(需事务支持时启用)。

?2、队列权限管理?:右键目标队列 → ?属性? → ?安全? 选项卡,添加用户或组并设置权限(如发送/接收消息、完全控制)。

三、操作实例

1、不管是BS端还是CS端都要引入下面的命名空间

  using System.Messaging;

2、CS端消息队列的使用实例:

private void btnHdhCms_Click(object sender, EventArgs e)

{

    string hdhcmsQueuePath = @".\private$\hdhcms_queue_test";

    bool boolExit = MessageQueue.Exists(hdhcmsQueuePath);

    if (!boolExit)

    {

        // 创建队列(若不存在)

        MessageQueue.Create(hdhcmsQueuePath);

    }

    // 发送消息

    MessageQueue hdhcmsQueue = new MessageQueue(hdhcmsQueuePath);

    // 消息内容支持字符串或序列化对象:ml-citation{ref="5,8" data="citationList"}

    hdhcmsQueue.Send("测试消息");

    // 接收消息

    Message hdhcmsReBackMsg = hdhcmsQueue.Receive();

    //Console.WriteLine(hdhcmsReBackMsg.Body.ToString());

    MessageBox.Show(hdhcmsReBackMsg.Body.ToString());

}

3、BS端MVC使用消息队列的实例:

public ActionResult Index()

{

    string hdhcmsQueuePath = @".\private$\hdhcms_queue_test";

    bool boolExit = MessageQueue.Exists(hdhcmsQueuePath);

    if (!boolExit)

    {

        // 创建队列(若不存在)

        MessageQueue.Create(hdhcmsQueuePath);

    }

    // 发送消息

    MessageQueue hdhcmsQueue = new MessageQueue(hdhcmsQueuePath);

    // 消息内容支持字符串或序列化对象:ml-citation{ref="5,8" data="citationList"}

    hdhcmsQueue.Send("测试消息");

    // 接收消息

    Message hdhcmsReBackMsg = hdhcmsQueue.Receive();

    //Console.WriteLine(hdhcmsReBackMsg.Body.ToString());

    //MessageBox.Show(hdhcmsReBackMsg.Body.ToString());

    return Content(hdhcmsReBackMsg.Body.ToString());

    //return View();

}


查看更多关于Windows 消息队列服务器设置步骤及C#开发调用方法的详细内容...

  阅读:19次