webabcd - 专注于asp.net
ASP.NET
从现在开始 一切都不晚
posts - 150, comments - 4182, trackbacks - 344, articles - 0
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
Posted on 2008-07-11 14:47
webabcd
阅读(3301)
评论(10)
编辑
收藏
所属分类:
WCF
[索引页]
[源码下载]
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
作者:
webabcd
介绍
WCF(Windows Communication Foundation) - 消息队列(MSMQ - MicroSoft Message Queue):
netMsmqBinding的binding属性配置如下:
·
ExactlyOnce - 确保消息只被投递一次。只能应用于事务型队列,默认值 ture
·
Durable - 消息是否需要持久化。默认值 enabled,如果设置为disable,当MSMQ服务重启后,先前保存在MSMQ中的消息将会丢失
·TimeToLive - 消息过期并且从原有的队列移动到死信队列的时间。默认值 1.00:00:00 (1天)
·ReceiveRetryCount - 配置队列管理器在一定重试间隔中,尝试重新投递消息的次数,也就是将消息传输到重试队列前尝试发送该消息的最大次数(每隔RetryCycleDelay的时间重试ReceiveRetryCount次)。缺省值 5
·MaxRetryCycles - 配置队列管理器重新投递消息的重试间隔数(执行RetryCycleDelay的次数),也就是重试最大周期数。缺省值 2
·RetryCycleDelay - 表示两次重试之间的间隔时间,也就是重试周期之间的延迟。缺省值 00:30:00
·ReceiveErrorHandling - 指定如何处理错误的消息。Fault、Drop、Reject或Move(具体说明查MSDN)
·DeadLetterQueue - 指定所使用的死信队列的类型。None、System、或Custom(具体说明查MSDN)
·CustomDeadLetterQueue - 本地自定义死信队列的URI
示例
1、服务
IMSMQ.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace
WCF.ServiceLib.Message
{
/**/
///
<summary>
///
演示MSMQ的接口
///
</summary>
///
<remarks>
///
DeliveryRequirements - 指定绑定必须提供给服务或客户端实现的功能要求
///
QueuedDeliveryRequirements - 指定服务的绑定是否必须支持排队协定
///
QueuedDeliveryRequirementsMode.Allowed - 允许排队传送
///
QueuedDeliveryRequirementsMode.Required - 要求排队传送
///
QueuedDeliveryRequirementsMode.NotAllowed - 不允许排队传送
///
</remarks>
[ServiceContract]
[DeliveryRequirements(QueuedDeliveryRequirements
=
QueuedDeliveryRequirementsMode.Required)]
public
interface
IMSMQ
{
/**/
///
<summary>
///
将字符串写入文本文件
///
</summary>
///
<param name="str">
需要写入文本文件的字符串
</param>
///
<remarks>
///
如果要使用 MSMQ 的话,则必须配置IsOneWay = true
///
</remarks>
[OperationContract(IsOneWay
=
true
)]
void
Write(
string
str);
}
}
MSMQ.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace
WCF.ServiceLib.Message
{
/**/
///
<summary>
///
演示MSMQ的类
///
</summary>
public
class
MSMQ : IMSMQ
{
/**/
///
<summary>
///
将字符串写入文本文件
///
</summary>
///
<param name="str">
需要写入文本文件的字符串
</param>
public
void
Write(
string
str)
{
System.IO.StreamWriter sw
=
new
System.IO.StreamWriter(
@"
C:\WCF_Log_MSMQ.txt
"
,
true
);
sw.Write(str);
sw.WriteLine();
sw.Close();
}
}
}
2、宿主
MSMQ.cs
//
队列名
//
只能使用.\private$\YourPrivateMSMQName来访问本机的私有MSMQ队列
string
queueName
=
@"
.\private$\SampleMSMQ
"
;
//
没有queueName队列,则创建queueName队列
if
(
!
System.Messaging.MessageQueue.Exists(queueName))
{
//
第二个参数为是否创建事务性队列
System.Messaging.MessageQueue.Create(queueName,
true
);
}
using
(ServiceHost host
=
new
ServiceHost(
typeof
(WCF.ServiceLib.Message.MSMQ)))
{
host.Open();
Console.WriteLine(
"
服务已启动(WCF.ServiceLib.Message.MSMQ)
"
);
Console.WriteLine(
"
按<ENTER>停止服务
"
);
Console.ReadLine();
}
App.config
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
system.serviceModel
>
<
services
>
<!--
name - 提供服务的类名
-->
<!--
behaviorConfiguration - 指定相关的行为配置
-->
<
service
name
="WCF.ServiceLib.Message.MSMQ"
behaviorConfiguration
="MessageBehavior"
>
<!--
address - 服务地址
-->
<!--
binding - 通信方式
-->
<!--
contract - 服务契约
-->
<!--
bindingConfiguration - 指定相关的绑定配置
-->
<
endpoint
address
=""
binding
="netMsmqBinding"
contract
="WCF.ServiceLib.Message.IMSMQ"
bindingConfiguration
="MSMQBindingConfiguration"
/>
<
endpoint
address
="mex"
binding
="mexHttpBinding"
contract
="IMetadataExchange"
/>
<
host
>
<
baseAddresses
>
<
add
baseAddress
="http://localhost:12345/Message/MSMQ"
/>
<
add
baseAddress
="net.msmq://localhost/private/SampleMSMQ"
/>
</
baseAddresses
>
</
host
>
</
service
>
</
services
>
<
behaviors
>
<
serviceBehaviors
>
<
behavior
name
="MessageBehavior"
>
<!--
httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false
-->
<
serviceMetadata
httpGetEnabled
="true"
/>
<
serviceDebug
includeExceptionDetailInFaults
="true"
/>
</
behavior
>
</
serviceBehaviors
>
</
behaviors
>
<
bindings
>
<
netMsmqBinding
>
<
binding
name
="MSMQBindingConfiguration"
>
<
security
>
<!--
msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain
-->
<!--
MsmqAuthenticationMode.None - 不使用任何安全性
-->
<!--
MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域
-->
<!--
MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中
-->
<!--
msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign
-->
<!--
ProtectionLevel.None - 只做身份验证
-->
<!--
ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性
-->
<!--
ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性
-->
<
transport
msmqAuthenticationMode
="None"
msmqProtectionLevel
="None"
/>
<!--
clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName
-->
<!--
BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证
-->
<!--
BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证
-->
<
message
clientCredentialType
="UserName"
/>
</
security
>
</
binding
>
</
netMsmqBinding
>
</
bindings
>
</
system.serviceModel
>
</
configuration
>
3、客户端
MSMQ.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.ServiceModel;
namespace
Client2.Message
{
/**/
///
<summary>
///
演示Message.MSMQ的类
///
</summary>
public
class
MSMQ
{
/**/
///
<summary>
///
用于测试 MSMQ 的客户端
///
</summary>
///
<param name="str">
需要写入文本文件的字符串
</param>
public
void
HelloMSMQ(
string
str)
{
using
(var proxy
=
new
MessageSvc.MSMQ.MSMQClient())
{
proxy.Write(str);
}
}
}
}
App.config
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
system.serviceModel
>
<
client
>
<!--
address - 服务地址
-->
<!--
binding - 通信方式
-->
<!--
contract - 服务契约
-->
<!--
bindingConfiguration - 指定相关的绑定配置
-->
<
endpoint
address
="net.msmq://localhost/private/SampleMSMQ"
binding
="netMsmqBinding"
contract
="MessageSvc.MSMQ.IMSMQ"
bindingConfiguration
="MSMQBindingConfiguration"
/>
</
client
>
<
bindings
>
<
netMsmqBinding
>
<
binding
name
="MSMQBindingConfiguration"
>
<
security
>
<!--
msmqAuthenticationMode - 指示 MSMQ 传输必须采用什么方式对消息进行身份验证,默认值 WindowsDomain
-->
<!--
MsmqAuthenticationMode.None - 不使用任何安全性
-->
<!--
MsmqAuthenticationMode.WindowsDomain - 通过 Kerberos 进行身份验证,客户端和服务器必须连接到受信任域
-->
<!--
MsmqAuthenticationMode.Certificate - 客户端通过 X.509 证书进行身份验证,客户端证书必须显示在服务器的证书存储区中
-->
<!--
msmqProtectionLevel - 保护级别,设置与 MsmqAuthenticationMode 相关联的 ProtectionLevel,默认值 Sign
-->
<!--
ProtectionLevel.None - 只做身份验证
-->
<!--
ProtectionLevel.Sign - 对数据做签名,以确保所传输数据的完整性
-->
<!--
ProtectionLevel.EncryptAndSign - 对数据做加密和签名,以确保所传输数据的保密性和完整性
-->
<
transport
msmqAuthenticationMode
="None"
msmqProtectionLevel
="None"
/>
<!--
clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName
-->
<!--
BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证
-->
<!--
BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证
-->
<
message
clientCredentialType
="UserName"
/>
</
security
>
</
binding
>
</
netMsmqBinding
>
</
bindings
>
</
system.serviceModel
>
</
configuration
>
运行结果:
客户端调用时,如果没有启动服务端,那么消息会进入到消息队列中。等到服务端启动后,会执行消息队列中的所有消息。
OK
[源码下载]
Feedback
#1楼
回复
引用
2008-07-12 00:45 by
黑白 [未注册用户]
上次和博主留言说想看一个msmq的例子,果然就有了
多谢
#2楼
[
楼主
]
回复
引用
查看
2008-07-14 07:23 by
webabcd
@黑白
:)
呵呵,不谢,在计划之列
#3楼
回复
引用
2008-07-14 23:17 by
黑白 [未注册用户]
你那个“客户端调用时,如果没有启动服务端,那么消息会进入到消息队列中”
这个消息队列要从哪看啊
#4楼
[
楼主
]
回复
引用
查看
2008-07-15 07:42 by
webabcd
@黑白
右键 - 我的电脑 - 管理 - 服务和应用程序 - 消息队列
#5楼
回复
引用
2008-07-20 15:49 by
黑白 [未注册用户]
@webabcd
找不到啊,和系统有关系吗,我的是win2003
#6楼
[
楼主
]
回复
引用
查看
2008-07-21 07:33 by
webabcd
@黑白
xp和03是msmq3
vista和08是msmq4
找不到的话,应该是没装
在添加windows组件中安装
#7楼
回复
引用
2008-07-22 16:51 by
student2 [未注册用户]
vista下报错
net.msmq://localhost/private/logService
打开队列时出错。确保已安装和运行 MSMQ,队列存在并且具有正确的读取权限
#8楼
[
楼主
]
回复
引用
查看
2008-07-22 18:39 by
webabcd
@student2
:)
看这个错误,应该是没装MSMQ吧
#9楼
回复
引用
查看
2008-08-07 18:02 by
菜无罪1
net.msmq://localhost/private/logService
打开队列时出错。确保已安装和运行 MSMQ,队列存在并且具有正确的读取权限
我的2003也出现了这样的错误
#10楼
[
楼主
]
回复
引用
查看
2008-08-11 08:21 by
webabcd
@菜无罪1
先确定一下是否启动了MSMQ的服务
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-07-24 08:36 编辑过
相关文章:
ExtJs+WCF+LINQ实现分页Grid
ExtJs+WCF+LINQ实现分页Grid
消息队列(Message Queue)简介及其使用
利用 Remoting 实现异步队列机制
有办法在WCF中方便地凭空创建Message对象吗?
学习WCF是不是就不需要学习.net remoting等了
轻松玩转MSMQ, WCF和 IIS 7.0
WCF技术书籍推荐
相关链接:
所属分类的其他文章:
化零为整WCF系列文章索引
化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)
化零为整WCF(17) - 安全(Security)
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
化零为整WCF(15) - 可靠性消息(ReliableMessaging)
化零为整WCF(14) - 事务(Transaction)
化零为整WCF(13) - 并发控制(锁)(Mutex, Semaphore, Monitor, Lock, ThreadPool, Interlocked, ReaderWriterLock)
化零为整WCF(12) - 并发和限流(Concurrent和Throttle)
化零为整WCF(11) - 会话状态(Session)
化零为整WCF(10) - 实例模型(InstanceContextMode)
最新IT新闻:
搞死开心网还是搞活他?
网络书店“新”军
百度C2C电子商务平台“有啊”youa.com上线
Silverlight 2.0正式版下周发布
Wikipedia“变心”,力挺Ubuntu
Powered by:
博客园
Copyright © webabcd
公告
网名:webabcd
本名:王磊
职业:号称软件工程师
位置:中国 北京
安装 Webabcd Activity
请使用IE8浏览器
与我互动
给我发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(137)
给我留言
查看私人留言
我参与的团队
Design & Pattern团队(0/869)
.Net 商业智能(0/395)
.NET 控件与组件开发(0/1385)
Ajax&Atlas技术团队(0/1138)
BluePrint翻译团队(0/171)
ASP.NET AJAX (Atlas)学习(0/1352)
.NetFramework3.0 & 3.5(WCF,WPF,WF)团队(0/567)
报表与图表团队(0/178)
Silverlight学习与研究(0/287)
Windows Vista开发(0/237)
CLR基础研究团队(0/407)
数据库团队(0/276)
WCF技术研究团队(0/178)
asp.net开发团队(0/234)
随笔分类
(215)
AJAX/JavaScript(23)
ASP.NET(16)
C#(5)
Control控件(8)
CustomControl自定义控件(18)
DesignPattern设计模式(25)
Flash ActionScript(3)
Index文章索引(9)