本帖最后由 天若有情 于 2014-6-16 14:02 编辑 8 X4 I: |( y* Q$ ?# I, w& I. Y+ g
. ~% T4 l1 M4 h. _, r# |
本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。: v6 W6 ^8 ?) e7 F% {$ B/ x1 j
托管内部WCF服务
+ W3 F! X+ {5 g- u) R1 x# Z# n 其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
: S) g) z( X% h6 s* t# P1 ] public override bool OnStart()
- r# C1 a6 k/ |5 _- G/ z {5 |+ s; i( ]# Q) x
// 设置最大并发连接数
; j1 `7 P2 A# ]" a& v ServicePointManager.DefaultConnectionLimit = 12;" L3 C; ?8 l* ?# W; e/ y
DiagnosticMonitor.Start(“DiagnosticsConnectionString”);
3 l/ c! t$ l. G/ A ^ // For information on handling configuration changes
6 K6 `" P0 N3 ^+ k$ s! t- A // see the MSDN topic at =166357.. ]3 u9 ]/ d, `, r5 B6 u
RoleEnvironment.Changing += RoleEnvironmentChanging;$ M* C5 k7 M; J2 f% a$ E
StartWCFService();/ _9 h- T# ?" ^" l E4 X0 V
return base.OnStart();( Z/ _% V# w$ [ z- E0 b
}
8 ?, E9 J$ ]8 m. @. y private void StartWCFService(); r: P0 _4 s6 I6 S/ r3 H1 X
{- E# q0 M! o& R9 N
var baseAddress = String.Format(, R) v2 K" I1 n5 O4 r
“net.tcp://{0}”,
! O& w7 s* Z9 B( W; W; _ RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint( l# g2 [ `9 a. S" B
); }+ y7 g1 U4 c6 j4 D9 m
var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));* d8 ~/ w, _6 Z% ]
host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);+ D# W7 d; F& P& c5 C
host.Open();& o' X* j; i# G8 k
使用内部WCF服务1 B4 s* h; L* o/ j
我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:3 r; ~. \4 |$ k
protected void Button1_Click(object sender, EventArgs e)
$ {* [) g: q- t3 ~( `+ l {& Y" H/ E# R) J! B0 X, B$ D. {
var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
* v% o! i* d( A var channel = factory.CreateChannel(GetRandomEndpoint());1 D, u7 |5 m" i( D: Q6 u) |
Label1.Text = channel.Echo(TextBox1.Text);
( {$ W7 _. e# Y% x; Y }
# {! Y d& `$ F: N; d# E$ l" X private EndpointAddress GetRandomEndpoint()- ^6 B+ S' a3 m9 F; ]
{& ^2 y% `" Q' P9 t, U7 ^
var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances% Y4 ^% @! d; J2 b2 J0 h* g
.Select(i =》 i.InstanceEndpoints[“EchoService”])
4 r1 u$ D2 }! m# w& w .ToArray();, j- {( k( H& J' z0 l( Q1 T
var r = new Random(DateTime.Now.Millisecond);
' a1 t5 h: \- q+ I, i: d return new EndpointAddress(
4 k g+ }! X6 _7 q$ w String.Format(
2 \5 }/ i @) R2 e8 h4 J “net.tcp://{0}/echo”,
' z% A* V) Z7 K7 e1 }0 o$ ? endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)
( a; |1 n8 q) m6 y );) ]; {5 _& S! S# _; Z `
}
7 y' Y6 D3 e7 D( Z- t% p G) D 这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。
4 P1 B0 U# u" b+ E( ~ [* w 我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。
) X5 H: x* Y; j$ ^7 q, |* \ 托管公共WCF服务
' v- M5 ?( D$ a" P3 g 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。
, t: U; D3 d: m. G+ K |