Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using EPiServer.Web;
namespace MyAlloy.Business.Initialization
{
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class EnvironmentInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
if (ConfigurationManager.ConnectionStrings["EPiServerDB"].ConnectionString.Contains("LocalDb")) //making sure we are on a local dev machine
{
//Setup local development site definitions
MergeSiteDefinitions(GetLocalDevSites());
}
}
private void MergeSiteDefinitions(IEnumerable<SiteDefinition> mergeWithDefinitions)
{
var siteDefinitionRepository = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>();
var existingSites = siteDefinitionRepository.List().ToList();
foreach (var definition in mergeWithDefinitions)
{
//Update the site definition if it doesn't have the same value for SiteUrl
var site = existingSites.FirstOrDefault(s => s.Name == definition.Name && s.SiteUrl != definition.SiteUrl);
if (site != null)
{
site = site.CreateWritableClone();
site.SiteUrl = definition.SiteUrl;
site.Hosts = definition.Hosts;
siteDefinitionRepository.Save(site);
}
}
}
private IEnumerable<SiteDefinition> GetLocalDevSites()
{
//These are the site definitions we want to use on local development machine
var sites = new List<SiteDefinition>
{
new SiteDefinition
{
Name = "Alloy1",
SiteUrl = new Uri("http://localhost:53469/"),
Hosts = new List<HostDefinition> { new HostDefinition { Name = "localhost:53469", Language = new CultureInfo("en") } }
},
new SiteDefinition
{
Name = "Alloy2",
SiteUrl = new Uri("http://alloy2:53469/"),
Hosts = new List<HostDefinition> { new HostDefinition { Name = "alloy2:53469", Language = new CultureInfo("en") } }
},
};
return sites;
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment