Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to host wcf service programmatically on azure

Tags:

c#

wcf

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.

like image 674
Alwyn Avatar asked May 25 '12 02:05

Alwyn


People also ask

Can WCF be used with Azure?

Azure Relay takes existing WCF web services and makes those services securely accessible to solutions that are outside the corporate perimeter without requiring intrusive changes to the corporate network infrastructure.

What is Azure WCF relay?

The Azure Relay service enables you to securely expose services that run in your corporate network to the public cloud. You can do so without opening a port on your firewall, or making intrusive changes to your corporate network infrastructure.


1 Answers

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.

like image 193
David Makogon Avatar answered Sep 28 '22 05:09

David Makogon