PowerShell / PowerShell/PowerShell

Feature Request Async callbacks C# interaction

Open
#15,080 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue-Enhancement Needs-Triage WG-Language
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

Summary of the new feature/enhancement

As a user I want to provide a pure script blocks to C# AsyncCallback functions so that the functions can be called via PowerShell calls. Currently PowerShell crashes because it tries to invoke the Delegate without a runspace.

Proposed technical implementation details (optional)

When casting a script block to a System.AsyncCallback add code to initialize the runspace automatically.

$http = [System.Net.HttpListener]::new()
$http.Prefixes.Add("http://127.0.0.1:9999/")
$http.Start()
[Console]::WriteLine("Listening...")

$callback = [System.AsyncCallback]{param($asyncResult); [System.Net.HttpListener]$listener = [System.Net.HttpListener] $asyncResult.AsyncState; [System.Net.HttpListenerContext]$context = $listener.EndGetContext($asyncResult); [System.Net.HttpListenerRequest]$request = $context.Request; [System.Net.HttpListenerResponse]$response = $context.Response; [string]$responseString = "<HTML><BODY>Hello World!</BODY></HTML>"; [byte[]]$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString); $response.ContentLength64 = $buffer.Length; [System.IO.Stream]$output = $response.OutputStream; $output.Write($buffer,0,$buffer.Length); $output.Close() }
[System.IAsyncResult]$result = $http.BeginGetContext($callback, $http)

$uri = [System.Uri]::new("http://127.0.0.1:9999/")
Invoke-WebRequest -Uri $uri

This should work as seemingly and with the same "restrictions" as ForEach-Object -Parallel does in regards to runspace handling.
Maybe a new more lightweight handling that only supports pure functions could be considered for both.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the HttpListener and System.AsyncCallback example from the issue, then trace how a script block is cast to System.AsyncCallback and how runspaces are handled for ForEach-Object -Parallel. Compare the expected callback behavior with the current crash, and consider the stated restriction that callbacks should support pure script blocks. Done means the sample runs through PowerShell without requiring manual runspace setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, shell
Domain
backend, cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.