本帖最后由 天若有情 于 2014-6-16 14:02 编辑 2 I) W; a ]4 W3 `: A
% e# j+ j' Z! ]0 z$ p' D
本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。; `/ M% X! E( u: b" J2 o- b
托管内部WCF服务& d3 S; a2 T# A$ K" y/ I
其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
# T( i* L( W! t9 l( ] public override bool OnStart()* r" O d- {! f$ V) }' L% _. K! M
{
7 x* ^6 }' D8 k7 _ { // 设置最大并发连接数9 L* r7 q- z) G9 w
ServicePointManager.DefaultConnectionLimit = 12;6 n! {4 x* `, X+ n
DiagnosticMonitor.Start(“DiagnosticsConnectionString”);! E3 h" V+ R5 m2 {& y5 B4 z3 a
// For information on handling configuration changes6 p x U8 a- q" H: d$ a. h, F& i
// see the MSDN topic at =166357.
2 w* Q" ?: U. s7 @" I2 B RoleEnvironment.Changing += RoleEnvironmentChanging;
% k1 W2 e# s: y" L7 H3 Y7 u& d) h StartWCFService();
( g; G" ?, S% ` return base.OnStart();# f" l9 m) f/ m( O8 B
}, T2 W$ j: _5 N; \
private void StartWCFService()* u7 p( L) @- W/ [- B: Z
{
2 q# X% y1 @4 |2 F8 } var baseAddress = String.Format(! I- I5 _! k, h) P2 `7 s
“net.tcp://{0}”,
$ \5 J8 u: q( p ?6 A! c" h+ L) _ RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint l5 G) s* w i$ a3 S/ N
);5 r( r0 |$ S* b6 V$ Q$ J$ S( I
var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));
; D% P- Q! A- i3 r host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
" b9 N+ S2 n6 L3 V& G host.Open();
& P0 n6 U- h( `: ^9 O; L' I 使用内部WCF服务; B% f" a2 I" S G# L1 C
我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:
( y; O" @# ~3 t2 V protected void Button1_Click(object sender, EventArgs e)
, @0 B# n6 S4 B1 ?5 M {
9 M4 v ^$ d: u& c# E var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));3 J4 t" G+ u# {! S, ~! b+ f( E" |
var channel = factory.CreateChannel(GetRandomEndpoint());1 [0 ^! g- u8 L" i& R
Label1.Text = channel.Echo(TextBox1.Text);5 v" P2 s% {3 r5 [
}/ f) v3 G! g8 n! t
private EndpointAddress GetRandomEndpoint()1 _) l8 M0 q- i& i9 W
{
# L; O7 P$ Y+ E" X; W( \' ?+ d var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances0 i, _/ I8 L1 x: a+ a9 i
.Select(i =》 i.InstanceEndpoints[“EchoService”])8 P0 R6 @0 W6 N% |
.ToArray();
4 U: T V7 m& i" y var r = new Random(DateTime.Now.Millisecond);
' C. a8 ?+ h( W; C return new EndpointAddress(
3 d) h! i, ^- l G2 u String.Format(/ }( u: v$ B) d7 O# V& K2 U
“net.tcp://{0}/echo”,/ L2 l' ?6 T W- d* L. o7 c
endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)
1 v- B/ W: _6 O# A2 o9 G4 O );" `% P0 H2 g" @5 S
}/ P9 W4 {8 ~0 L- C9 `. o. t
这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。+ G% C7 @* B( q9 l& f
我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。" Y& r8 r0 c6 M% a1 L$ w
托管公共WCF服务
' d1 ]8 J0 B R2 F! h; t: X/ K 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。
$ {5 [7 H" q! K) k |