public static Servant.Business.Objects.Enums.SiteStartResult StartSite(Servant.Business.Objects.Site site) { using (var manager = new ServerManager()) { var iisSite = manager.Sites.SingleOrDefault(x => x.Id == site.IisId); if (iisSite == null) { throw new SiteNotFoundException("Site " + site.Name + " was not found on IIS"); } try { iisSite.Start(); return(SiteStartResult.Started); } catch (Microsoft.Web.Administration.ServerManagerException) { return(SiteStartResult.BindingIsAlreadyInUse); } catch (FileLoadException) { return(SiteStartResult.CannotAccessSitePath); } } }
public static string GetSitename(Servant.Business.Objects.Site site) { if (site == null) { return("Unknown"); } return(site.Name); }
public static void StopSite(Servant.Business.Objects.Site site) { using (var manager = new ServerManager()) { var iisSite = manager.Sites.SingleOrDefault(x => x.Id == site.IisId); if (iisSite == null) { throw new SiteNotFoundException("Site " + site.Name + " was not found on IIS"); } iisSite.Stop(); } }
public static void UpdateSite(Servant.Business.Objects.Site site) { using (var manager = new ServerManager()) { var iisSite = manager.Sites.SingleOrDefault(x => x.Id == site.IisId); var mainApplication = iisSite.Applications.First(); mainApplication.VirtualDirectories[0].PhysicalPath = site.SitePath; // In some scenarios Microsoft.Web.Administation fails to save site if property-set is detected with same name. //I believe it deletes and insert sites on updates and this makes a name conflict. Fixed by the hack below: if (site.Name != iisSite.Name) { iisSite.Name = site.Name; } mainApplication.ApplicationPoolName = site.ApplicationPool; // Commits bindings iisSite.Bindings.Clear(); foreach (var binding in site.Bindings) { if (binding.Protocol == Protocol.https) { var certificate = GetCertificates().Single(x => x.Thumbprint == binding.CertificateThumbprint); iisSite.Bindings.Add(binding.ToIisBindingInformation(), certificate.Hash, "My"); } else { iisSite.Bindings.Add(binding.ToIisBindingInformation(), binding.Protocol.ToString()); } } //Intelligently updates virtual applications foreach (var application in site.Applications) { var iisApp = iisSite.Applications.SingleOrDefault(x => x.Path == application.Path); if (iisApp == null) { if (!application.Path.StartsWith("/")) { application.Path = "/" + application.Path; } iisSite.Applications.Add(application.Path, application.DiskPath); iisApp = iisSite.Applications.Single(x => x.Path == application.Path); } iisApp.VirtualDirectories[0].PhysicalPath = application.DiskPath; iisApp.ApplicationPoolName = application.ApplicationPool; } var applicationsToDelete = iisSite.Applications.Skip(1).Where(x => !site.Applications.Select(a => a.Path).Contains(x.Path)); foreach (var application in applicationsToDelete) { application.Delete(); } manager.CommitChanges(); } }