C# Post-Deployment script

I have a special configuration for my application that requires me to run a c# script with some assemblies like

using Microsoft.Web.Administration;
using Configuration = System.Configuration.Configuration;
using WebConfigurationManager = System.Web.Configuration.WebConfigurationManager;

How can I add assemblies that can be used by the scripts?

So I’ve learned that the Using statements don’t really work so I’ve switched the code to the following.

tring absolute = Octopus.Parameters[“Octopus.Action[Install Admin Portal].Output.Package.InstallationDirectoryPath”] ;
// Connect to IIS ServerManager
using (var serverManager = new Microsoft.Web.Administration.ServerManager())
{
alias = “/” + alias;
foreach (Microsoft.Web.Administration.Site site in serverManager.Sites)
{
// Go through all sites configured
bool found = false;
foreach (Microsoft.Web.Administration.Application application in site.Applications)
{
// Look at each application configured in IIS.

        if (application.Path.Equals(alias)){
            Console.WriteLine("\n\nApplication: {0} {1}", application.ApplicationPoolName,
                application.Path);
            found = true;
            string phyPath = application.VirtualDirectories[0].PhysicalPath;
            if (!phyPath.Equals(absolute)){
                // Check to see if application physical path needs to be updated
                Console.WriteLine("\n*Replacing path {0} with {1}\n", application.Path, absolute);
                application.VirtualDirectories[0].PhysicalPath = absolute;
            }
        }
    }
    if (!found){
        // We need to add the application
        Console.WriteLine("Adding Alias:{0} Path:{1}", alias, absolute);
        try{
            site.Applications.Add(alias, absolute);
        }
        catch (Exception e){
            Console.WriteLine(e.Message);
        }
    }
}
serverManager.CommitChanges();

}

However, this still doesn’t work I get a

ERROR: Script compilation failed: C:\Octopus\Applications\Dev\AdminPanel\1.1.20_26\Octopus.Action.CustomScripts.PostDeploy.csx(4,46): error CS0234: The type or namespace name ‘Administration’ does not exist in the namespace ‘Microsoft.Web’ (are you missing an assembly reference?).

Hi,

Thanks for getting in touch.

You’d need to create a package with all of your script’s dependent assemblies. Octopus uses ScriptCS to execute C# scripts, so your C# script(s) would need have a .csx extension. You may also need to reference your assemblies, which you can do as follows:

Eg. Where the DLLs sit in the same folder as the script in your package:

#r "Microsoft.Web.Administration.dll";
using (var serverManager = new Microsoft.Web.Administration.ServerManager())
{
    Console.WriteLine("hey this works");
}

If for some reason you are unable to use packages for your C# scripts, you’d need to make your assemblies available for the tentacle when it is executing the scripts inline.

When inline script work is assigned for an Octopus tentacle to execute, the scripts are streamed to a temporary folder (typically located at C:\Octopus[your tentacle folder]\Work). This temporary work folder will be named with a datestamp, the script will be executed, and the temporary folder will then be removed.

So if you needed inline scripts to reference assemblies, you could copy your assemblies into the tentacle’s \Work folder, then use a relative path to reference the assembly, like so:

Eg. For inline scripts, where the assemblies are located in the tentacle’s Work folder, one up from where the temporary folder is created to execute the script:

#r "../Microsoft.Web.Administration.dll";
using (var serverManager = new Microsoft.Web.Administration.ServerManager())
{
    Console.WriteLine("hey this works too");
}

You can find more information in the Referencing assemblies section of ScriptCS here.

Hope that helps :slight_smile:

Cheers
Mark

I found a much better way using a powershell script to setup the application.

New-WebApplication -Name $webservicename -PhysicalPath $OctopusParameters[‘Octopus.Action[Install Admin Portal].Output.Package.InstallationDirectoryPath’] -Site “Default Web Site” -force