/// <summary>
        /// Helper private function that resolve the name to the corresponding binding information.
        /// </summary>
        private ReadOnlyBindingInfo GetBindingInfo(string name)
        {
            Guid currentRunspaceId = Runspace.DefaultRunspace.InstanceId;
            var  bindingMap        = FunctionMetadata.GetOutputBindingInfo(currentRunspaceId);

            // If the instance id doesn't get us back a binding map, then we are not running in one of the PS worker's Runspace(s).
            // This could happen when a custom Runspace is created in the function script, and 'Push-OutputBinding' is called in that Runspace.
            if (bindingMap == null)
            {
                string      errorMsg = PowerShellWorkerStrings.DontPushOutputOutsideWorkerRunspace;
                ErrorRecord er       = new ErrorRecord(
                    new InvalidOperationException(errorMsg),
                    nameof(PowerShellWorkerStrings.DontPushOutputOutsideWorkerRunspace),
                    ErrorCategory.InvalidOperation,
                    targetObject: currentRunspaceId);

                this.ThrowTerminatingError(er);
            }

            if (!bindingMap.TryGetValue(name, out ReadOnlyBindingInfo bindingInfo))
            {
                string      errorMsg = string.Format(PowerShellWorkerStrings.BindingNameNotExist, name);
                ErrorRecord er       = new ErrorRecord(
                    new InvalidOperationException(errorMsg),
                    nameof(PowerShellWorkerStrings.BindingNameNotExist),
                    ErrorCategory.InvalidOperation,
                    targetObject: name);

                this.ThrowTerminatingError(er);
            }

            return(bindingInfo);
        }
Exemplo n.º 2
0
        public void RegisterAndUnregisterFunctionMetadataShouldWork()
        {
            string path = Path.Join(s_funcDirectory, "testBasicFunction.ps1");

            var(functionInfo, testManager) = PrepareFunction(path, string.Empty);

            FunctionMetadata.RegisterFunctionMetadata(testManager.InstanceId, functionInfo);
            var outBindingMap = FunctionMetadata.GetOutputBindingInfo(testManager.InstanceId);

            Assert.Single(outBindingMap);
            Assert.Equal(TestOutputBindingName, outBindingMap.First().Key);

            FunctionMetadata.UnregisterFunctionMetadata(testManager.InstanceId);
            outBindingMap = FunctionMetadata.GetOutputBindingInfo(testManager.InstanceId);
            Assert.Null(outBindingMap);
        }