Note: The approach outlined below currently only works with Internal endpoints, not Input endpoints.
Anyway, let’s hop over to the actual problem I ran into the other day. My goal was to self-host some of my WCF services with HTTP-based endpoints in a Windows Azure worker role – should be easy, eh?
Turned out that it was easy. Once you consider using the Azure API to get the actual endpoint to listen on (which obviously you need to specify/model in the service’s .csdef file), like this:
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Threading;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Services;
namespace WCFWorkerRole
{
public class WorkerRole : RoleEntryPoint
{
private ServiceHost host;
public override void Run()
{
Trace.WriteLine("WCFWorkerRole entry point called", "Information");
while (true)
{
Thread.Sleep(10000);
Trace.WriteLine("Working", "Information");
}
}
public override bool OnStart()
{
ServicePointManager.DefaultConnectionLimit = 12;
DiagnosticMonitor.Start("DiagnosticsConnectionString");
StartWCFService();
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
public override void OnStop()
{
StopWCFService();
base.OnStop();
}
private void StartWCFService()
{
IPEndPoint ip = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[
Uri baseAddress = new Uri(String.Format("http://{0}", ip));