.NET Remoting, Event Subscription to the Remoting Object / Subscribed event does not fire...

E

erbilkonuk

Hi,
I am very new to .NET Remoting and I try to run a simple program to
subscribe to an event raised by Remoting Class. The Remoting Server
initiates an instance of Remoting Class as Singleton / Server
activated mode on startup. The Remoting Client accesses the Remoting
Class through the interface of the Class and subscribes to an event
of
the Remoting Class that will be fired upon the private member value
change.

The problem is that I can not get the subscribed event fired. Do I
miss a point about Client Registration / Server Registration such
that
2 instances are created, 1 on Server Side, 1 on Client Side and the
event subscription is done on the wrong one? Please help :) Thanks in
advance...


[Remoting Class]
public class NetworkElement : MarshalByRefObject, INetworkElement
{
public event AlarmEventHandler AlarmEvent;


private Boolean _ACFail;
public Boolean ACFail
{
get { return _ACFail; }
set
{
_ACFail = value;
OnAlarmsEvent(new AlarmEventArgs(1));
}
}


protected void OnAlarmsEvent(AlarmEventArgs arg)
{
if (AlarmEvent != null) AlarmEvent(this, arg);
}


public void AddAlarmEvent(AlarmEventHandler handler)
{
AlarmEvent += handler;
}
}


[Interface for Remoting Class]
public delegate void AlarmEventHandler(object sender,
AlarmEventArgs arg);


[Serializable]
public class AlarmEventArgs : EventArgs
{
private Int32 _AlarmType;
public Int32 AlarmType
{
get { return _AlarmType; }
}


public AlarmEventArgs(Int32 Type)
{
_AlarmType = Type;
}
}


public interface INetworkElement
{
void AddAlarmEvent(AlarmEventHandler handler);
}


[The Remoting Client]
public class Client : MarshalByRefObject
{


public void SubscribeToAlarmEvent()
{
RemotingConfiguration.Configure("Client.exe.config");


WellKnownClientTypeEntry[] entry =
RemotingConfiguration.GetRegisteredWellKnownClientTypes();
INetworkElement ine =
(INetworkElement)Activator.GetObject(entry[0].ObjectType,
entry[0].ObjectUrl);
ine.AddAlarmEvent(new
AlarmEventHandler(ClientEventHandler));
AlarmType = -1;


}


public void ClientEventHandler(object sender, AlarmEventArgs
arg)
{
AlarmType = arg.AlarmType;
}


}


config files are :


