How to host a wcf service programmatically on azure

I want to host a wcf service on Azure, but this instance must be dynamic, so I want to create new services as needed, however ...

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

What should be the base Uri (Schema, server name and port):

  • Working role
  • Web role
    • External endpoint
    • The internal endpoint. (Some services need to talk to each other behind a load balancer for performance reasons, but how?)

The following options are also possible:

  • More than one service for each web role.
  • Changing the binding of endpoints, i.e. I want one service host on Http, another Net.tcp, if so I need to declare both protocols in the csdef file during deployment, or can I add them as needed programmatically (for example, late bind)?

I am looking for a solution that does not require a ServiceBus for $$$ reasons.

+7
source share
1 answer

The approach will be the same, whether in the role of a web role or a working role, since both of them are essentially Windows 2008 Server (only that IIS works in the role of the Web, which also consume several ports). Whichever port you want to hang your wcf services for, just define them as input endpoints (one endpoint per port), and also determine what role this endpoint handles.

As long as you have available ports, you can have several ServiceHost. It is currently limited to 25 input endpoints and 25 full internal deployment endpoints, so this is your absolute limit. Of course, if you enable RDP, the number of available ports will drop. A: regarding protocols: if you need both http and tcp, you will need to define two endpoints, because the protocol is defined with endpoint definition.

The internal endpoint of WCF Services is pretty much identical, but you can get rid of security and go with net.tcp for a quick transfer. One difference in load balancing:

  • WCF service hanging on the input endpoint will be load balanced across the entire instance of the role
  • The WCF service hanging on the internal endpoint will not be load balanced.

In the latter case: let's say your web role should talk to the WCF service of the worker role role on the internal endpoint. You will need to list all the instances, get the IP + port of each of them, and then choose one randomly (or cyclic or any other method that you choose). Here is an example method that returns a random endpoint instance from a given role and a given endpoint name (code borrowed from the Michael Washam 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(); } 

Regarding configuring the WCF service and its associated URI, I would strongly suggest grabbing the latest Windows Azure Training Kit and going through the Worker role , which details how to configure the ServiceHost with input endpoints and internal endpoints.

+4
source

All Articles