本帖最后由 天若有情 于 2014-6-16 14:02 编辑 , y& \1 e! H$ U- E# d7 H
8 w5 P) {5 ]2 Z" [& C* m 本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。: e, A4 l v; u
托管内部WCF服务! H* \! z& d4 T$ \5 Y/ L4 n
其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。4 |0 k& k: q: i. r0 V# S% q
public override bool OnStart()! P/ b) \" s& s6 ^1 q
{
" x6 V4 s& i ?9 n* j // 设置最大并发连接数
. P1 y! E1 I- D/ K ServicePointManager.DefaultConnectionLimit = 12;
: U' R0 e3 u, M# F! ~6 y+ k DiagnosticMonitor.Start(“DiagnosticsConnectionString”);9 }# W) {, z8 ^/ u$ t* g* q
// For information on handling configuration changes
$ h3 ^/ e% z: R: V* i1 t. a // see the MSDN topic at =166357.
, l3 x$ r6 \) n" x b/ h5 M& m: ^7 y RoleEnvironment.Changing += RoleEnvironmentChanging;' N% ]. V. ~1 F$ p- z1 O& }
StartWCFService();
0 l' C& k5 i6 Y( }% } return base.OnStart();
) x3 Z8 }* j6 ^4 A$ a' R }- E0 C9 `4 {5 Y/ m" k {1 S2 x. f
private void StartWCFService()
& O) L7 `! F( \1 h {
{! u4 r6 V: K$ ~ var baseAddress = String.Format(7 w3 g% k1 [( ]+ G) s( j7 b
“net.tcp://{0}”,$ W; F6 a# B+ [8 T L% Z( X& Q0 m
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint
2 \! s9 J( l$ ] );
# l L" Z* [8 b; A var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));7 m0 `0 o! N1 F3 i& H5 ]% `4 v3 C0 t
host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
- B* }# }2 k3 y6 D% M3 M host.Open();$ \$ ]' C1 y2 U7 w& x4 ^
使用内部WCF服务
; Y2 q5 G* n# ] 我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:
7 W# R# H3 q' W% i protected void Button1_Click(object sender, EventArgs e)
: s- c6 C8 ]8 L* t l: X$ o+ D7 H {& y3 Y* U5 ~$ O) j1 ~
var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
' x1 K8 }4 `- {3 \9 w var channel = factory.CreateChannel(GetRandomEndpoint());4 q) Y3 M2 o5 t0 [
Label1.Text = channel.Echo(TextBox1.Text);; ?' t" R7 v2 c0 w/ a6 F) L7 m. z
}
! Z. c" m- U0 j/ S private EndpointAddress GetRandomEndpoint()
4 f% p$ Q( T3 C X5 y5 H+ J6 P+ l {
* ~6 s2 x2 }1 G4 } var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances
/ O) A; t" Z0 A6 k .Select(i =》 i.InstanceEndpoints[“EchoService”])
+ _' \/ Q9 G0 h, I) \8 d# e: Z; d .ToArray(); y* T9 [: I- Z( ^7 d
var r = new Random(DateTime.Now.Millisecond);
$ _* y) n/ R+ ]2 G return new EndpointAddress(. U" P2 c" x2 n
String.Format(/ w0 b+ e* G6 V# M+ \2 Q0 Q
“net.tcp://{0}/echo”,% T. Y x+ `- C* D5 }( P! ?
endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)0 e5 I0 }; U& g1 R' `3 B" e
);
, \/ P: d6 D! e# t3 J' Q }
3 }8 s5 y0 Q) t7 @1 ~! x 这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。2 H. q. g3 n% k+ S9 X
我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。$ s- m# \( ?; I
托管公共WCF服务
; L s) Q" t: F. ? 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。
8 A, X* D. H) ^0 @5 Q* L |