protected override Task RemoteConfigureAsync(IRemoteOperationExecutionContext context) { this.LogDebug($"Looking for Site \"{this.Template.Name}\"..."); using (var manager = new ServerManager()) { var site = manager.Sites[this.Template.Name]; if (this.Template.Exists) { if (!string.IsNullOrWhiteSpace(this.Template.ApplicationPoolName)) { if (manager.ApplicationPools[this.Template.ApplicationPoolName] == null) { this.Log( context.Simulation ? MessageLevel.Warning : MessageLevel.Error, $"The specified application pool ({this.Template.ApplicationPoolName}) does not exist." ); if (!context.Simulation) { return(Complete()); } } } if (site == null) { this.LogDebug("Does not exist. Creating..."); if (!context.Simulation) { var templateBinding = this.Template.Bindings?.FirstOrDefault(); if (templateBinding == null) { throw new ExecutionFailureException("When creating a new IIS site, at least one binding is required."); } var binding = BindingInfo.FromMap(templateBinding); if (binding == null) { throw new ExecutionFailureException("Binding info could not be parsed. At a minimum, 'IPAddress' and 'Port' must be specified."); } site = manager.Sites.Add(this.Template.Name, this.Template.VirtualDirectoryPhysicalPath, int.Parse(binding.Port)); manager.CommitChanges(); } this.LogInformation($"Site \"{this.Template.Name}\" added."); this.LogDebug("Reloading configuration..."); if (!context.Simulation) { site = manager.Sites[this.Template.Name]; } } this.LogDebug("Applying configuration..."); if (!context.Simulation) { IisSiteConfiguration.SetMwaSite(this, this.Template, site); } } else { if (site == null) { this.LogWarning("Site does not exist."); return(Complete()); } if (!context.Simulation) { manager.Sites.Remove(site); } } this.LogDebug("Committing configuration..."); if (!context.Simulation) { manager.CommitChanges(); } this.LogInformation($"Site \"{this.Template.Name}\" {(this.Template.Exists ? "configured" : "removed")}."); } return(Complete()); }