Runbook C# Script Error

I’m attempting to run a C# script in a runbook and getting this error, and I believe it has something to do with the Newtonsoft.json reference, but I’m not sure how to handle it.

ERROR: Script compilation failed. [CompilationErrorException] C:\Octopus\Work\20230815205146-166352-6979\Script.csx(15,26): error CS0012: The type ‘Object’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’.

Here’s my script:

#r "C:\\Program Files\\Octopus Deploy\\Octopus\\Newtonsoft.Json.dll"
  
using Newtonsoft.Json.Linq;

string authenticationString = $"username:pw";
string base64Auth = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

// Make call to web service
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Basic {base64Auth}");
var response = client.GetStringAsync("http://localhost:15672/api/overview");
response.Wait();

// Parse web service response
string json = response.Result.ToString();
JObject result = JObject.Parse(json);
JObject queue_totals = JObject.Parse(result["queue_totals"].ToString());
int totalMessageCount = Convert.ToInt32(queue_totals["messages"]);

Console.WriteLine($"Total RabbitMQ messages queued: {totalMessageCount}");

I’m on Octopus Server version 2021.3 if that helps.

Hi @wfroese

It does indeed look like the reference to the JSON file your referencing. I gather this particular script will work outside of Octopus since you’re directly referencing the JSON file tied into our program files, but would you mind testing this?

One thing I will comment on is that utilising the JSON file within our program files won’t work if you are deploying to a target or on a worker, since that file will not be deployed across with your script. I’d recommend using a separate script to call down the JSON file from a package store (NuGet, chocolatey, etc.) and install this on the machine at runtime. This will allow you to use the latest version, as well as include any upstream references you may need (.NET, HTTPClient, etc.), so it will make it much more robust.

Another alternative would be to have your script directly execute on the Octopus Server, but I would still recommend pulling the NuGet file for JSON at runtime.

Since this is more of a scripting problem than an Octopus problem I won’t be able to offer too much direct help, but I’ll give as much advice as I can.

Let me know how you get on!
Kind Regards

I am running this directly on the Octopus Server, rather than on any specific tentacle.

For testing outside Octopus - is the best way to do that installing a ScriptCS runtime and manually running it that way? Or is ScriptCS already bundled with Octopus and usable outside of it?

Hi @wfroese,

Thanks for getting back! I’m just stepping in for Sean while they’re offline as a member of our UK Support rotation.

I’m just wondering if you could perhaps benefit from executing your script in an execution container?

We currently recommend this as an alternative to messing with dependencies on your Octopus server or deployment targets. You can execute the script with a container image pre-loaded with all of the tools and dependencies you require for the script to execute. The documentation on execution containers I linked to has details on using a custom docker image if the ones we provide don’t suit your needs.

Here are our worker tools and the Docker file for the windows image to show what we load.
https://hub.docker.com/r/octopusdeploy/worker-tools

Our worker-tools image is using some fairly old versions for specific tools as many have deprecated features in later versions. For us to upgrade them would pull the rug out from under a decent amount of customer projects, so we decided to create a handful of new images that we can keep up to date for different uses. You can find them here.
https://hub.docker.com/u/octopuslabs

Hopefully execution containers help you achieve this. Otherwise, let us know if you have any questions or if this doesn’t help.

Best regards,
Daniel

So those execution contains look nice (I didn’t know about them, thanks), but after a bit of research I think what I really want is this: Request for Comments - Migrating from scriptcs to dotnet-script - Octopus Deploy

And according to this GitHub issue, it was released in v2023.3?

I just did an upgrade of our instance this morning, but the latest version I got is 2023.2.13175. How do I get to version 2023.3?

The release comparison tool says I can’t?

And the downloads page says 2023.3 isn’t recommended yet for self-hosted installations - any idea if that will be coming soon? If so, I can just wait for that I think.

Hey @wfroese,

I’m just stepping in for others on the team, but I’m happy to help!

Based on the open GitHub issue you linked, it looks like these changes have been merged into the latest versions of Octopus Deploy, but unfortunately, these versions are only available to our cloud customers at this time (versus self-hosted/on-premise). You can see more information on these releases on our download archives page here:

Although this is the case, I will reach out internally to check the timeline for merging these changes into the on-premise versions of Octopus Deploy and when this might be available to download.

I will let you know as soon as I have an update, and let me know if I can be of any other help in the meantime.

Cheers,

Britton

Hi @wfroese,

Thanks for your patience while I contacted our engineering team on this!

They told me these changes are still in progress and have not yet been merged into the main codebase. Once these changes are merged, they will be included in new releases, starting with the 2023.3.x versions of Octopus Deploy, eventually making their way into on-premise versions.

With this being the case, I’d recommend subscribing to the GitHub issue you found, as the team will post updates here as they become available.

Cheers,

Britton

Ok - subscribed to the GitHub issue. I’m content to close this support thread and get back to what I was attempting once the tooling’s improved.

Thanks,
Will

1 Like

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