Listen for Events from Powershell given from NewWebService Proxy c# and Powershe

Hi Everyone!

I am trying to get events to kick off from my powershell script and can’t seem to properly kick off the event. It shows it registers them in a stop state and then never triggers. I was thinking I needed to do a while loop or wait to keep the script alive while I am working in ISE but no dice. Anyone know how to get this process to work to receive events triggered from the webserver

Output

SubscriptionId : 2
SourceObject : Portal.API.API
EventName : GetActiveClientsCompleted
SourceIdentifier : PortalEvent
Action : System.Management.Automation.PSEventJob
HandlerDelegate :
SupportEvent : False
ForwardEvent : False

SubscriptionId : 3
SourceObject : Portal.API
EventName : GetActiveClientsCompleted
SourceIdentifier : PortalEvent2
Action : System.Management.Automation.PSEventJob
HandlerDelegate :
SupportEvent : False
ForwardEvent : False

Module : __DynamicModule_3449aaa6-bd94-4cda-81a9-e965e1356643
StatusMessage :
HasMoreData : False
Location :
Command :
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -ForegroundColor red

JobStateInfo : NotStarted
Finished : System.Threading.ManualResetEvent
InstanceId : 7fad94f1-f14d-4630-9ce6-85c10f005638
Id : 1
Name : PortalEvent
ChildJobs : {}
PSBeginTime :
PSEndTime :
PSJobTypeName :
Output : {}
Error : {}
Progress : {}
Verbose : {}
Debug : {}
Warning : {}
Information : {}
State : NotStarted

Module : __DynamicModule_1109d5e2-09f7-480a-8d38-f81fb3ed03d3
StatusMessage :
HasMoreData : False
Location :
Command :
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -ForegroundColor red

JobStateInfo : NotStarted
Finished : System.Threading.ManualResetEvent
InstanceId : 5697a8be-0c1d-46c0-b50e-df3ef7ce9ef6
Id : 2
Name : PortalEvent2
ChildJobs : {}
PSBeginTime :
PSEndTime :
PSJobTypeName :
Output : {}
Error : {}
Progress : {}
Verbose : {}
Debug : {}
Warning : {}
Information : {}
State : NotStarted
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host #This will Break the Webcall scripts

if ( !(Test-Path variable:proxy) ) {
    $proxy = New-WebServiceProxy -uri "https://Portal/API.asmx?WSDL" -namespace "Portal" -UseDefaultCredential
    $api = New-WebServiceProxy -uri "https://Portal/API.asmx?WSDL" -namespace "Portal.API" -UseDefaultCredential
}

$action = {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -ForegroundColor red
}

Register-ObjectEvent -SourceIdentifier "ISPortalEvent" -InputObject $api -EventName GetActiveClientsCompleted -Action $action
Register-ObjectEvent -SourceIdentifier "ISPortalEvent2" -InputObject $proxy -EventName GetActiveClientsCompleted -Action $action #-Action { Write-Host "New entry" }
Get-EventSubscriber
$proxy.StartSession("50180544-27e1-4125-a986-c900d1f89cc8", 2);
$api.StartSession("50180544-27e1-4125-a986-c900d1f89cc9", 1);

Get-Job

#Wait-Event -SourceIdentifier "ISPortalEvent"
Wait-Event -SourceIdentifier "ISPortalEvent2"

while ($true) {
    #process the pending message
    [System.Windows.Forms.Application]::DoEvents()
}

$proxy.StopSession("50180544-27e1-4125-a986-c900d1f89cc8");

 

 

 

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Threading;

namespace WebService
{
    ///


    /// Summary description for WebService
    ///
    [WebService(Namespace = "http://localhost/webservices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class WebService : System.Web.Services.WebService
    {
        #region private members
        // This static Dictionary keeps track of all currently open sessions
        private static Dictionary s_services = new Dictionary();

        private int m_clientID;
        #endregion private members

        public WebService ()
        {
        }

        [WebMethod]
        public void Trigger1()
        {
        System.Diagnostics.Debug.WriteLine("Trigger called");

        }

        [WebMethod]
        public void Trigger()
        {
            System.Diagnostics.Debug.WriteLine("Trigger called");
            lock (s_services)
            {
            // Signal GetActiveClientsCompleted event for each client
                foreach (Guid sID in s_services.Keys)
                {
                    s_services[sID].GetActiveClientsCompleted.Set();
                }
            }
        }

    // I wish to have something like commented text below to be able setup event staff for WebService
    // (see also commented out ActiveClientsChangedDelegate definition at the end of this namespace)
    // but unfortunately it's not a case:
    //[WebEvent]
    //public event ActiveClientsChangedDelegate OnActiveClientsChanged = null;

    #region WebService interface
        [WebMethod]
        public void StartSession(Guid sessionID, int clientID)
        {
        System.Diagnostics.Debug.WriteLine(sessionID + " - " + clientID);
        lock (s_services)
        {
        if (s_services.ContainsKey(sessionID))
        {
        // Session found in the list
        m_clientID = s_services[sessionID].ClientID;
        }
        else
        {
        // Add session to the list
        m_clientID = clientID;
        s_services.Add(sessionID, new ClientState(m_clientID));
        }
        }

        lock (s_services)
        {
        // Signal GetActiveClientsCompleted event for each client
        foreach (Guid sID in s_services.Keys)
        {
        s_services[sID].GetActiveClientsCompleted.Set();
        }
        }
        }

        [WebMethod]
        public void StopSession(Guid sessionID)
        {
        lock (s_services)
        {
        if (s_services.ContainsKey(sessionID))
        {
        // Remove session from the list
        s_services.Remove(sessionID);
        }
        }

        lock (s_services)
        {
        // Signal GetActiveClientsCompleted event for each client
        foreach (Guid sID in s_services.Keys)
        {
        s_services[sID].GetActiveClientsCompleted.Set();
        }
        }
        }

        [WebMethod]
        public int[] GetActiveClients(Guid sessionID)
        {
        if (!s_services.ContainsKey(sessionID))
        {
        //string message = string.Format("No relevant sessions detected: {0}", sessionID.ToSTring());
        //System.Diagnostics.Debug.WriteLine(message);

        // Return empty client list
        return new int[] { };
        }

        //DateTime dt = DateTime.Now;
        bool signalled = s_services[sessionID].GetActiveClientsCompleted.WaitOne(); // wait for GetActiveClientsCompleted event
        if (signalled)
        {
        lock (s_services)
        {
        //string message = string.Format("GetActiveClientsCompleted event detected during {0} ms timeout", (DateTime.Now - dt).TotalMilliseconds);
        //System.Diagnostics.Debug.WriteLine(message);

        // Create client list and return it
        List clients = new List();
        foreach (Guid sID in s_services.Keys)
        {
        if (sID == sessionID) continue;
        clients.Add(s_services[sID].ClientID);
        }
        return clients.ToArray();
        }
        }
        else
        {
        //string message = string.Format("No GetActiveClientsCompleted events detected during {0} ms timeout", timeoutMsec);
        //System.Diagnostics.Debug.WriteLine(message);

        // Return empty client list
        return new int[] { };
        }
        }
    #endregion

        private class ClientState
        {
            public int ClientID;
            public AutoResetEvent GetActiveClientsCompleted = new AutoResetEvent(false);
            public ClientState(int clientID)
            {
            ClientID = clientID;
            }
        }
    }

//public delegate void ActiveClientsChangedDelegate(int[] clients);
}