[Server.exe.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel port="6000" displayName="ServerChannel" ref="tcp">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"> </
formatter>
</serverProviders>


<clientProviders>
<formatter ref="binary"></formatter>
</clientProviders>
</channel>
</channels>
<service>
<wellknown type="NetworkE.NetworkElement, NetworkElement"
objectUri="NetworkElement.rem" mode="Singleton">
</wellknown>
</service>
</application>
</system.runtime.remoting>
</configuration>


[Client.exe.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel port="0" displayName="ClientChannel" ref="tcp">
<clientProviders>
<formatter ref="binary"></formatter>
</clientProviders>
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"></
formatter>
</serverProviders>
</channel>
</channels>
<client>
<wellknown type="INetworkE.INetworkElement, INetworkElement"
url="tcp://localhost:6000/NetworkElement.rem">


</wellknown>
</client>
</application>
</system.runtime.remoting>
</configuration>
 
L

Lebesgue

This is a common question - see
http://www.google.sk/search?hl=sk&q=.net+remoting+events&meta= with many
relevant results.

erbilkonuk said:
Hi,
I am very new to .NET Remoting and I try to run a simple program to
subscribe to an event raised by Remoting Class. The Remoting Server
initiates an instance of Remoting Class as Singleton / Server
activated mode on startup. The Remoting Client accesses the Remoting
Class through the interface of the Class and subscribes to an event
of
the Remoting Class that will be fired upon the private member value
change.

The problem is that I can not get the subscribed event fired. Do I
miss a point about Client Registration / Server Registration such
that
2 instances are created, 1 on Server Side, 1 on Client Side and the
event subscription is done on the wrong one? Please help :) Thanks in
advance...


[Remoting Class]
public class NetworkElement : MarshalByRefObject, INetworkElement
{
public event AlarmEventHandler AlarmEvent;


private Boolean _ACFail;
public Boolean ACFail
{
get { return _ACFail; }
set
{
_ACFail = value;
OnAlarmsEvent(new AlarmEventArgs(1));
}
}


protected void OnAlarmsEvent(AlarmEventArgs arg)
{
if (AlarmEvent != null) AlarmEvent(this, arg);
}


public void AddAlarmEvent(AlarmEventHandler handler)
{
AlarmEvent += handler;
}
}


[Interface for Remoting Class]
public delegate void AlarmEventHandler(object sender,
AlarmEventArgs arg);


[Serializable]
public class AlarmEventArgs : EventArgs
{
private Int32 _AlarmType;
public Int32 AlarmType
{
get { return _AlarmType; }
}


public AlarmEventArgs(Int32 Type)
{
_AlarmType = Type;
}
}


public interface INetworkElement
{
void AddAlarmEvent(AlarmEventHandler handler);
}


[The Remoting Client]
public class Client : MarshalByRefObject
{


public void SubscribeToAlarmEvent()
{
RemotingConfiguration.Configure("Client.exe.config");


WellKnownClientTypeEntry[] entry =
RemotingConfiguration.GetRegisteredWellKnownClientTypes();
INetworkElement ine =
(INetworkElement)Activator.GetObject(entry[0].ObjectType,
entry[0].ObjectUrl);
ine.AddAlarmEvent(new
AlarmEventHandler(ClientEventHandler));
AlarmType = -1;


}


public void ClientEventHandler(object sender, AlarmEventArgs
arg)
{
AlarmType = arg.AlarmType;
}


}


config files are :


[Server.exe.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel port="6000" displayName="ServerChannel" ref="tcp">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"> </
formatter>
</serverProviders>


<clientProviders>
<formatter ref="binary"></formatter>
</clientProviders>
</channel>
</channels>
<service>
<wellknown type="NetworkE.NetworkElement, NetworkElement"
objectUri="NetworkElement.rem" mode="Singleton">
</wellknown>
</service>
</application>
</system.runtime.remoting>
</configuration>


[Client.exe.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel port="0" displayName="ClientChannel" ref="tcp">
<clientProviders>
<formatter ref="binary"></formatter>
</clientProviders>
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"></
formatter>
</serverProviders>
</channel>
</channels>
<client>
<wellknown type="INetworkE.INetworkElement, INetworkElement"
url="tcp://localhost:6000/NetworkElement.rem">


</wellknown>
</client>
</application>
</system.runtime.remoting>
</configuration>
 
N

Nicholas Paldino [.NET/C# MVP]

More likely than not, your service is not aware of the type of the
client (which is understandable). Events on objects that are exposed by
services are generally a bad idea.

What you want to do is create a proxy class which exposes the events you
want to fire. It should also implement an interface that has methods that
correspond to the event signatures. This proxy and the interface should
reside in a dll that is outside of the service and the client, but
referenced by both.

Then, you have a method on the service which you can use to register the
INTERFACE (not the actual type). Of course, the implementation of the proxy
derives from MarshalByRefObject.

Then, when you want to fire the event, the service calls the method on
the interface, which will then trigger the event in the proxy, which the
client will have subscribed to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

erbilkonuk said:
Hi,
I am very new to .NET Remoting and I try to run a simple program to
subscribe to an event raised by Remoting Class. The Remoting Server
initiates an instance of Remoting Class as Singleton / Server
activated mode on startup. The Remoting Client accesses the Remoting
Class through the interface of the Class and subscribes to an event
of
the Remoting Class that will be fired upon the private member value
change.

The problem is that I can not get the subscribed event fired. Do I
miss a point about Client Registration / Server Registration such
that
2 instances are created, 1 on Server Side, 1 on Client Side and the
event subscription is done on the wrong one? Please help :) Thanks in
advance...


[Remoting Class]
public class NetworkElement : MarshalByRefObject, INetworkElement
{
public event AlarmEventHandler AlarmEvent;


private Boolean _ACFail;
public Boolean ACFail
{
get { return _ACFail; }
set
{
_ACFail = value;
OnAlarmsEvent(new AlarmEventArgs(1));
}
}


protected void OnAlarmsEvent(AlarmEventArgs arg)
{
if (AlarmEvent != null) AlarmEvent(this, arg);
}


public void AddAlarmEvent(AlarmEventHandler handler)
{
AlarmEvent += handler;
}
}


[Interface for Remoting Class]
public delegate void AlarmEventHandler(object sender,
AlarmEventArgs arg);


[Serializable]
public class AlarmEventArgs : EventArgs
{
private Int32 _AlarmType;
public Int32 AlarmType
{
get { return _AlarmType; }
}


public AlarmEventArgs(Int32 Type)
{
_AlarmType = Type;
}
}


public interface INetworkElement
{
void AddAlarmEvent(AlarmEventHandler handler);
}


[The Remoting Client]
public class Client : MarshalByRefObject
{


public void SubscribeToAlarmEvent()
{
RemotingConfiguration.Configure("Client.exe.config");


WellKnownClientTypeEntry[] entry =
RemotingConfiguration.GetRegisteredWellKnownClientTypes();
INetworkElement ine =
(INetworkElement)Activator.GetObject(entry[0].ObjectType,
entry[0].ObjectUrl);
ine.AddAlarmEvent(new
AlarmEventHandler(ClientEventHandler));
AlarmType = -1;


}


public void ClientEventHandler(object sender, AlarmEventArgs
arg)
{
AlarmType = arg.AlarmType;
}


}


config files are :


[Server.exe.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel port="6000" displayName="ServerChannel" ref="tcp">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"> </
formatter>
</serverProviders>


<clientProviders>
<formatter ref="binary"></formatter>
</clientProviders>
</channel>
</channels>
<service>
<wellknown type="NetworkE.NetworkElement, NetworkElement"
objectUri="NetworkElement.rem" mode="Singleton">
</wellknown>
</service>
</application>
</system.runtime.remoting>
</configuration>


[Client.exe.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel port="0" displayName="ClientChannel" ref="tcp">
<clientProviders>
<formatter ref="binary"></formatter>
</clientProviders>
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"></
formatter>
</serverProviders>
</channel>
</channels>
<client>
<wellknown type="INetworkE.INetworkElement, INetworkElement"
url="tcp://localhost:6000/NetworkElement.rem">


</wellknown>
</client>
</application>
</system.runtime.remoting>
</configuration>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top