powershell azure function app throws an exception "Unable to find type [HttpResponseContext]" when debugging in VS 2022

Viewed 252

I created a PowerShell azure Function App with the default/sample function in it.

On the function app 'overview' page I clicked the 'download app content' and downloaded the 'content and visual studio project' including the app settings.

I put the content into an empty folder on my dev computer and opened it with Visual Studio 2022. Updated the .net.sdk.functions nuget package to 4.1.1 and set the target framework to 4.8

When I start the debugging the function loads and listens, but when I open the URL it throws an exception: "Unable to find type [HttpResponseContext]"

It looks like the 'using namespace System.Net' line in the powershell code doesn't work. (the code/debugging works on the same computer from VSCode, but I would prefer using VS2022)

Any help to make this work would be appreciated. Thank you in advance.

This is part of the .csproj file:

<PropertyGroup>
    <TargetFramework>net48</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

This is the code:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

if ($name) {
    $body = "Hello, $name. This HTTP triggered function executed successfully."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})
1 Answers

Scenario 1:

Created PowerShell Core 7.2 Versioned Azure Function Http Trigger in the Azure Portal and downloaded it from the Overview blade with the following [Download App Content > Content & VS Project - Include app settings in the download].

Scenario 2:

When we create the same project from VS Code locally, it is not creating any .csproj file and if it is published without .csproj to Azure Portal Function App, Azure is sending the .csproj file in its Content and Visual Studio Project option with the net461 as target framework.

Error:

<Message>Exception while executing function: Functions.HttpTrigger1 -> PowerShell script error -> Unable to find type [HttpResponseContext].</Message>
....
<StatusCode>InternalServerError</StatusCode>

According to Azure Functions PowerShell Official Doc, .NET Framework is not supported in the PowerShell Runtime, but Azure is sending the framework version to the .csproj file.

We raised the triage ticket to the Microsoft on this issue and follow the updates from this GitHub ticket.

Related