flancy
flancy copied to clipboard
Pass variables to webschema's "script"
We need some way to pass variables/objects to webschema's script
.
Ideally, script
should be passed and executed as an actual PowerShell scriptblock, with it's SessionState and so on. This way we could capture required variables with GetNewClosure
or pass them to scriptblock as arguments.
Right now, if I understand it correctly, scriptblocks are used as text to build function that executed in nancy via PowerShell host:
string command = @"function RouteBody {{ param($Parameters, $Request) {0} }}";
I'd love to be able to do this. I'm not sure I can though - I will be actively pursuing a solution over this week while I'm at the MVP summit in Redmond.
I'm not sure that we can pass an actual PS scriptblock, but as viable workaround we can create a new InitialSessionState, create Runspace using it, assign this Runspace to a PowerShell object, and then import current variables/modules into it. Unfortunately, while it's all can be done in PowerShell, I have no idea how to pass resulting Runspace to the Flancy. Probably C# code has to be extended with some method/property that would accept Runspace object.
References:
- Windows PowerShell Host Quickstart
- Warren F.'s Invoke-Parallel function. It's a good example of importing variables/modules in a Runspace.
More thoughts on a subject:
- If we could pass a runspace to a Nancy's PS host, we could ditch
string command = @"function RouteBody {{ param($Parameters, $Request) {0} }}";
line entirely and instead add this function to a Runspace beforehand in PS part of module. - If this Runspace object could be passed as some kind of "shared" object (by reference?), we could modify it on the fly and
script
could be reloaded (but not routes, though) without restarting PowerShell.