C# script with Nuget dependencies

I’m trying to run the following C# script:

#r "Microsoft.VisualStudio.Services.WebApi"
#r "Microsoft.VisualStudio.Services.Common"
#r "Microsoft.TeamFoundation.DistributedTask.WebApi"

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

var connection = new VssConnection(new Uri(Octopus.Parameters["DevopsUrl"]), new VssBasicCredential(string.Empty, Octopus.Parameters["DevopsToken"]));
var client = connection.GetClient<TaskAgentHttpClient>();
var agentPools = await client.GetAgentPoolsAsync();
var poolList = new[] { "Pool1", "Pool2"};
foreach (var pool in agentPools.Where(p => poolList.Contains(p.Name)))
{
  var agents = await client.GetAgentsAsync(pool.Id);
  foreach (var agent in agents.Where(a => a.Status == TaskAgentStatus.Offline))
  {
    await client.DeleteAgentAsync(pool.Id, agent.Id);
  }
}

I have added Microsoft.TeamFoundation.DistributedTask.WebApi and Microsoft.VisualStudio.Services.Client as referenced packages on the step. Bombs out with the following error:

ERROR: Error executing script ‘E:\Octopus\Work\20211213213045-189365-4110\Bootstrap.73f-4dd1-a379-58ff4c32a22c.Script.csx’ [FileNotFoundException] Assembly not found.

Any suggestions?

Hi Chris,

Thanks for reaching out!

We have seen a similar issue in the past due to the System.IO.Compression.FileSystem.dll not being include which was resolved by slightly modifying the references used. Possibly including the full file paths might work!

Let me know if the resolution for that past issue doesn’t help you case and I’ll be happy to take a deeper look into what’s going on!

If you could please send through the Task Log, as well as any other relevant information such as Octopus version and if you also get the same error when running the script outside of Octopus, that would be appreciated.

Best Regards,

The issue makes sense, but how do I fully resolve the Nuget package’s DLL path in the #r directive without knowing it upfront?

Hey @Chris_Robison,

Just jumping in for Finnian as he is offline until a bit later today.

The best method to troubleshoot this may be to get the script running outside of Octopus first, then we put it back into Octopus. On the tentacle/worker that is running the script, can you please try to pre-install those assemblies and make sure they are accessible by the service account running Octopus? If you pre-install the assemblies, you could then put the full path of the dlls into the script.

Please let me know if that helps or if you have more questions.

Best,
Jeremy

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