@@ -19,23 +19,44 @@ internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
19
19
{
20
20
List < RpcFunctionMetadata > indexedFunctions = new List < RpcFunctionMetadata > ( ) ;
21
21
22
+ // This is not the correct way to deal with getting a runspace for the cmdlet.
23
+
24
+ // Firstly, creating a runspace is expensive. If we are going to generate a runspace, it should be done on
25
+ // the function load request so that it can be created while the host is processing.
26
+
27
+ // Secondly, this assumes that the AzureFunctions.PowerShell.SDK module is present on the machine/VM's
28
+ // PSModulePath. On an Azure instance, it will not be. What we need to do here is move the call
29
+ // to SetupAppRootPathAndModulePath in RequestProcessor to the init request, and then use the
30
+ // _firstPwshInstance to invoke the Get-FunctionsMetadata command. The only issue with this is that
31
+ // SetupAppRootPathAndModulePath needs the initial function init request in order to know if managed
32
+ // dependencies are enabled in this function app.
33
+
34
+ // Proposed solutions:
35
+ // 1. Pass ManagedDependencyEnabled flag in the worker init request
36
+ // 2. Change the flow, so that _firstPwshInstance is initialized in worker init with the PSModulePath
37
+ // assuming that managed dependencies are enabled, and then revert the PSModulePath in the first function
38
+ // init request should the managed dependencies not be enabled.
39
+ // 3. Continue using a new runspace for invoking Get-FunctionsMetadata, but initialize it in worker init and
40
+ // point the PsModulePath to the module path bundled with the worker.
41
+
42
+
22
43
InitialSessionState initial = InitialSessionState . CreateDefault ( ) ;
23
44
Runspace runspace = RunspaceFactory . CreateRunspace ( initial ) ;
24
45
runspace . Open ( ) ;
25
- System . Management . Automation . PowerShell ps = System . Management . Automation . PowerShell . Create ( ) ;
26
- ps . Runspace = runspace ;
46
+ System . Management . Automation . PowerShell _powershell = System . Management . Automation . PowerShell . Create ( ) ;
47
+ _powershell . Runspace = runspace ;
27
48
28
- ps . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
49
+ _powershell . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
29
50
string outputString = string . Empty ;
30
- foreach ( PSObject rawMetadata in ps . Invoke ( ) )
51
+ foreach ( PSObject rawMetadata in _powershell . Invoke ( ) )
31
52
{
32
53
if ( outputString != string . Empty )
33
54
{
34
55
throw new Exception ( PowerShellWorkerStrings . GetFunctionsMetadataMultipleResultsError ) ;
35
56
}
36
57
outputString = rawMetadata . ToString ( ) ;
37
58
}
38
- ps . Commands . Clear ( ) ;
59
+ _powershell . Commands . Clear ( ) ;
39
60
40
61
List < FunctionInformation > functionInformations = JsonConvert . DeserializeObject < List < FunctionInformation > > ( outputString ) ;
41
62
0 commit comments