Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Created June 6, 2014 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanread/6cf93cf3cf4ba3100f08 to your computer and use it in GitHub Desktop.
Save jonathanread/6cf93cf3cf4ba3100f08 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using Telerik.Sitefinity.Cloud.WindowsAzure;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Services;
namespace SitefinityWebApp
{
/// <summary>
/// Sitefinity's Windows Azure role entry point.
/// </summary>
/// <remarks>
/// Windows Azure looks for RoleEntryPoint inheritors only in the web role assembly and that is why this class is defined here.
/// The functionality goes into the base class - <see cref="AzureWebRoleBase"/>.
/// </remarks>
public class AzureWebRole : AzureWebRoleBase
{
private List<string> _tempUrls = new List<string>();
private int _count = 0;
private bool _isModified = false;
private List<string> SitefinityInternalEndpointIPs
{
get
{
var ips = new List<string>();
var currentRole = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.Roles["SitefinityWebApp"];
if (currentRole != null)
{
foreach (var instance in currentRole.Instances)
{
var internalEndpoint = instance.InstanceEndpoints["SitefinityInternalEndpoint"];
if (internalEndpoint != null)
{
ips.Add(internalEndpoint.IPEndpoint.Address.ToString());
}
}
}
return ips;
}
}
private List<string> ServerUrls
{
get
{
LoadBalancingConfig lbConfig = Config.Get<SystemConfig>().LoadBalancingConfig;
var urls = lbConfig.URLS;
foreach (var u in urls)
{
_tempUrls.Add(u.Value);
}
_count = _tempUrls.Count;
return _tempUrls;
}
set
{
_tempUrls = value;
}
}
public override bool OnStart()
{
foreach (string i in SitefinityInternalEndpointIPs)
{
if (SitefinityInternalEndpointIPs.Count >= 2 && ServerUrls != null && ServerUrls.Count >= 2)
{
if (ServerUrls.Contains(i))
{
continue;
}
else
{
_tempUrls.Add(i);
_isModified = true;
}
}
}
if (_tempUrls.Count > _count || _isModified)
{
ConfigManager manager = Config.GetManager();
SystemConfig section = manager.GetSection<SystemConfig>();
//section.LoadBalancingConfig.URLS = ;
//manager.SaveSection(section);
}
return base.OnStart();
}
public override void OnStop()
{
List<string> azureIps = SitefinityInternalEndpointIPs;
base.OnStop();
}
}
}
@nicholas-balcolm
Copy link

Hey Jon. I think the problem with this is that Sitefinity hasn't yet bootstrapped when OnStart() is called, so the Config.Get() is failing. I think what we need to do is let the Global.asax handle the case where a new role instance is coming up. Hopefully the OnStop() will be able to access the configs... I'm not sure there would be another way to update when the role stops, besides just updating the configs in a recurring task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment