ASP.NET 操作消息队列
1
public partial class _Default : System.Web.UI.Page
2
{
3
private string queuePath = @".\Private$\mydemoqueues";
4
protected void Page_Load(object sender, EventArgs e)
5
{
6
//异步获取消息
7
GetQueueMessage();
8
}
9
protected void Button1_Click(object sender, EventArgs e)
10
{
11
MessageQueue mq;
12
if(MessageQueue.Exists(@".\Private$\mydemoqueues"))
13
{
14
mq=new MessageQueue(@".\Private$\mydemoqueues");
15
}
16
else
17
{
18
mq = MessageQueue.Create(@".\Private$\mydemoqueues");
19
}
20
BinaryWriter writer=new BinaryWriter(new MemoryStream());
21
writer.Write(this.TextBox1.Text.Trim());
22
Message msg=new Message();
23
msg.BodyStream = writer.BaseStream;
24
msg.Label = "发送信息,飘渺峰测试";
25
mq.Send(msg);
26![](/Images/OutliningIndicators/InBlock.gif)
27
}
28
protected void Button2_Click(object sender, EventArgs e)
29
{
30
MessageQueue mq;
31
Message msg=new Message();
32
string result;
33
if (MessageQueue.Exists(queuePath))
34
{
35
mq=new MessageQueue(queuePath);
36
}
37
else
38
{
39
mq = MessageQueue.Create(queuePath);
40
}
41
try
42
{
43
mq.MaximumQueueSize = 1000000;
44
msg = mq.Receive(new TimeSpan(0, 0, 3));
45
BinaryReader br=new BinaryReader(msg.BodyStream);
46
result=new string(br.ReadChars(Convert.ToInt32(msg.BodyStream.Length)));
47
this.TextBox2.Text = result;
48
mq.Close();
49
}
50
catch (Exception)
51
{
52
Response.Write("没有消息可以接收!");
53
}
54
}
55
/// <summary>
56
/// 异步获取消息队列
57
/// </summary>
58
private void GetQueueMessage()
59
{
60
MessageQueue mq=new MessageQueue(queuePath);
61
mq.Formatter = new XmlMessageFormatter(new Type[] {typeof (string)});
62
mq.PeekCompleted+=new PeekCompletedEventHandler(MyMessageCompleted);
63
mq.BeginPeek();
64
return;
65
}
66![](/Images/OutliningIndicators/InBlock.gif)
67
private void MyMessageCompleted(object sender, PeekCompletedEventArgs e)
68
{
69
MessageQueue mq = (MessageQueue) sender;
70
Message msg = mq.EndPeek(e.AsyncResult);
71
BinaryReader reader=new BinaryReader(msg.BodyStream);
72
string temp=new string(reader.ReadChars(Convert.ToInt32(msg.BodyStream.Length)));
73
//Response.Write("这是异步队列信息:"+temp);
74
this.TextBox2.Text = temp;
75
mq.BeginPeek();
76
return;
77
}
78![](/Images/OutliningIndicators/InBlock.gif)
79
private void SendMessage()
80
{
81
MessageQueue mq;
82
if (MessageQueue.Exists(@".\Private$\mydemoqueues"))
83
{
84
mq = new MessageQueue(@".\Private$\mydemoqueues");
85
}
86
else
87
{
88
mq = MessageQueue.Create(@".\Private$\mydemoqueues");
89
}
90
BinaryWriter writer = new BinaryWriter(new MemoryStream());
91
writer.Write("sdfsdfsd");
92
Message msg = new Message();
93
msg.BodyStream = writer.BaseStream;
94
msg.Label = "发送信息,飘渺峰测试";
95
mq.Send(msg);
96
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)