简体   繁体   English

如何在azure上以编程方式托管wcf服务

[英]How to host wcf service programmatically on azure

I want to host a wcf service on Azure, but this instantiation must be dynamic, so I want to instantiate new services as needed however... 我想在Azure上托管一个wcf服务,但是这个实例化必须是动态的,所以我想根据需要实例化新服务但是......

new ServiceHost(new Service(),<<What the heck is the base URI!?>>)

What's supposed to be the base Uri (Scheme, servername & port) on: 什么应该是基础Uri(方案,服务器名称和端口):

  1. A worker role 工人角色
  2. A web role 一个网络角色
    • External endpoint 外部端点
    • Internal endpoint. 内部终点。 (Some services need to talk to each other behind the load balancer for performance reasons, but how?) (出于性能原因,有些服务需要在负载均衡器后面互相交谈,但是如何?)

Also are these possible: 这些也是可能的:

  1. More than one servicehost per web role. 每个Web角色有多个服务主机。
  2. Varying endpoint binding ie. 不同的端点绑定即。 I want one servicehost on Http, another Net.tcp if so will I need to declare both protocols in the csdef file at deploy time, or can I add them as needed programmatically (aka. late bind)? 我想在Http上使用一个servicehost,另一个Net.tcp如果需要,我需要在部署时在csdef文件中声明这两个协议,还是可以按需编程添加它们(又名。后期绑定)?

I am looking for a solution that doesn't involve ServiceBus for $$$ reasons. 我正在寻找一个解决方案,因为$$$原因不涉及ServiceBus。

The approach would be the same, whether on Web Role or Worker Role instances, since they're both essentially Windows 2008 Server (just that Web Roles have IIS running, which also consumes a few ports). 无论是在Web角色实例还是工作者角色实例上,这种方法都是相同的,因为它们本质上都是Windows 2008 Server(只是Web角色有IIS运行,它也消耗了几个端口)。 Whichever port you want to hang your wcf services on, just define these as Input Endpoints (one endpoint per port), and also decide which role handles that endpoint. 无论您想要挂起wcf服务的端口,只需将它们定义为输入端点(每个端口一个端点),并确定哪个角色处理该端点。

As long as you have ports available, you can have multiple ServiceHosts. 只要您有可用的端口,就可以拥有多个ServiceHost。 You're currently limited to 25 total Input Endpoints and 25 total Internal Endpoints per deployment, so this is your absolute limit. 您目前限制为每个部署总共25个输入端点和25个内部端点,因此这是您的绝对限制。 Of course, if you enable RDP, the available port count drops. 当然,如果启用RDP,则可用端口数会下降。 Oh: regarding protocols: If you wanted both http and tcp, you'd need to define two endpoints, as the protocol is defined with the Endpoint definition. 哦:关于协议:如果你想要http和tcp,你需要定义两个端点,因为协议是用端点定义定义的。

Internal Endpoint WCF Services are pretty much identical, but you can do away with security and go with net.tcp for fast transfer. 内部端点WCF服务几乎完全相同,但您可以取消安全性并使用net.tcp进行快速传输。 One difference in load-balancing though: 负载平衡的一个区别是:

  • A WCF service hanging on an Input Endpoint will be load-balanced across all of a role's instance 挂在输入端点上的WCF服务将在角色的所有实例之间进行负载平衡
  • A WCF service hanging on an Internal Endpoint will not be load-balanced. 挂在内部端点上的WCF服务将不会进行负载平衡。

For the latter case: Let's say your Web Role needs to talk to the Worker Role's WCF service on Internal endpoint. 对于后一种情况:假设您的Web角色需要与内部端点上的Worker Role的WCF服务进行通信。 You'd need to enumerate all instances, get the IP+port of each, then select one at random (or round-robin, or whatever method you choose). 您需要枚举所有实例,获取每个实例的IP +端口,然后随机选择一个(或循环,或您选择的任何方法)。 Here's a sample method that returns a random endpoint instance from a given role and given endpoint name (code borrowed from Michael Washam's blog ): 这是一个示例方法,它返回给定角色的随机端点实例和给定的端点名称(从Michael Washam的博客借来的代码):

private String GetRandomServiceIP(String roleName, String endPointName)
{
    var endpoints = RoleEnvironment.Roles[roleName].Instances.Select(i => i.InstanceEndpoints[endPointName]).ToArray();
    var r = new Random(DateTime.Now.Millisecond);
    int ipIndex = r.Next(endpoints.Count());
    return endpoints[ipIndex].IPEndpoint.Address.ToString();
}

As far as setting up the WCF service and related URI, I'd strongly suggest grabbing the latest Windows Azure Training Kit and walking through the Worker Role Communication hands-on lab, which goes into lots of detail about setting up a ServiceHost, with both Input Endpoints and Internal Endpoints. 至于设置WCF服务和相关的URI,我强烈建议抓住最新的Windows Azure培训套件并走过工作者角色通信动手实验室,该实验室详细介绍了如何设置ServiceHost,输入端点和内部端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM