电脑互动吧

 找回密码
 注册
查看: 5929|回复: 0

在Windows Azure中托管WCF服务的方法

[复制链接]
发表于 2014-6-16 14:00:45 | 显示全部楼层 |阅读模式
本帖最后由 天若有情 于 2014-6-16 14:02 编辑 : X# w  c& X+ O2 s2 ]
% }8 M$ A; A3 ]; Z0 f
  本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。5 i9 W2 I# t. |8 u! o
  托管内部WCF服务0 b) {. m3 D) g1 k/ _9 J
  其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。# s% M/ V) U6 L0 {3 z% P, M
  public override bool OnStart()
2 |0 U0 D9 q. n+ c& y( L5 w8 g  {
, j' f1 A0 ]9 L0 [0 H' e: d  // 设置最大并发连接数
- b1 i; ~; f8 ^; s# `- p) D5 k  ServicePointManager.DefaultConnectionLimit = 12;
0 a" m/ v0 c0 p, }: p$ Q; w3 z  DiagnosticMonitor.Start(“DiagnosticsConnectionString”);. o# R7 b0 Z% \! o+ m; w: ]1 [
  // For information on handling configuration changes
8 @  C+ p+ v4 u$ e, s4 C5 ?  // see the MSDN topic at =166357.5 Y- s. u# @) C. ]2 _  t4 s# D9 [
  RoleEnvironment.Changing += RoleEnvironmentChanging;4 ]3 }, R2 ~9 c( ^; h5 L) n+ d( ~8 D
  StartWCFService();
. F% d+ v# G* v" [1 U  return base.OnStart();
& g( h+ }# F6 N# Q4 G- I9 d1 t% y  }% m  l, n1 N9 [2 u* c: L
  private void StartWCFService()" D$ V# @5 L! H" q, B
  {- W6 e0 C  q3 W: z
  var baseAddress = String.Format(
5 n% A- Z6 L* F1 G  “net.tcp://{0}”,
7 I4 P" |) q9 O  RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint) u# L3 S( m+ d1 L/ @0 l4 b
  );
9 W4 y0 [% b& @' G$ N7 A  var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));* z# C3 M0 L+ m, P4 z
  host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
! a' I2 ~0 k, v3 i6 a* J  host.Open();
; ?- F5 p2 p3 l! {' s3 O) V  使用内部WCF服务' Q" Q0 }9 C3 E
  我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:3 c* h+ K" [5 y9 {
  protected void Button1_Click(object sender, EventArgs e)' D  v8 g/ p7 |0 l
  {# L1 V1 M" ^- a* Q5 m$ ~
  var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
3 `+ e2 U$ v! ?6 d' T  var channel = factory.CreateChannel(GetRandomEndpoint());
- b& X8 x1 k* T7 |: y$ B( D# d+ _  Label1.Text = channel.Echo(TextBox1.Text);
3 X) r7 y/ K5 Z% \% S  j  }  x- W6 Z- r9 J# w# ^
  private EndpointAddress GetRandomEndpoint()# V0 @0 {% }% b; Q0 {1 }
  {
+ A* B, F7 V9 g  var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances+ e1 d& Y7 I9 |+ M: A* k
  .Select(i =》 i.InstanceEndpoints[“EchoService”])) g. h5 C! r- y8 p, v% A$ g
  .ToArray();2 Z2 ]" A2 j) a- v
  var r = new Random(DateTime.Now.Millisecond);1 Y3 X5 b1 Q8 E. _8 v
  return new EndpointAddress(# @  t5 W% i7 w. \. l9 Y$ Q
  String.Format(
; K' M- ~  L3 {( f! v0 s5 Y  “net.tcp://{0}/echo”,0 l/ S6 E- E: P" N  \
  endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)9 a& q8 N) m0 W% o: Z" b$ I! Q
  );
7 S* m- q1 l1 @( W+ ~  }, I7 ~" s) _8 d3 v$ f
  这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。
& V- G$ g! j9 M9 u6 H& O6 U! g: [' D  我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。  B; B& y* A( M) o$ ?
  托管公共WCF服务. B, |  z! A7 o9 z, R
  托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。
$ v4 t6 ~) M4 `; _" H( R* g9 A& n/ l5 A
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系我们|手机版|小黑屋|Archiver|电脑互动吧 ( 浙ICP备13037409号 )

浙公网安备 33032402001025号

GMT+8, 2025-12-22 00:12 , Processed in 0.057408 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表