本帖最后由 天若有情 于 2014-6-16 14:02 编辑 ( \; ]: S x2 b4 Q. N- E0 b- `- V" d
7 P' r$ V: W7 e) x/ O7 i
本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。
+ [6 S4 O! r1 S* ^ 托管内部WCF服务, g' r4 e- U' i7 o6 R& t [
其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
+ ^! u1 u! r# H( I* K5 j public override bool OnStart()
* f g" K! T* ?8 B3 ?* k {
% M* B- C& t, m8 y // 设置最大并发连接数
( A4 Y/ B" u6 z+ j: m/ O ServicePointManager.DefaultConnectionLimit = 12;, [% [/ L6 L4 M
DiagnosticMonitor.Start(“DiagnosticsConnectionString”);% f6 I7 E0 H2 M3 F( G) ^
// For information on handling configuration changes5 B, d5 w0 M0 r
// see the MSDN topic at =166357.
: H4 ?$ L' T7 ?: A RoleEnvironment.Changing += RoleEnvironmentChanging;, P3 ]. K* s* q8 _
StartWCFService();
1 Z# t: T$ V0 } [' S return base.OnStart();
' g& g- W- c X! p8 p/ L }
* ^3 Q2 }3 F0 X, w6 v/ c( F private void StartWCFService()! F4 S/ V: r) P
{
5 r$ i8 P3 V- w E& @% |' _ var baseAddress = String.Format(( y$ d% O0 L* ~ \% w
“net.tcp://{0}”,
. X0 E, E. `: y" m# c1 \( @' } RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint. ^8 G5 I7 }' E6 m" {* X: T
);
& l* T7 W7 b) G8 S2 ~ var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));, `+ U! Q) [/ P8 U2 m: W
host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);& b" z2 g2 P( |
host.Open();" |! a) R7 P$ Y' i% p1 s
使用内部WCF服务
: `3 _8 K5 a' R$ c5 P1 r 我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:9 Q; _+ g( A6 Z) a z; W" m
protected void Button1_Click(object sender, EventArgs e)
3 P3 K2 _# ]/ M- x {& ]! f+ I4 G7 t" l
var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
- W: Y7 W) M- \4 L% e var channel = factory.CreateChannel(GetRandomEndpoint());" t) T" F* [6 }& f! k
Label1.Text = channel.Echo(TextBox1.Text);
5 {. l" p! Z! C3 w0 U# o# s }
5 b k7 J1 p2 A' Q4 W: M% a) a3 x private EndpointAddress GetRandomEndpoint()$ S/ Z" D2 z" N
{& Q5 x2 S9 \" a. M: l& {
var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances
4 Y- G' }( D; E" m$ |, A .Select(i =》 i.InstanceEndpoints[“EchoService”])' n. z0 d$ w- H# o' x, }
.ToArray();* O! Z \8 `/ g/ o
var r = new Random(DateTime.Now.Millisecond);4 X/ V3 @8 p) h! `# g- U+ L$ r9 G
return new EndpointAddress(0 l+ m/ S4 ?4 L* i0 J
String.Format(
9 G i- o; ` u# u7 V& J “net.tcp://{0}/echo”,0 R0 e% J9 u9 K- R f/ J8 h" K* R
endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)
9 O5 }) j1 M% U+ n );
3 w$ D* C9 c* {% W }
" |/ o* N( m9 O7 H2 k' I 这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。
" O* W' [0 z8 Q2 p" m 我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。% E; G$ `) c6 O$ [9 U
托管公共WCF服务6 d! ?/ Z; ^9 V5 L) n. ~* ], {8 Y& l- l
托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。8 j( G. I9 u1 u3 S4 y# h7 B
|