国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

.Net消息隊列的使用方法

2019-11-02 15:52:45
字體:
來源:轉載
供稿:網友

 這篇文章主要介紹了.Net消息隊列的使用方法,需要的朋友可以參考下

.Net使用消息隊列,借助windows組件來存儲要完成的一系列任務,不用程序使用同一個隊列,方便不同程序之間的數據共享和協作…… 以本人經驗,這個在某個方面類似于session(當然還有很多方面不同),相同之處:session可以把信息存儲在aspnet_state服務中,網站重新編譯或者重新啟動網站,session不會丟失(session超時是正常情況,這種情況除外)。 win7中安裝消息隊列組件,其他操作系統請百度搜索相關資料。16.jpg16.jpg .Net消息隊列的使用方法   電腦高手 如果服務沒有自動啟動,需要啟動服務:22.jpg 先創建隊列,再使用隊列,隊列中的消息,發送一個多一個,接收一個少一個,先進先出。 碼 代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Messaging;//添加物理文件 System.Messaging 的引用namespace testweb{    public partial class MSMQtest : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            //CreateNewQueue("MsgQueue");//創建一個消息隊列            //sendSimpleMsg();//每一個隊列最好只發送和接收同一種格式的信息,不然不好轉換格式。            //receiveSimpleMsg();//            //receiveSimpleMsg();            //sendComplexMsg();            //receiveComplexMsg();            MsgModel m = receiveComplexMsg<MsgModel>();            Response.Write(m.ToString());         }        private void sendSimpleMsg()        {            //實例化MessageQueue,并指向現有的一個名稱為VideoQueue隊列            MessageQueue MQ = new MessageQueue(@".private$MsgQueue");            //MQ.Send("消息測試", "測試消息");            System.Messaging.Message message = new System.Messaging.Message();            message.Label = "消息lable";            message.Body = "消息body";            MQ.Send(message);             Response.Write("成功發送消息," + DateTime.Now + "<br/>");        }        private void receiveSimpleMsg()        {            MessageQueue MQ = new MessageQueue(@".private$MsgQueue");            //調用MessageQueue的Receive方法接收消息            if (MQ.GetAllMessages().Length > 0)            {                System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));                if (message != null)                {                    //message.Formatter = new System.Messaging.XmlMessageFormatter(new string[] { "Message.Bussiness.VideoPath,Message" });//消息類型轉換                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });                    Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, message.Body.ToString(), DateTime.Now));                }            }            else            {                Response.Write("沒有消息了!<br/>");            }        }        private void sendComplexMsg()        {            //實例化MessageQueue,并指向現有的一個名稱為VideoQueue隊列            MessageQueue MQ = new MessageQueue(@".private$MsgQueue");            //MQ.Send("消息測試", "測試消息");            System.Messaging.Message message = new System.Messaging.Message();            message.Label = "復雜消息lable";            message.Body = new MsgModel("1", "消息1");            MQ.Send(message);             Response.Write("成功發送消息,"+DateTime.Now+"<br/>");        }        private void receiveComplexMsg()        {            MessageQueue MQ = new MessageQueue(@".private$MsgQueue");            //調用MessageQueue的Receive方法接收消息            if (MQ.GetAllMessages().Length > 0)            {                System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));                if (message != null)                {                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MsgModel) });//消息類型轉換                    MsgModel msg = (MsgModel)message.Body;                    Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, msg, DateTime.Now));                }            }            else            {                Response.Write("沒有消息了!<br/>");            }        }        private T receiveComplexMsg<T>()        {            MessageQueue MQ = new MessageQueue(@".private$MsgQueue");            //調用MessageQueue的Receive方法接收消息            if (MQ.GetAllMessages().Length > 0)            {                System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));                if (message != null)                {                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(T) });//消息類型轉換                    T msg = (T)message.Body;                    return msg;                }            }             return default(T);        }         /// <summary>        /// 創建消息隊列        /// </summary>        /// <param name="name">消息隊列名稱</param>        /// <returns></returns>        public void CreateNewQueue(string name)        {            if (!System.Messaging.MessageQueue.Exists(".private$" + name))//檢查是否已經存在同名的消息隊列            {                 System.Messaging.MessageQueue mq = System.Messaging.MessageQueue.Create(".private$" + name);                mq.Label = "private$"+name;                Response.Write("創建成功!<br/>");            }            else            {                //System.Messaging.MessageQueue.Delete(".private$" + name);//刪除一個消息隊列                Response.Write("已經存在<br/>");            }        }     }    [Serializable]    public class MsgModel    {        public string id { get; set; }        public string Name { get; set; }        public MsgModel() { }        public MsgModel(string _id, string _Name)        {            id = _id;            Name = _Name;        }        public override string ToString()     
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四子王旗| 石楼县| 阿城市| 韶关市| 临沂市| 蒲江县| 东莞市| 高碑店市| 彭泽县| 永兴县| 肇庆市| 宁远县| 阿拉善盟| 青浦区| 双鸭山市| 通许县| 龙胜| 铜鼓县| 玉屏| 皋兰县| 彰武县| 剑河县| 大宁县| 天等县| 独山县| 石楼县| 麦盖提县| 安远县| 枞阳县| 阳谷县| 抚州市| 峨眉山市| 澄城县| 双峰县| 宁蒗| 湖州市| 会泽县| 祁东县| 蓝田县| 黄龙县| 浏阳市|