开发环境:
Win2008 sp2 VS2008 sp1
Add roles:Web Server(IIS7),Application Server(Message Queuing)
Add features : WCF Activation Msmq:multicasting Support
确认:Net.Msmq Listener Adapter Running
因为我是使用IIS7来Host WCF 而且WCF使用多种通道绑定,所以在WCF所在的web application 确定enable了msmq绑定
如图:
Publisher 实现
向多播地址发送Message msg
msg.Body = Your Object;
代码
1
[DataContract]
2 public class
RelMomInfo
3
{
4
[DataMember]
5 public string
GUID
6
{
7 get
;
8 set
;
9
}
10
[DataMember]
11 public string
strMomInfo
12
{
13 get
;
14 set
;
15
}
16
}
17
18 #region Test Pub-Sub :Send multicase message of rmi
19 string multiCastAddress = "224.0.255.1:8123"
;
20 using (var queue = new MessageQueue(string.Format("FormatName:MULTICAST={0}"
, multiCastAddress)))
21
{
22 RelMomInfo RMI = new
RelMomInfo();
23 RMI.GUID =
Guid.NewGuid().ToString();
24 RMI.strMomInfo = "Client Sent."
;
25
26 Message msg = new
Message();
27 msg.Body =
RMI;
28
29 queue.Send(msg, "MultiCast Msg"
);
30
}
31
32 #endregion
33
Contract
定义WCF契约
代码
namespace
RelSrvContract
{
[ServiceContract]
[ServiceKnownType(
typeof
(RelMomInfo))]
public
interface
ISrvReceiveMomInfoContract
{
[OperationContract(IsOneWay
=
true
, Action
=
"
*
"
)]
void
ReceiveMomInfo(MsmqMessage
<
RelMomInfo
>
rmi);
}
}
WCF Services
WCF Service 代码:
代码
namespace
SrvSubscriber
{
[AspNetCompatibilityRequirements(RequirementsMode
=
AspNetCompatibilityRequirementsMode.Allowed)]
public
class
SrvSubscriberOne:ISrvReceiveMomInfoContract
{
public
void
ReceiveMomInfo(MsmqMessage
<
RelMomInfo
>
rmi)
{
RelMomInfo RMI
=
(RelMomInfo)rmi.Body;
RMI.strMomInfo
=
RMI.strMomInfo
+
"
SrvReceiveOne Got it!
"
;
RelComm.SendMsgComm sm
=
new
RelComm.SendMsgComm();
sm.SendRelMsg(RMI);
}
}
[AspNetCompatibilityRequirements(RequirementsMode
=
AspNetCompatibilityRequirementsMode.Allowed)]
public
class
SrvSubscriberTwo : ISrvReceiveMomInfoContract
{
public
void
ReceiveMomInfo(MsmqMessage
<
RelMomInfo
>
rmi)
{
RelMomInfo RMI
=
(RelMomInfo)rmi.Body;
RMI.strMomInfo
=
RMI.strMomInfo
+
"
SrvReceiveTwo Got it!
"
;
RelComm.SendMsgComm sm
=
new
RelComm.SendMsgComm();
sm.SendRelMsg(RMI);
}
}
}
IIS7 Host two WCF subscribers
IIS7 Host 两个WCF 订阅
SrvSubcriberOne.svc 和 SrvSubscriberTwo.svc 的代码为:
<%
@ ServiceHost Language
=
"
C#
"
Debug
=
"
true
"
Service
=
"
SrvSubscriber.SrvSubscriberTwo
"
%>
<%
@ ServiceHost Language
=
"
C#
"
Debug
=
"
true
"
Service
=
"
SrvSubscriber.SrvSubscriberOne
"
%>
配置IIS7支持SVC
需要手动添加*.svc扩展名才能打开IIS7对WCF的支持。
打开“开始|所有程序|附件|命令提示符”,输入“cd C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation”,回车。
再输入“ServiceModelReg.exe -i”,回车。
在“Internet 信息服务(IIS)管理器”中打开“处理程序映射”会看到IIS7增加了对*.svc扩展名的映射。
IIS7的Web server需要配置支持Msmq绑定,Host Wcf 订阅服务的Web Application需要配置支持Msmq绑定.如图:
Web server需要配置支持Msmq
Web Application需要配置支持Msmq,在下图画圈的位置增加net.msmq
这两个WCF 订阅服务分别为 SrvSubcriberOne.svc SrvSubscriberTwo.svc 这两个WCF分别以绑定在不同的msmq上
需要配置WCF绑定的MSMQ,HOST在IIS7上的WCF的配置
配置bindingconfig 安全部分为None
配置Msmq:
Publisher将消息通过多播地址:224.0.255.1:8123发送出去,我们在Application Server中就会看到这个队列,我们在创建两个用于WCF绑定的非Transactional msmq 配置这两个队列的Multi Cast Address为:224.0.255.1:8123。
运行PublisherClient 发送 body为RelMomInfo对象的Message到多播地址,2个订阅WCF就会取到消息并执行。
注意:
编译后了WCF并没有运行到内存中,需要在IE中浏览一下才可以起作用,而且在修改Web.config后也需要重新刷新。
如图:
to be Continued