Steven Rosenberg
2008-04-15 14:40:39 UTC
I am trying to create a remote class that will allow a client to subscribe
to an event using delegates in C#. I have tried to do this my setting up a
class that is marshaled by reference to be used as the object that handles
the event
public delegate void RemoteAlert(string str);
public abstract class RemoteHandlerBase : MarshalByRefObject
{
public void Alert(string s)
{
//override on client is required
HandleAlert(s);
}
abstract protected void HandleAlert(string s);
}
The following class manages the actual delegate creation and fireing of the
event:
public class RemoteAlerter: MarshalByRefObject
{
#region public attributes and methods.
public event RemoteAlert RemoteAlertEvent;
//constructor...required if event object is used within a singleton
public RemoteAlerter()
{
Console.WriteLine("RemoteAlerter constructor.");
}
public void SetAsSubscriber()
{
EventSubscribers.Add(this); //add subscriber to list
}
public bool FireAlert(string s)
{
//if there are subscribers to the published events
if ( RemoteAlertEvent != null && EventSubscribers.Count > 0)
{
Console.WriteLine("# of subscribers = {0}.",
EventSubscribers.Count);
for (int i = 0; i < EventSubscribers.Count; i++)
{
((RemoteAlerter)(EventSubscribers[i])).RemoteAlertEvent(s);
}
return true;
}
return false;
}
#endregion
#region private attributes and methods.
private ArrayList EventSubscribers = new ArrayList(); //required for
singleton use
#endregion
}
The name of the inherited class that contains the event handler is...
class RemoteHandler : IProcessActivator_ClientActivated.RemoteHandlerBase
{
override protected void HandleAlert(string msg)
{
Console.WriteLine("Alert from server: {0}", msg);
}
}
When I instantiate the inherited class on the client side no execeptions are
thrown. When I create an event...
RemoteAlerter alert = (RemoteAlerter)Activator.GetObject(typeof
(IProcessActivator_ClientActivated.RemoteAlerter),
"tcp://localhost:9000/RemoteAlerter");
RemoteHandler rh = new RemoteHandler();
alert.RemoteAlertEvent += new RemoteAlert(rh.Alert);
...again no execeptions are thrown, however when the event is fired on the
server it is never received by the client.
The connection to the remote class uses a TCP / Binary connection method
Any ideas?
to an event using delegates in C#. I have tried to do this my setting up a
class that is marshaled by reference to be used as the object that handles
the event
public delegate void RemoteAlert(string str);
public abstract class RemoteHandlerBase : MarshalByRefObject
{
public void Alert(string s)
{
//override on client is required
HandleAlert(s);
}
abstract protected void HandleAlert(string s);
}
The following class manages the actual delegate creation and fireing of the
event:
public class RemoteAlerter: MarshalByRefObject
{
#region public attributes and methods.
public event RemoteAlert RemoteAlertEvent;
//constructor...required if event object is used within a singleton
public RemoteAlerter()
{
Console.WriteLine("RemoteAlerter constructor.");
}
public void SetAsSubscriber()
{
EventSubscribers.Add(this); //add subscriber to list
}
public bool FireAlert(string s)
{
//if there are subscribers to the published events
if ( RemoteAlertEvent != null && EventSubscribers.Count > 0)
{
Console.WriteLine("# of subscribers = {0}.",
EventSubscribers.Count);
for (int i = 0; i < EventSubscribers.Count; i++)
{
((RemoteAlerter)(EventSubscribers[i])).RemoteAlertEvent(s);
}
return true;
}
return false;
}
#endregion
#region private attributes and methods.
private ArrayList EventSubscribers = new ArrayList(); //required for
singleton use
#endregion
}
The name of the inherited class that contains the event handler is...
class RemoteHandler : IProcessActivator_ClientActivated.RemoteHandlerBase
{
override protected void HandleAlert(string msg)
{
Console.WriteLine("Alert from server: {0}", msg);
}
}
When I instantiate the inherited class on the client side no execeptions are
thrown. When I create an event...
RemoteAlerter alert = (RemoteAlerter)Activator.GetObject(typeof
(IProcessActivator_ClientActivated.RemoteAlerter),
"tcp://localhost:9000/RemoteAlerter");
RemoteHandler rh = new RemoteHandler();
alert.RemoteAlertEvent += new RemoteAlert(rh.Alert);
...again no execeptions are thrown, however when the event is fired on the
server it is never received by the client.
The connection to the remote class uses a TCP / Binary connection method
Any ideas?