I am trying to write a PowerShellCmdlet in C#. It is a PowerShell command to make a REST API call. Here's a snippet of the important functions of the code -
[Cmdlet(VerbsCommon.Get, "MXL3Firewall")]
[OutputType(typeof(MXFirewallRule))]
public class GetMXL3FirewallCommand : PSCmdlet
{
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public string Token { get; set; }
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public string NetId { get; set; }
private static async Task<IList<MXFirewallRule>> GetFWRules(string Token, string NetId)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Cisco-Meraki-API-Key", Token);
// Created the request header above
var streamTask = client.GetStreamAsync($"https://api.meraki.com/api/v0/networks/{NetId}/l3FirewallRules");
// converting the serialized JSON response into a C# object
return await JsonSerializer.DeserializeAsync<IList<MXFirewallRule>>(await streamTask);
}
}
private static IList<MXFirewallRule> ProcessRecordAsync(string Token, string NetId)
{
var task = GetFWRules(Token, NetId);
task.Wait();
return task.Result;
}
protected override void ProcessRecord()
{
WriteVerbose("Entering Get Firewall Rules Call");
var list = ProcessRecordAsync(Token, NetId);
WriteObject(list, true);
}
}
I get the following error -
Get-MXL3Firewall : Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.1,
Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file
specified.
At line:1 char:1
+ Get-MXL3Firewall -Token $token -NetId $netid
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-MXL3Firewall], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,RestToPSCommand.GetMXL3FirewallCommand
In the (.csproj) I have the following line -
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
I tried updating the Nuget package as mentioned here, but it did not help. How can I fix this error?