Poshcriber.Service.SubscriptionEventNotification C# (CSharp) Method

SubscriptionEventNotification() public method

public SubscriptionEventNotification ( SubscriptionNotification notification ) : bool
notification SubscriptionNotification
return bool
        public bool SubscriptionEventNotification(SubscriptionNotification notification)
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.ApartmentState = System.Threading.ApartmentState.STA;
            runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;

            runspace.Open();

            Pipeline pipeline = runspace.CreatePipeline();
            var myCmd = new Command( Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Invoke-Subscriber.ps1" ) );
            myCmd.Parameters.Add( new CommandParameter( "event", notification.NotificationType.ToString() ));
            myCmd.Parameters.Add( new CommandParameter( "href", notification.RelativeHref ));
            myCmd.Parameters.Add( new CommandParameter( "subscriptions", notification.SubscriptionArray ));
            myCmd.Parameters.Add( new CommandParameter( "changes", notification.ChangesArray ));
            pipeline.Commands.Add( myCmd );

            // Execute PowerShell script
            // Instead of implementing our own Host and HostUI we keep this extremely simple by
            // catching everything to cope with HostExceptions and UnknownCommandExceptions etc.
            // The first will be thrown if someone tries to access unsupported (i.e. interactive)
            // host features such as Read-Host and the latter will occur for all unsupported commands.
            // That can easily happen if a script is missing an import-module or just contains a mispelled command
            try
            {
                var result = pipeline.Invoke().FirstOrDefault();
                return result != null && result.BaseObject is bool && (bool)result.BaseObject;
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Exception caught when invoking powershell script: " + ex);
                return false;
            }
        }

Usage Example

Ejemplo n.º 1
0
        public void ReturningFalseFromScriptReturnsFalseOnReceviedNotificationEvent()
        {
            CreateScript( result: false);
            var subscriber = new Service();
            var notification = SetupExpectedNotification();

            var result = subscriber.SubscriptionEventNotification(notification);

            Assert.IsFalse(result);
        }
All Usage Examples Of Poshcriber.Service::SubscriptionEventNotification