C# Predeployment Script doesn't get correct NuGet package

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;
    }
}

Hello,

Thanks for getting in touch. I’ve reproduced the problem the package is on NuGet, what have you got set up to fetch it from NuGet. Is it being installed to the Global Assembly Cache (GAC)?

When using ScriptCS you’ll need to use the #r reference syntax. For me the assembly is available from the GAC.

Here’s what worked for me:

#r "Microsoft.Web.Administration";

using Microsoft.Web.Administration;
using System;
  
using (ServerManager serverManager = new ServerManager())
{
  Configuration config = serverManager.GetApplicationHostConfiguration();
}

Regards,
Nick

Hi Again,

I tried one more thing and found a simpler solution, it looks like it all comes down to the missing #r reference.

I’ll edit the previous repose so it’s clear for anyone else coming to this post.But this should work for you:

#r "Microsoft.Web.Administration";


using Microsoft.Web.Administration;
using System;
  
  
using (ServerManager serverManager = new ServerManager())
{
  Configuration config = serverManager.GetApplicationHostConfiguration();
}

Let me know how you go.

Regards,
Nick

Hi Nick

Thanks for the help! the #r reference at the top of the file was what i needed to get the nuget package working!

Thanks for the help!

Kind Regards

Andy

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.