电脑互动吧

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

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

[复制链接]
发表于 2014-6-16 14:00:45 | 显示全部楼层 |阅读模式
本帖最后由 天若有情 于 2014-6-16 14:02 编辑
7 C. N2 }. T& z! m6 w! ^3 x' ^
* t/ L$ {+ D: t( }, h$ r$ E  本文将向大家介绍如何托管内部WCF服务和公共WCF服务,为了托管内部WCF服务,需要建立一个内部端点,并使用内部角色通信,与在输入端点上托管一个外部服务最主要的区别是内部端点不具有负载均衡特性,而输入端点是挂钩在负载均衡器上的,具有负载均衡功能。
  T2 h1 T7 }% g$ G& K( C7 D! Y  托管内部WCF服务
; M; f6 U7 q" N2 Z* M  其实要托管一个内部WCF服务很简单,唯一需要注意的是传递给 ServiceHost 的基地址不同,因为端口号和IP地址要等到运行时才知道,因此需要创建一个主机,动态地传递这些信息给它。
" M$ o! X6 K( v7 s4 u  public override bool OnStart(); |( E6 L8 t+ ?$ F0 I
  {6 ]: L$ c. S6 @( e9 I7 b- i
  // 设置最大并发连接数
0 y$ o6 F3 ?: Y# q  ServicePointManager.DefaultConnectionLimit = 12;  y# ]: u" d8 a$ M- T
  DiagnosticMonitor.Start(“DiagnosticsConnectionString”);
4 V. Y+ A$ I% ^7 l# }! @) M/ Z6 k  // For information on handling configuration changes# V) b- h- @6 Y0 m$ p6 h5 x
  // see the MSDN topic at =166357.+ R$ u8 I% I# j/ f
  RoleEnvironment.Changing += RoleEnvironmentChanging;  ~  J1 P5 {) R
  StartWCFService();1 Z: E, N+ B+ ?
  return base.OnStart();
) [* l  S) _/ K+ q6 u  }
7 g5 ~7 V& l8 ]7 q$ ?  private void StartWCFService()- S* U  _3 h0 i1 N; p( J
  {
  c! C" c. g3 j  var baseAddress = String.Format(
9 Q% V  A4 R9 E0 _  “net.tcp://{0}”,& S: z1 J/ V+ o8 Z# l9 ]/ B
  RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[雨林木风系统“EchoService”].IPEndpoint
7 d$ T9 ]  E# d) S  );
$ H: x$ g( I0 D+ U  Q; M  var host = new ServiceHost(typeof(EchoService), new Uri(baseAddress));9 }7 P# h1 o4 e, Z! i/ d9 D0 _
  host.AddServiceEndpoint(typeof(IEchoService), new NetTcpBinding(SecurityMode.None), “echo”);
/ ?8 j& C- ^" @% e5 v  host.Open();! g4 p! A4 e) I& Q9 A
  使用内部WCF服务
  V( w. J: f( n$ u, W/ J  我想从我另一个托管的服务调用这个服务,下面就是调用这个服务的所有代码:
9 h2 W  E  _8 D7 d3 I( I1 @' L6 Y  protected void Button1_Click(object sender, EventArgs e)/ H% X+ J& O2 u. Z/ v! v0 B( ?
  {
+ {% o' M. j& o) j  var factory = new ChannelFactory(new NetTcpBinding(SecurityMode.None));
- i# Y- Q: n  Y4 {% s! b  var channel = factory.CreateChannel(GetRandomEndpoint());
& s& g' |) o' u) W4 U) Z: Y  Label1.Text = channel.Echo(TextBox1.Text);
( z! W0 |% ?/ V4 K7 Z5 c& l) ]  }( E' a" L  f4 G/ ]
  private EndpointAddress GetRandomEndpoint()
9 S- f7 V* J+ v5 F  {/ g  |1 t# v% b6 r( M" X$ N1 V
  var endpoints = RoleEnvironment.Roles[“WorkerHost”].Instances
- k3 b4 O+ u8 \3 J/ c: o  .Select(i =》 i.InstanceEndpoints[“EchoService”]). Y+ Y7 l# M5 m; }6 S5 g
  .ToArray();! G+ Y+ s+ ?. A5 Z1 o! d
  var r = new Random(DateTime.Now.Millisecond);
+ X* g8 x. f+ K8 @/ M/ S" b  return new EndpointAddress(/ }5 ^! ?! C3 I0 ]8 O' Q7 m
  String.Format(
2 t% q+ _3 u  A  “net.tcp://{0}/echo”,- S( \2 s$ V7 D  c) `
  endpoints[r.Next(endpoints.Count() - 1)].IPEndpoint)3 `5 ^6 p0 Z+ T+ _4 x9 a
  );
% u. N* `, E  ]9 J% `; W  }' ]$ ~5 r. @; l$ p
  这里唯一要注意的是查询F abric ,确定 WorkerHost 角色中实现了 EchoService 端点,深度系统官网并随机给它们路由请求的所有端点,本来不需要路由请求,我这样做是因为内部端点没有负载均衡功能,我希望在每个 WorkerHost 实例上均匀地分配负载。' b- ~! ?+ d) b" @5 B" J$ N; A
  我发现一个技巧,就是不需要缓存你找到的 IPEndpoint ,因为它已经缓存在API调用中,但根据最佳实践,你应该缓存你的 ChannelFactory 。6 G- b: N& Q2 e
  托管公共WCF服务1 a( e6 r+ `1 m- q
  托管公共WCF服务也很简单,唯一需要注意的是要使用一个新的行为,为MEX端点处理负载均衡,此外,在你的服务上需要包括一个类属性处理地址过滤不匹配问题。- y# F2 T, P4 s0 t+ ?0 K& S3 U
回复

使用道具 举报

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

本版积分规则

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

浙公网安备 33032402001025号

GMT+8, 2025-12-8 17:15 , Processed in 0.102257 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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