AspNetForPowerShell - PowerShell module that packages AspNetCore for easy use in PowerShell #19596
Closed
rhubarb-geek-nz
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This project was inspired by the discussion here ASP.NET Core WebApplication in Powershell 7
The project is on GitHub at AspNetForPowerShell - Host PowerShell pages in ASP.NET
This project contains four main components
C#
RequestDelegate
that uses async programming to deliver the request.body to the PowerShell input pipeline, and takes the output pipeline and writes it to the response.body. The use of async programming allows true pipelines in PowerShell to write the output directly to the normal output pipeline without needing to deal with the underlying http content streams. This means that you can have a pipeline where each time an object is written to the PowerShell output it will then be written directly to the http response. If you use curl, you can see each object being sent individually as a chunk within the http response.A couple of
Cmdlets
to create the application and request delegate without having to resort to loading the AspNet dlls yourself. The AspNet runtime is loaded by the use of the Cmdlets.Packaging that directly creates a
PowerShell Module
, as a zip, as a NuGet package and as a Debian package.Test suite demonstrating the techniques both in
C#
andPowerShell
.The project is built as components so you can use as much or as little as you need. For example you only need the request delegate if you want to host PowerShell handlers in a standard C# AspNet server.
There are some issues with packaging the AspNet runtime as a PowerShell Module, these are
They are native dlls, not Any Cpu so you can't put them in a common NuGet repository or the PowerShellGallery
They are version specific. This project matches the exact version of the AspNet runtime that goes with the DotNet runtime used within each version of PowerShell itself.
They are large.
I have had good success with making a Debian package that installs in /opt/microsoft/powershell/7/Modules that is version specific and uses symbolic links to the matching files in /usr/share/dotnet/share/Microsoft.AspNetCore.App directory. The use of the Debian package means the dependancies can be made exact.
Programming styles supported include the top-level C# style and also the direct injection using the Startup class. Demonstration code shows a Program.cs delegating to a Program.ps1 to do the configuration, a Startup.cs delegating to Startup.ps1 as well as the request delegate delagating to a RequestDelegate.ps1.
A completely PowerShell only Hello World can now be as simple as
The Cmdlets take care of the loading of the AspNet runtime itself. The use of the Cmdlets removes much of the boiler plate that you would otherwise have doing it without this module.
The test harness contains a complete web server demonstration where all the url mapping is done by PowerShell. This would then allow you to implement a /cgi-bin directory containing PowerShell scripts.
I see this as useful for demonstrations, proof of concept, and integrating PowerShell into existing servers. I would not recommend trying to replace security frameworks or filters with PowerShell. Use the existing frameworks together with PowerShell.
Beta Was this translation helpful? Give feedback.
All reactions