Hi
I’m writing a pre-deployment script in c# to change the variables on an IIS server in the ‘Configuration Scripts’ section of a deployment step. however, when I create a release and deploy, I’m getting this error message:
ERROR: Script compilation failed. [CompilationErrorException] error CS0234: The type or namespace name 'Web' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
I believe it is because the script uses Microsoft.Web.Administration, which you can get from NuGet, is not being installed before the script is ran. See below for the code I’m using in the pre-deployment script. Is it possible to install a nuget package before the script is ran?
Any help would be greatly appreciated.
using Microsoft.Web.Administration;
using System;
internal static class Sample
{
private static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();
ConfigurationElement addElement = FindElement(applicationPoolsCollection, "add", "ASPNETCORE_ENVIRONMENT", @"TST-INT-WEB");
if (addElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElementCollection environmentVariablesCollection = addElement.GetCollection("environmentVariables");
ConfigurationElement addElement1 = environmentVariablesCollection.CreateElement("add");
addElement1["name"] = @"ASPNETCORE_ENVIRONMENT";
addElement1["value"] = @"TST-INT-WEB";
environmentVariablesCollection.Add(addElement1);
serverManager.CommitChanges();
}
}
private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
{
foreach (ConfigurationElement element in collection)
{
if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
{
bool matches = true;
for (int i = 0; i < keyValues.Length; i += 2)
{
object o = element.GetAttributeValue(keyValues[i]);
string value = null;
if (o != null)
{
value = o.ToString();
}
if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
{
matches = false;
break;
}
}
if (matches)
{
return element;
}
}
}
return null;
}
}