public static Business.Objects.CreateSiteResult CreateSite(Site site) { var result = new Business.Objects.CreateSiteResult(); var bindingInformations = site.Bindings.Select(x => x.ToIisBindingInformation()).ToList(); // Check bindings var bindingInUse = GetBindingInUse(0, bindingInformations); // 0 never exists if (bindingInUse != null) { result.Result = CreateSiteResult.BindingAlreadyInUse; return(result); } using (var manager = new ServerManager()) { // Create site manager.Sites.Add(site.Name, "http", bindingInformations.First(), site.SitePath); var iisSite = manager.Sites.SingleOrDefault(x => x.Name == site.Name); // Add bindings iisSite.Bindings.Clear(); foreach (var binding in bindingInformations) { iisSite.Bindings.Add(binding, "http"); } // Set/create application pool if (string.IsNullOrWhiteSpace(site.ApplicationPool)) // Auto create application pool { var appPoolName = site.Name; var existingApplicationPoolNames = manager.ApplicationPools.Select(x => x.Name).ToList(); var newNameCount = 1; while (existingApplicationPoolNames.Contains(appPoolName)) { appPoolName = site.Name + "_" + newNameCount; newNameCount++; } manager.ApplicationPools.Add(appPoolName); iisSite.ApplicationDefaults.ApplicationPoolName = appPoolName; } else { iisSite.ApplicationDefaults.ApplicationPoolName = site.ApplicationPool; } manager.CommitChanges(); var created = false; var sw = new Stopwatch(); sw.Start(); while (!created && sw.Elapsed.TotalSeconds < 3) { try { if (iisSite.State == ObjectState.Started || iisSite.State == ObjectState.Stopped) { created = true; } } catch (COMException) { System.Threading.Thread.Sleep(100); } } sw.Stop(); if (created) { result.Result = CreateSiteResult.Success; result.IisSiteId = (int)iisSite.Id; } else { result.Result = CreateSiteResult.Failed; } return(result); } }
public static Business.Objects.CreateSiteResult CreateSite(Site site) { var result = new Business.Objects.CreateSiteResult(); var bindingInformations = site.Bindings.Select(x => x.ToIisBindingInformation()).ToList(); // Check bindings var bindingInUse = GetBindingInUse(0, bindingInformations); // 0 never exists if (bindingInUse != null) { result.Result = CreateSiteResult.BindingAlreadyInUse; return result; } using (var manager = new ServerManager()) { // Create site manager.Sites.Add(site.Name, "http", bindingInformations.First(), site.SitePath); var iisSite = manager.Sites.SingleOrDefault(x => x.Name == site.Name); // Add bindings iisSite.Bindings.Clear(); foreach (var binding in bindingInformations) iisSite.Bindings.Add(binding, "http"); // Set/create application pool if (string.IsNullOrWhiteSpace(site.ApplicationPool)) // Auto create application pool { var appPoolName = site.Name; var existingApplicationPoolNames = manager.ApplicationPools.Select(x => x.Name).ToList(); var newNameCount = 1; while (existingApplicationPoolNames.Contains(appPoolName)) { appPoolName = site.Name + "_" + newNameCount; newNameCount++; } manager.ApplicationPools.Add(appPoolName); iisSite.ApplicationDefaults.ApplicationPoolName = appPoolName; } else { iisSite.ApplicationDefaults.ApplicationPoolName = site.ApplicationPool; } manager.CommitChanges(); var created = false; var sw = new Stopwatch(); sw.Start(); while (!created && sw.Elapsed.TotalSeconds < 3) { try { if (iisSite.State == ObjectState.Started || iisSite.State == ObjectState.Stopped) { created = true; } } catch (COMException) { System.Threading.Thread.Sleep(100); } } sw.Stop(); if (created) { result.Result = CreateSiteResult.Success; result.IisSiteId = (int) iisSite.Id; } else { result.Result = CreateSiteResult.Failed; } return result; } }