Loren Baur

Loren Baur

  • NA
  • 1
  • 0

Remoting & Events ("Exception has been thrown by the target of an invocation.”) - urgent!

Mar 21 2010 2:16 PM

Hi,
I have been struggling with receiving events from a server to a client (using .NET remoting) for quite a long time now but I keep receiving the following message on client side ("Exception has been thrown by the target of an invocation."). This message is received as the server is trying to call the event handler while the event handler exists in the client and since the client assembly is not available on the side of the server, this message comes.
Any idea?

Server Class library
-----------------------------

 

 
public delegate void MyDel();

public class ServerClass : MarshalByRefObject
{
public event MyDel myEvent;

public void StartServer (bool shouldFireEvent)
{
TcpServerChannel tsc;
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary di = new Hashtable();
di["port"] = 8085;
di["bindTo"] = "172.19.35.191";
tsc = new TcpServerChannel(di, provider);
ChannelServices.RegisterChannel(tsc, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerClass), "myURI", WellKnownObjectMode.Singleton);

if (shouldFireEvent)
{
while (true)
{
myEvent();
Thread.Sleep(1000);
}
}
}
}
Server GUI
-----------------------------
 
class ServerGui
{
static void Main(string[] args)
{
ServerClass server = new ServerClass();
server.myEvent += new MyDel(ReceivedEventFromServerHandler); //lets register to event
server.StartServer(true);
Console.WriteLine("server is running. press enter to quit");
Console.ReadKey();
}

static void ReceivedEventFromServerHandler()
{
Console.WriteLine("I am the server GUI. Got a message from the server");
}
}
Client
--------------
 
class ClientClass
{
static void Main(string[] args)
{
ServerClass server = (ServerClass)Activator.GetObject(typeof(ServerClass), "tcp://172.19.35.191:8085/myURI");
server.myEvent += new MyDel(server_myEvent); //HERE IT CRASHES
while (true)
{
Thread.Sleep(1000);
}
}

static void server_myEvent()
{
Console.WriteLine("I am the client. Got a message from the server over the network");
}
}