本帖最后由 天若有情 于 2014-6-16 14:02 编辑
9 u9 M5 C$ ?$ {+ ~
) ]8 L) D$ p: v) {( t5 t 本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。
6 F" b/ t; f6 E f) a( X 托管内部WCF服务
. p: Q5 E: f; j6 b& A 其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
* j3 ~% i9 _) t public override bool OnStart(), C# P' ?7 X7 Z6 w+ l
{& j- Q* q Z( O9 x/ b# ^+ T( c
// 设置最大并发连接数. m: b9 F4 Z$ J3 `( y6 g$ n
ServicePointManager.DefaultConnectionLimit = 12;, d% m4 S) T0 w9 N% H8 }) u3 @+ R, A
DiagnosticMonitor.Start(“DiagnosticsConnectionString”);
5 [5 f9 c* J0 |3 l9 ~ Y8 ` // For information on handling configuration changes* D) ^5 i+ @5 S7 F7 b0 v
// see the MSDN topic at =166357.5 f2 x8 O- `7 U0 s
RoleEnvironment.Changing += RoleEnvironmentChanging;
' @' J* @1 w1 [ o" f4 j7 X' ? StartWCFService();
% b( Z7 [; e2 ` return base.OnStart();
! h$ c2 H& G1 E$ z# o }
2 M# j1 Y9 @$ W3 }1 S private void StartWCFService()
# Z* {( g; ~7 h0 ^; N H1 U+ U7 U; J9 T {; p1 k7 F6 h2 O* I+ G: O4 U4 v
var baseAddress = String.Format(
+ l) F. M9 R$ ~1 L) c/ o5 o “net.tcp://{0}”,# k$ W4 t# D4 v* d/ ^; M$ c( z! @; E
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint
0 G0 L% x5 t6 e2 m* j9 `# V );
# L) `8 w7 C% ~ var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));
" \1 E1 r/ v; l9 p host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
# v& _3 `; m9 `; u2 S host.Open();" W- ?/ f! V9 s
使用内部WCF服务' z5 I, {7 s# A$ X( b& B. z" B
我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:+ x* e# N: g2 |2 Y& p
protected void Button1_Click(object sender, EventArgs e)
3 }9 ^& U8 X. R6 v0 f {0 r: c I. Y3 ?$ c
var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));, B$ m4 O% ]1 G3 N
var channel = factory.CreateChannel(GetRandomEndpoint());
" Q. N+ P% M/ x( B Label1.Text = channel.Echo(TextBox1.Text);
. W) H( ~/ M& V& L: L! q }
( d, R% i' Q" p) O" C; M. a6 [ private EndpointAddress GetRandomEndpoint()
) O3 W6 a+ P0 d5 t: p" n; b {9 g; x* V5 L6 w* r/ S' a! a7 V
var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances" e0 Z8 b) {7 i" K: G. \: a: [
.Select(i =》 i.InstanceEndpoints[“EchoService”])! n' S$ x( r. e6 M
.ToArray();
1 Y" q+ V2 n+ t var r = new Random(DateTime.Now.Millisecond);
8 r6 ?1 ]* m5 L9 U return new EndpointAddress(+ e( W+ g) r' r+ {2 |
String.Format(
_' b* A* ]' c v# ~9 |, ~ “net.tcp://{0}/echo”,
1 j3 l* j- U U endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)
7 ?3 P+ l4 F0 a$ i% }: c, [ );
) m. G( g: Y5 T2 r* q4 n$ j }! S. o5 j4 e) j- u' c* c2 s
这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。; S m4 n; Y% U) o- q4 E/ e! R
我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。
$ }8 `* X; K G6 x+ X 托管公共WCF服务
9 |" F2 y+ G8 S- D' V2 g; ` 托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。. }9 j4 w% g5 F- F- K
|