本帖最后由 天若有情 于 2014-6-16 14:02 编辑 0 r; M8 q' _' _" P6 \4 F
0 F+ O, q; i* X+ [
本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。5 o1 H- B& d5 O8 U+ U+ O
托管内部WCF服务
& P6 t# Y% N1 ~( U 其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。% ?) |( H& B @# b* Q" Q5 a
public override bool OnStart(), A3 S! b3 {% b5 t0 J- ^+ |
{
+ b5 q" ?3 q/ }% o/ I // 设置最大并发连接数- l; t+ ^8 h! y# C ]3 J1 S7 N
ServicePointManager.DefaultConnectionLimit = 12;
. M. i- {8 j+ u) a" B+ a7 {8 V DiagnosticMonitor.Start(“DiagnosticsConnectionString”);
) Y6 Y* C; s$ S! ~$ g6 V. J+ z // For information on handling configuration changes
$ l3 p) n; X" z6 \, O // see the MSDN topic at =166357.9 m* [# q% e. j( ]/ q
RoleEnvironment.Changing += RoleEnvironmentChanging;) [' N8 c/ t. o5 {( I+ J
StartWCFService();0 }, w5 D# @+ x' e/ B" A
return base.OnStart();
# w% F a7 c: e: n% e }
- A* m0 L7 E% t private void StartWCFService()
( V5 h0 j9 |5 P" D7 d, r- S {" t( V1 j$ ~8 b s, y8 H
var baseAddress = String.Format(7 E0 @1 a- _: g \0 k
“net.tcp://{0}”,
" e0 I5 x# @7 U2 X! U# v* s# Z RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint
4 B$ B4 T" L4 ^- {3 O% J$ P );
0 W1 @# D4 b0 t8 |( v var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));
3 Y* x2 W+ C: k3 C host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
2 c; N3 J0 A# }. U3 t host.Open();! \: \& r* }1 s/ J
使用内部WCF服务
h3 s# ^* d# E* E. |: g& J 我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:$ |5 X" H# a7 \# S1 B- L
protected void Button1_Click(object sender, EventArgs e)
) l" I! T7 D) L! m {
7 u3 l& T: Y/ H: w6 [! w3 s9 F var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
?2 R% D; C1 h, g+ i var channel = factory.CreateChannel(GetRandomEndpoint());
+ }9 y8 g: w, Z+ H' c: h Label1.Text = channel.Echo(TextBox1.Text);
1 U9 y+ g: S! b$ y }' s" q4 E& Y, K, `& U
private EndpointAddress GetRandomEndpoint()
% V% _! |1 E9 V3 y9 n {- E5 Y4 y8 U# R) ?
var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances0 H f, I. l3 l: m# h
.Select(i =》 i.InstanceEndpoints[“EchoService”])
6 r2 N; Y. r, T+ p .ToArray();
1 R4 C8 m8 |; b4 | var r = new Random(DateTime.Now.Millisecond);/ U. w+ h6 [3 ~: X* ^# S
return new EndpointAddress(7 d" F/ G7 S/ M" J" X
String.Format(
# A3 F% h, S* l* X2 A" A “net.tcp://{0}/echo”,- |* b/ |/ i: z' m
endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)
3 G; m2 d/ t- c p y) N8 m: J );& y) C4 E2 _1 B# ]: H2 I
}
% a) i% Y6 |' s! K, j4 h 这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。; }0 w3 Z, ~! Y, s3 v
我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。$ N- [3 s! @9 Z- x" y: ]5 [
托管公共WCF服务
& O8 h) E4 b+ G/ ^3 e% L 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。9 I; ]4 P6 D4 p/ |8 a& h) B' `
|