本帖最后由 天若有情 于 2014-6-16 14:02 编辑
; \* y) n' M& v. G% O( @
1 O1 O, R {: ] 本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。* W4 J! H) ?7 Q
托管内部WCF服务) _5 `- w* v/ z. d2 o, u
其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
4 s7 N7 ]+ Q3 f% s; S. q public override bool OnStart()6 a: }5 O+ Y' s% C: `
{( \7 B$ J$ X Q5 T
// 设置最大并发连接数
( J% h! K3 v/ `+ Y5 Y& M ServicePointManager.DefaultConnectionLimit = 12;! k9 o1 _5 _/ L! I! L0 M( [! Q
DiagnosticMonitor.Start(“DiagnosticsConnectionString”);7 z: N4 x; ~, f% |7 ?
// For information on handling configuration changes+ K7 P2 @/ N/ Q+ Z# X
// see the MSDN topic at =166357.
4 {& Y2 O+ h5 E RoleEnvironment.Changing += RoleEnvironmentChanging;8 u, l# J8 n, c- Y" A2 I. C0 ]
StartWCFService();* a+ Y7 L2 X) J% j: X2 I& [( Q
return base.OnStart();
) j( k* \3 f/ M6 M: s$ } \; N }
! E; [1 O5 y5 F" |( h K private void StartWCFService()
* h/ }. Y) p" V' A4 w* _/ L {
5 O) G# ~; ~; h, W" M var baseAddress = String.Format(
O' ^ |/ R: b7 A% D0 S6 h& C6 | “net.tcp://{0}”,
( @# f6 d/ B" M" m& h; J- i RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint
9 L* q. p, o* V5 I$ J! v );# R: M2 f8 Q& n, t( }: N
var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));
# p M6 f8 s$ S0 V8 x& l host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
( u1 I& \8 ? I( W host.Open();
+ k4 y. U$ `* [9 y, l& N4 L 使用内部WCF服务1 ]( a, Y/ m) g/ @. j
我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:
9 h. l9 J) E) @9 f protected void Button1_Click(object sender, EventArgs e) F. E, v) W; `8 K3 M$ q
{
, W! @* i0 ~7 v/ q/ l3 f8 M: B var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
! }: ~+ d: H( {- g. H; E9 A var channel = factory.CreateChannel(GetRandomEndpoint());
/ k9 y' Y8 V" z" B2 B Label1.Text = channel.Echo(TextBox1.Text);
/ t( Y; Z% R, c }
9 s) F. a8 c2 t0 B private EndpointAddress GetRandomEndpoint()
& r9 i7 R' c! N0 K: _ {! L) G3 s' I; c }0 ?
var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances$ l+ y& [1 B' s' {6 F
.Select(i =》 i.InstanceEndpoints[“EchoService”])+ t" F' l" K7 J$ _) e7 Y+ a* z$ h9 [
.ToArray();
, J( j6 N6 S5 D var r = new Random(DateTime.Now.Millisecond);; n2 s3 N: T3 @4 E7 {
return new EndpointAddress($ [! o/ a" H+ H3 g
String.Format(2 C: A1 r9 j7 A3 n0 Z0 R7 e' V
“net.tcp://{0}/echo”,# Z$ t, E; h6 i( P x
endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)
9 x" Q7 m, N# |: @8 ? );
$ E P# n% R' K. }5 h0 _* L3 Y }
4 V7 U6 Q% s7 u" X0 z 这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。5 ] s6 X$ ]% _' B& `* T
我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。$ e1 u3 A) {% K* |3 z9 v) b6 {% Y1 S
托管公共WCF服务
! V1 s" I1 P# V( A 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。
' v! N4 q7 ], s; I8 z9 L |