本帖最后由 天若有情 于 2014-6-16 14:02 编辑 # J3 ?- ?0 Z/ [' V
7 |% ?8 @7 A8 Y; a, q- ~0 j( m
本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。 o/ U1 O; {4 i8 w% m/ l- p1 |
托管内部WCF服务; I8 c; b# U" P# f% {
其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
$ d5 |, P; A8 I+ F \7 P4 [ public override bool OnStart()
5 T+ a5 P2 W% d, g4 ~* Q; X {8 l$ C' h; x$ g# w r. K8 `
// 设置最大并发连接数
; ?( h; F7 [& K/ X6 P5 N; l; k ServicePointManager.DefaultConnectionLimit = 12;9 ]! B4 c* f" A$ ^
DiagnosticMonitor.Start(“DiagnosticsConnectionString”);
" x3 ?; q, Z) I5 Y- s7 r q // For information on handling configuration changes2 y, y% |4 k3 v3 o: g) r- t" g& y
// see the MSDN topic at =166357.
3 a5 u* p6 V# O: _" U RoleEnvironment.Changing += RoleEnvironmentChanging;1 d9 y6 z2 P" @
StartWCFService();; V8 f: J8 y0 d% `! Q: L, l$ j* m' M
return base.OnStart();6 H1 ^- R( U3 `2 u
}9 r* P$ l% T* W5 Q: V& ~
private void StartWCFService()' A& H: @3 ~0 B; l* d
{, A: C/ G( _4 X. c
var baseAddress = String.Format(
( p) F6 W" `% A7 ^* ?* e6 q “net.tcp://{0}”,
' S+ R# D8 ^5 v RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint
) p0 ?' ^& I" R% k1 r0 ]2 ? );9 X+ o9 g: H& n/ M3 ^; b
var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));
5 q" \9 c% l7 n* v host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
& G; u% t h) D$ z: _8 i/ ^ host.Open();
' t* `- Y+ p( c& r9 [( d8 ~ 使用内部WCF服务# H9 D0 F- G; X& t* {% j
我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:+ y9 m5 Q l& n1 ?7 s& \
protected void Button1_Click(object sender, EventArgs e)$ h! |7 P) u$ h* y! L; X4 y
{' {: K" I1 v! s+ B- K: t
var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));6 ?% r/ ]' a" S( t- t, U8 F* `
var channel = factory.CreateChannel(GetRandomEndpoint());
- s5 z( l9 _ _' U7 d Label1.Text = channel.Echo(TextBox1.Text);
, E- x# ^8 `7 t$ X- L* j% d' \ N }
' E5 t3 S/ Q( T private EndpointAddress GetRandomEndpoint()
" a# t6 R1 z, L! X# T# I {
9 z! [$ \$ t/ j7 m var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances
! c- R6 a3 t: r1 m .Select(i =》 i.InstanceEndpoints[“EchoService”])
1 U- b2 E; i6 H .ToArray();; H! T) O1 Z$ M0 E. ~. C
var r = new Random(DateTime.Now.Millisecond);/ R9 @! ^3 ~' Z/ N7 C7 U
return new EndpointAddress(
. R1 w) X) m0 U" T2 Z) B String.Format(
& a! w" O$ x# s8 j “net.tcp://{0}/echo”,, P5 O6 Y8 ]- f$ P! Y% v8 a
endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)# u( k+ J( A) Z: \
);
( e! k7 x0 ]+ I1 `( U5 r { }1 W- {, o' j" V% ^, k! ]1 A% M- Z- Q
这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。
' F1 F/ W" ~0 I* H$ G6 P 我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。
; [7 w5 d- F" B9 u) o 托管公共WCF服务
% u6 w* P3 @* }# n# u: J# K; t 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。$ Q! t2 B) A, Z
|