简体   繁体   中英

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...

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

What's supposed to be the base Uri (Scheme, servername & port) on:

  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.
  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)?

I am looking for a solution that doesn't involve ServiceBus for $$$ reasons.

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). 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.

As long as you have ports available, you can have multiple ServiceHosts. You're currently limited to 25 total Input Endpoints and 25 total Internal Endpoints per deployment, so this is your absolute limit. Of course, if you enable RDP, the available port count drops. 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.

Internal Endpoint WCF Services are pretty much identical, but you can do away with security and go with net.tcp for fast transfer. 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
  • A WCF service hanging on an Internal Endpoint will not be load-balanced.

For the latter case: Let's say your Web Role needs to talk to the Worker Role's WCF service on Internal endpoint. 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). 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 ):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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